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

Slides:



Advertisements
Similar presentations
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Advertisements

TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Manipulating 2D arrays in Java
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
How to use the Java class libraries Brief documentation of how to do this all with Java.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
Copyright © 2015 Curt Hill Models and Textures Making your entity interesting.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Copyright © Curt Hill Turtles The beginning of media computation.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
Copyright © Curt Hill Multiple Dimension Arrays Extending Java Arrays.
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Day 8: Fruit Loops: Color. Loop Review What does the following loop print? for (int i= 0; i
Copyright Curt Hill GridBag Layout Manager A flexible but complicated layout.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Copyright Curt Hill Arrays in C/C++ What? Why? How?
An image has Reflective Symmetry if there is at least one line which splits the image in half so that one side is the mirror image of the other.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Getting and displaying
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Lab 9 Intro to Robots.
The for-each or for-all loop introduced in Java 5
Workshop for Programming And Systems Management Teachers
Two-Dimensional Arrays and Nested Loops – part 1
Two-Dimensional Arrays and Nested Loops – part 2
Two-Dimensional Arrays and Nested Loops – part 1
Looping through pixels.
CSE 8A Lecture 6 Reading for next class:
The most important decision statement
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Arrays in Java What, why and how Copyright Curt Hill.
Other displays Saving Arrays Using fors to process
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
CSC1401 Viewing a picture as a 2D image - 2
February , 2009 CSE 113 B.
Manipulating Pictures, Arrays, and Loops
The beginning of media computation Followed by a demo
Presentation transcript:

Copyright © 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 © Curt Hill

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

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 © Curt Hill

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 © Curt Hill

Example Copyright © 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); }

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 © Curt Hill

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 © Curt Hill

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 © Curt Hill

Mirror Copyright © 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); }

Applied to the beach Copyright © Curt Hill

Applied to Barbara Copyright © Curt Hill

One more time Copyright © 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); }

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 © Curt Hill

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 © Curt Hill

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 © Curt Hill

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 © Curt Hill

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 © Curt Hill

Processing a picture Copyright © Curt Hill

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 © Curt Hill

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 © Curt Hill

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 © Curt Hill

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 © Curt Hill

The main program Copyright © 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(); }

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 © Curt Hill

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

The process method Copyright © 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

Rest of process method Copyright © 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

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