Other displays Saving Arrays Using fors to process

Slides:



Advertisements
Similar presentations
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.
Advertisements

ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Jeopardy Heading1Heading2Heading3Heading4 Heading5 Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Copyright © Curt Hill Turtles The beginning of media computation.
CSC1401. Learning Goals Understand at a conceptual level What is media computation? How does color vision work? How can you make colors with red, green,
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Copyright © Curt Hill Multiple Dimension Arrays Extending Java Arrays.
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.
Copyright © – Curt Hill Pointers A Light Introduction.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Copyright Curt Hill Arrays in C/C++ More on usage.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Recipes How to craft items from others Copyright © 2015 Curt Hill.
Copyright © Curt Hill Flow of Control A Quick Overview.
Copyright © Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
By Melissa Dalis Professor Susan Rodger Duke University June 2011
Getting and displaying
Flow of Control An Overview
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
A Review or Brief Introduction
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
The for-each or for-all loop introduced in Java 5
Creating and Modifying Text part 2
Chapter I Digital Imaging Fundamentals
Java Review: Reference Types
Learning to program with Logo
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Predefined Dialog Boxes
A brief look at some of the new features introduced to the language
Data Representation.
Doing things more than once
A Kind of Binary Tree Usually Stored in an Array
Looping through pixels.
The most important decision statement
int [] scores = new int [10];
Arrays in Java What, why and how Copyright Curt Hill.
Throwing and catching exceptions
Multidimensional Arrays and Image Manipulation
Introduction to Media Computation
Workshop for Programming And Systems Management Teachers
Multidimensional Arrays
Methods Again Parameter Passage
Manipulating Pictures, Arrays, and Loops
The Java switch Statement
Creative Commons Attribution Non-Commercial Share Alike License
CS 177 Week 3 Recitation Slides
Java SE 7 One and Multi Dimensional Arrays Module 6
Manipulating Pictures, Arrays, and Loops
Arrays 2-May-19.
CSC1401 Manipulating Pictures 2
Web Programming and Design
The IF Revisited A few more things Copyright © Curt Hill.
The beginning of media computation Followed by a demo
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Other displays Saving Arrays Using fors to process Picture Processing Other displays Saving Arrays Using fors to process Just using counting for Copyright © 2009 – 2016 Curt Hill

Up to now we can: Use FileChooser to find a picture Load a picture into memory Display it with the show method Drop it into a turtle world Have the turtle draw over it Consider the next screen for a sample program that does the first three of these Copyright © 2009 – 2016 Curt Hill

