<< Chapter < Page Chapter >> Page >

Construct output pixel color

You can selectively enable and disable the last two lines of code in Listing 1 to cause the output image to be either the raw image (as in Image 1 ) or a modified version of the raw image (as in Image 2 ). As explained earlier , the red and green color components are inverted and the blue color component is set to zero in the last line of code in Listing 1 .

The remainder of the for loop

The code in Listing 2 is the same as in Pr0120-Image Explorer . It is included here to provide continuity in the discussion.

Listing 2. the remainder of the for loop.

if(width>= img.width){ if((cnt % img.width == 0)&&(cnt != 0)){ //Compensate for excess display width by// increasing the output counter. ctr += (width - img.width);}//end if //Store the pixel in the output pixel array// and increment the output counter. pixels[ctr]= c; ctr++;}//end if }//end for loopupdatePixels();//required }//end else
Listing 2. The remainder of the for loop.

The remainder of the run method

The code in Listing 3 is only slightly different from the code in Pr0120-Image Explorer .

Listing 3. the remainder of the run method.

//Display the author's name on the output.text("Dick Baldwin",10,20);//Disable the requirement to press a mouse button to // display information about the pixel.// if(mousePressed){ displayPixelInfo(img);// }//end if }//end run
Listing 3. The remainder of the run method.

The call to the text method was inserted in Listing 3 to display my name in the upper-left corner of Image 2 .

The call to the displayPixelInfo method was removed from the if statement in Listing 3 . This causes the pixel information shown at the bottom of Image 2 to be displayed any time that the mouse pointer is inside the display window withno requirement to press a mouse button to display the information.

(The information is actually displayed all of the time based on the last known location of the mouse pointer. At the beginning, the mouse pointer isassumed to be at coordinates 0,0. After that, if the mouse enters and then leaves the output display window, the last known location is the point on the edge where it leftthe window.)

The remainder of the class named class Pr0130aRunner

The remainder of the class named class Pr0130aRunner is essentially the same as before. I explained the entire class in Pr0120-Image Explorer and won't repeat that explanation in this module.

Run the program

I encourage you to copy the code from Listing 4 and Listing 5 into your PDE. Be sure to put the code from Listing 4 in the leftmost tab.

Don't forget to put an image file of your choice in a folder named data that is a child of the folder that contains the files with the .pde extension. You will need to edit the code from Listing 4 to change the name of the image file in two different places . Change the name from Pr0130a.jpg to the name of your file.

Run the sketchand observe the results. Experiment with the code. Make changes, run the sketch again, and observe the results of your changes. Make certain that you can explain why your changes behave as they do.

Don't forget to also create and run the JavaScript version of your sketch in your HTML 5 compatible browser.

Click here to view my JavaScript version of the sketch in your HTML 5compatible browser.

If you have a programmable Android device , try creating and running the Android version of your sketch in your Android device.

Also try creating and running the stand-alone version of the sketch by selecting Export Application from the File menu while in Java mode.

Summary

This module introduced you to pixel-based image processing algorithms, similar to those that you might find in commercial image editing software suchas Photoshop .

Click here to view the JavaScript version of the sketch discussed in this module in your HTML 5compatible browser.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Pr0130-Introduction to Image Processing Algorithms
  • File: Pr0130.htm
  • Published: 02/26/13
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

Complete listings of the classes discussed in this module are provided in Listing 4 and Listing 5 .

Listing 4. class pr0130a.

/*Pr0130a.pde Copyright 2013, R.G.BaldwinProgram illustrates how to write a relatively simple image processing algorithm and how to display the output inan image explorer. The image explorer displays the coordinates of the mouse pointer along with the RGB colorvalues of the pixel at the mouse pointer. Also displays the width and height of the image.Displays an error message in place of the image if the image is wider or taller than the output display window.**********************************************************/ //@pjs preload required for JavaScript version in browser./* @pjs preload="Pr0130a.jpg"; */ PImage img;PFont font; Pr0130aRunner obj;void setup(){ //This size matches the width of the image and allows// space below the image to display the text information. size(365,344);frameRate(30); img = loadImage("Pr0130a.jpg");obj = new Pr0130aRunner(); font = createFont("Arial",16,true);}//end setup //-------------------------------------------------------//void draw(){ obj.run();}//end draw
Listing 4. Class Pr0130a.

Listing 5. class pr0130arunner.

class Pr0130aRunner{void run(){ background(255);//whitetextFont(font,16);//Set the font size, and colorfill(0);//black textloadPixels();//required img.loadPixels();//requiredfloat reD,greeN,bluE;//store color values hereint ctr = 0;//output pixel array counter //Display error message in place of image if the// image won't fit in the display window. if(img.width>width){ text("--Image too wide--",10,20);text("Image width: " + img.width,10,40); text("Display width: " + width,10,60);}else if(img.height>height){ text("--Image too tall--",10,20);text("Image height: " + img.height,10,40); text("Display height: " + height,10,60);}else{ //Copy pixel colors from the input image to the// display image. for(int cnt = 0;cnt<img.pixels.length;cnt++){ //Get and save RGB color values for current pixel.reD = red(img.pixels[cnt]);greeN = green(img.pixels[cnt]);bluE = blue(img.pixels[cnt]);//Construct output pixel color //Selectively enable and disable the following two// statements to display either the raw image, or // a modified version of the raw image where the// red and green color components have been // inverted and the blue color component has been// set to zero. //color c = color(reD, greeN, bluE);//rawcolor c = color(255-reD, 255-greeN, 0);//modifiedif(width>= img.width){ if((cnt % img.width == 0)&&(cnt != 0)){ //Compensate for excess display width by// increasing the output counter. ctr += (width - img.width);}//end if //Store the pixel in the output pixel array// and increment the output counter. pixels[ctr]= c; ctr++;}//end if }//end for loopupdatePixels();//required }//end else//Display the author's name on the output.text("Dick Baldwin",10,20);//Disable the requirement to press a mouse button to // display information about the pixel.// if(mousePressed){ displayPixelInfo(img);// }//end if }//end run//-----------------------------------------------------// //Method to display coordinate and pixel color info at// the current mouse pointer location. Also displays // width and height information about the image.void displayPixelInfo(PImage image){ //Protect against mouse being outside the frameif((mouseX<width)&&(mouseY<height)&&(mouseX>= 0)&&(mouseY>= 0)){//Get and display the width and height of the // image.text("Width: " + image.width + " Height: " + image.height,10,height - 50);//Get and display coordinates of mouse pointer.text("X: " + mouseX + ", Y: " + mouseY,10, height - 30);//Get and display color data for the pixel at the// mouse pointer. text("R: " + red(pixels[mouseY*width+mouseX]) + " G: " + green(pixels[mouseY*width+mouseX]) + " B: " + blue(pixels[mouseY*width+mouseX]), 10,height - 10);}//end if }//end displayPixelInfo}//end class Pr0130aRunner
Listing 5. Class Pr0130aRunner.

-end-

Questions & Answers

what is defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, The processing programming environment. OpenStax CNX. Feb 26, 2013 Download for free at http://cnx.org/content/col11492/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'The processing programming environment' conversation and receive update notifications?

Ask