Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2008-2010 Curt Hill Further Picture Manipulation Considering position.

Similar presentations


Presentation on theme: "Copyright © 2008-2010 Curt Hill Further Picture Manipulation Considering position."— Presentation transcript:

1 Copyright © 2008-2010 Curt Hill Further Picture Manipulation Considering position

2 Previously We have manipulated pictures with a one dimensional array of pixels This if fine for changing each pixel in some specified way It ignores lines boundaries and other important information It would be impossible to do many interesting manipulations Copyright © 2008-2010 Curt Hill

3 What sort of manipulations? There are many manipulations that require some knowledge of relative position Mirroring the image Finding points of high contrast Altering the size of an image Copyright © 2008-2010 Curt Hill

4 How to obtain a pixel in context? Getting the pixel is not enough We need to know where it is from as well This is done with the getPixel method getPixel is a method of Picture It takes two parameters –The x and y locations Copyright © 2008-2010 Curt Hill

5 Processing a picture This also changes how we process a picture Up to this point we have used a single counting for or a single for all What we have to do now is use two nested counting fors One for processes the rows –Count from zero to height The second the position within a row Copyright © 2008-2010 Curt Hill

6 Example Copyright © 2008-2010 Curt Hill public class PictureDemo5{ static void process(Picture p){ for(int y=0;y<p.getHeight();y++) for(int x=0;x<p.getWidth();x++){ Pixel px = p.getPixel(x,y); Color color = px.getColor(). brighter(); px.setColor(color); }

7 Discussion Two for loops here First one went from zero to p.getHeight()-1 –This is the outer loop –It processes rows The inner loop processes one row –Counts from zero to p.getWidth()-1 The pixel is obtained with: p.getPixel(x,y); Copyright © 2008-2010 Curt Hill

8 More discussion Once pixel is obtained we can do anything we want to the pixel In this case we used getColor to obtain the color Then we made it brighter using the brighter method What we did this time was possible with simpler processing of pixels Lets do something we could not do before Copyright © 2008-2010 Curt Hill

9 Mirroring The idea is to reflect half of an image across a line The line can be horizontal or vertical or diagonal Here we take the obvious one reflect across a vertical line that is in the middle Copyright © 2008-2010 Curt Hill

10 Mirror Copyright © 2008-2010 Curt Hill static void process(Picture p){ int mirror = p.getWidth()/2; int width = p.getWidth()-1; for(int y=0;y<p.getHeight();y++) for(int x=0;x<mirror;x++){ // Get left half pixel Pixel px = p.getPixel(x,y); int c = px.getColor().getRGB(); // Set into right half p.setBasicPixel(width-x,y,c); }

11 Applied to the beach Copyright © 2008-2010 Curt Hill

12 Applied to Barbara Copyright © 2008-2010 Curt Hill

13 One more time Copyright © 2008-2010 Curt Hill static void process(Picture p){ int mirror = p.getWidth()/2; int width = p.getWidth()-1; for(int y=0;y<p.getHeight();y++) for(int x=0;x<mirror;x++){ // Get left half pixel Pixel px = p.getPixel(x,y); int c = px.getColor().getRGB(); // Set into right half p.setBasicPixel(width-x,y,c); }

14 Discussion The variable mirror is the midpoint The variable width is the width minus one The method getRGB converts a pixel’s colors into an integer –This is what is needed for setBasicPixel This is an application that is impossible without understanding of the line boundaries Copyright © 2008-2010 Curt Hill

15 Reminder on Arrays The getPixel takes two parameters These are essentially array subscripts Recall the valid rows are zero to Height – 1 The valid positions in a row are zero to Width – 1 Notice the getWidth() – 1 in in the assignment Copyright © 2008-2010 Curt Hill

16 Subscript errors If the integers of getPixel are not in proper range a subscript error occurs x range is zero to Width – 1 y range is zero to Height – 1 Copyright © 2008-2010 Curt Hill

17 Changing picture size There are several ways One is to use a specialized program such as Adobe Photoshop If you can afford it Lets try doing it with a program Copyright © 2008-2010 Curt Hill

18 Reduction Strategy In this approach we will reduce each dimension by a factor of 2 Thus if the picture is 800 by 600 we will reduce to 400 by 300 How will we get rid of ¾ of the pixels? We will create a new pixel by averaging 4 pixels into one Copyright © 2008-2010 Curt Hill

19 Processing a picture Copyright © 2008-2010 Curt Hill 0 1 234 6 0 1 2 3 4 5 7 0 1 234 6 0 1 2 3 4 5 7

20 General Technique Read in an existing picture Create a new picture with reduced dimensions Copy the average of groups of four pixels of the old into the new Write out the new picture Copyright © 2008-2010 Curt Hill

21 The copying of old to new Count x by twos horizontally Count y by twos vertically Average the four pixels that are at (x,y), (x+1,y), (x,y+1) and (x+1,y+1) Set a new picture pixel with this average Copyright © 2008-2010 Curt Hill

22 Picture Constructor We have already seen this picture constructor: Picture p; p = new Picture(fileName); The fileName was obtained using FileChooser There are other constructors as well The one we need is based on size Copyright © 2008-2010 Curt Hill

23 Sized Constructor To create a new Picture –Not based on an existing file It takes two integer: Picture p; p = new Picture(400,300); This creates a blank image of this size We then have to fill in the pixels Copyright © 2008-2010 Curt Hill

24 The main program Copyright © 2008-2010 Curt Hill public static void main(String [] a){ String fileName; FileChooser.setMediaPath( “…/mediasources"); fileName = FileChooser.pickAFile(); Picture p1 = new Picture(fileName); Picture p2 = new Picture( p1.getWidth()/2,p1.getHeight()/2); p1.explore(); process(p1,p2); fileName = FileChooser.pickAFile(); System.out.println(fileName); p2.write(fileName); p2.show(); }

25 A Problem with Explore We had to use p2.show(); rather than p2.explore(); The explore will give an error with this kind of construction Copyright © 2008-2010 Curt Hill

26 Process Copyright © 2008-2010 Curt Hill Take four adjacent pixels. Average colors. Make into one new pixel.

27 The process method Copyright © 2008-2010 Curt Hill static void process(Picture p, Picture n){ int mirror = p.getWidth(); int width = p.getWidth()-1; for(int y=0;y<p.getHeight();y+=2) for(int x=0;x<width;x+=2){ int red = 0, green = 0, blue = 0; for(int x2 = 0; x2<2; x2++) for(int y2 = 0;y2<2; y2++){ Pixel px = p.getPixel(x+x2,y+y2); red += px.getRed(); green += px.getGreen(); blue += px.getBlue(); } // More on next page

28 Rest of process method Copyright © 2008-2010 Curt Hill int newx = x/2, newy = y/2; Pixel pix = n.getPixel(newx,newy); pix.setRed(red/4); pix.setGreen(green/4); pix.setBlue(blue/4); } // end of for } // This is within the outer two for loops // Set the averaged pixel into // the new picture

29 Now some examples Lets try the following: Other reflections Reducing the width and height by factor of two Blurring the picture Copyright © 2008-2010 Curt Hill


Download ppt "Copyright © 2008-2010 Curt Hill Further Picture Manipulation Considering position."

Similar presentations


Ads by Google