Load and Display public class PictureDemo2{ public static void main(String [] a){ String fileName; FileChooser.setMediaPath( “.../mediasources"); fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); p.show(); } Copyright © 2009 – 2016 Curt Hill

Picture Explorer One of the things that we want to do is look at or change individual pixels The picture show method does not help us with this There is a Picture explorer which does Instead of: p.show(); use: p.explore(); Copyright © 2009 – 2016 Curt Hill

What it does Picture explorer (or just explorer) displays the picture It also allows you to click any point on the picture When you do it shows: The x and y locations The red, green and blue values at that location Consider the beach picture next Look for the cross hairs Copyright © 2009 – 2016 Curt Hill

Picture Explorer Copyright © 2009 – 2016 Curt Hill

Commentary The cross hair was at location x = 230, y = 289 The color was: red = 145 green = 160 blue = 165 This is a handy tool for determining what kind of colors exist in an area or in a picture Copyright © 2009 – 2016 Curt Hill

Saving a picture So far we have not changed anything on a picture so there is nothing to save However, we are going to look at the saving next and then modification The method needed is write It requires a filename as a parameter If you use the old filename you will replace the file Generally, not a good idea Copyright © 2009 – 2016 Curt Hill

Saving You may use FileChooser to select the new filename You may select an existing file or type in a new one Consider the next two screens The new program The execution Copyright © 2009 – 2016 Curt Hill

Load, Display, Save public class PictureDemo3{ public static void main(String [] a){ String fileName; FileChooser.setMediaPath(“.../mediasources"); fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); p.explore(); process(p); p.write(fileName); } Copyright © 2009 – 2016 Curt Hill

Choosing Save File Name Copyright © 2009 – 2016 Curt Hill

Commentary Did you notice the process method? This is where we want to do something to the picture Inside this method we will use a for to do something to each pixel Most of our picture processing programs will have this form Main method reads/displays picture Process method modifies it Before we do that we need to consider a few things Copyright © 2009 – 2016 Curt Hill

What We need to be able to access a one dimensional array of pixels Later we will consider arrays in more depth We need to know how many pixels in the above array We need to know what can be done to each pixel First a digression through arrays Copyright © 2009 – 2016 Curt Hill

What is an array? Multiple occurrences of values of one type One name many values Item is selected at run time by an integer index A true data type in Java, unlike C++ Always an object Always dynamic The mark of arrays is a pair of square brackets: [ ] Copyright © 2009 – 2016 Curt Hill

Why do we need arrays? We often need to select a value by an index rather than just name A picture will typically have thousands or millions of pixels Awkward to think of having thousands or millions of names, one for each pixel Instead we find one name and use an integer subscript to choose which of the pixels we want Copyright © 2009 – 2016 Curt Hill

Obtaining the pixel array Pictures have a method called getPixels It returns an array of pixels We will process that array with a counting for today and a for all later Suppose that p is our picture, then use: Pixel [] pixels= p.getPixels(); Let us consider this further Copyright © 2009 – 2016 Curt Hill

Obtaining Pixels The type that is returned: Pixel [] is actually an array of pixels The brackets [ ] is the sure sign of an array in Java The size will be determined by what it receives We can obtain either a one dimensional or two dimensional access to the pixels For now we prefer the simpler one dimensional Copyright © 2009 – 2016 Curt Hill

Size How many pixels are there? We do not care before hand, but must know at run-time in order to process each one This is done using the length property Any array may be asked its size int i = pixels.length; Since it is a property it needs no parentheses Copyright © 2009 – 2016 Curt Hill

Basic Process Form static void process(Picture p){ Pixel [] px = p.getPixels(); int len = px.length; for(int i = 0;i<len;i++){ Pixel item = px[i]; //... } // end of for } // end of process Copyright © 2009 – 2016 Curt Hill

Commentary The getPixels obtains a handle to the array Inside the for we obtain one element of that array and make it a Pixel What can we do to a pixel? Copyright © 2009 – 2016 Curt Hill

A Pixel object The type is Pixel A pixel contains information about its location in the picture as well as the color Recall that the three colors in a picture are red, green and blue This leads to six methods: getRed and setRed getGreen and setGreen getBlue and setBlue Copyright © 2009 – 2016 Curt Hill

Getters and Setters Typical in Java for an object to have a get and set method The get usually returns a value as its result: int r = px.getRed(); The set takes a parameter which is the new value: px.setRed(r); Copyright © 2009 – 2016 Curt Hill

Example: Remove Red If we take the process method and have it remove all the red, what will happen? How will we do this? In the for setRed to zero The next two screens shows the code and results Copyright © 2009 – 2016 Curt Hill

The main program public static void main(String [] a){ String fileName; FileChooser.setMediaPath(“…/mediasources"); fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); p.explore(); process(p); //fileName = FileChooser.pickAFile(); //p.write(fileName); } Copyright © 2009 – 2016 Curt Hill

The process method static void process(Picture p){ Pixel [] px = p.getPixels(); int len = px.length; for(int i = 0;i<len;i++){ Pixel item = px[i]; item.setRed(0); } Copyright © 2009 – 2016 Curt Hill

Red Removed Copyright © 2009 – 2016 Curt Hill

Filters A photographic filter removes some of the light from a the camera The removal of red was like that We can also make the image a single color, by setting two of the colors to zero Copyright © 2009 – 2016 Curt Hill

Red Only Copyright © 2009 – 2016 Curt Hill

Darkening So far we have just set the colors to predefined values such as zero Most of the time we want to modify the colors Based upon existing values In this next one we make each pixel three quarters as bright Copyright © 2009 – 2016 Curt Hill

The process method static void process(Picture p){ // Process every pixel Pixel [] pixels = p.getPixels(); int len = pixels.length; for(int i = 0;i<len;i++){ Pixel px = pixels[i]; // Do something with px int j = px.getRed()*3/4; px.setRed(j); j = px.getGreen()*3/4; px.setGreen(j); j = px.getBlue()*3/4; px.setBlue(j); } Copyright © 2009 – 2016 Curt Hill

Commentary In the dimming process we obtain each pixel’s red value and reduce it by ¾ Set it back into the red Repeat for green and blue Copyright © 2009 – 2016 Curt Hill

Three Quarters Bright Copyright © 2009 – 2016 Curt Hill

Commentary In the dimming process we obtain each pixel’s red value and reduce it by ¾ Set it back into the red Repeat for green and blue Copyright © 2009 – 2016 Curt Hill

Pixels again Recall that the values for each color are strictly in the range 0 – 255 Thus we should make sure that whatever modifications we do leaves them in this range Thus brightening should not increase a color past 255 Copyright © 2009 – 2016 Curt Hill

Finally We are now able to read in a picture, modify it and display the result This allows us to do most any kind of processing that does not care where the pixel came from We will later do processing that is positionally sensitive Copyright © 2009 – 2016 Curt Hill