+ All Categories
Home > Documents > Copyright 2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Date post: 18-Jan-2018
Category:
Upload: kelley-tyler
View: 225 times
Download: 0 times
Share this document with a friend
Description:
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 © Curt Hill
29
Copyright © 2008-2010 Curt Hill Further Picture Manipulation Considering position
Transcript
Page 1: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

Copyright © 2008-2010 Curt Hill

Further Picture Manipulation

Considering position

Page 2: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 3: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 4: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 5: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 6: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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); } }

Page 7: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 8: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

beforeCopyright © 2008-2010 Curt Hill

Page 9: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 10: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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); } }

Page 11: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

Applied to the beach

Copyright © 2008-2010 Curt Hill

Page 12: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

Applied to Barbara

Copyright © 2008-2010 Curt Hill

Page 13: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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); } }

Page 14: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 15: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 16: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 17: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 18: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 19: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

Processing a picture

Copyright © 2008-2010 Curt Hill

0 1 2 3 4

6

012345

7

0 1 2 3 4

6

012345

7

Page 20: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 21: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 22: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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 sizeCopyright © 2008-2010 Curt Hill

Page 23: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 24: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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();}

Page 25: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 26: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

Process

Copyright © 2008-2010 Curt Hill

Take four adjacent pixels.Average colors.Make into one new pixel.

Page 27: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 28: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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

Page 29: Copyright  2008-2010 Curt Hill Further Picture Manipulation Considering position.

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


Recommended