Outline. Nested Loops. Nested loops. Nested loops. Nested loops TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS

Similar documents
Copies of the Color by Pixel template sheets (included in the Resources section). Colored pencils, crayons, markers, or other supplies for coloring.

Chapter 4: Modifying Pixels in a Range

Lesson 16: Relating Scale Drawings to Ratios and Rates

4/9/2015. Simple Graphics and Image Processing. Simple Graphics. Overview of Turtle Graphics (continued) Overview of Turtle Graphics

Chapter 4: Modifying Pixels in a Range

Inductive Reasoning Practice Test. Solution Booklet. 1

Learn to use translations, reflections, and rotations to transform geometric shapes.

RGB COLORS. Connecting with Computer Science cs.ubc.ca/~hoos/cpsc101

Introduction to DSP ECE-S352 Fall Quarter 2000 Matlab Project 1

Exercise NMCGJ: Image Processing

Color each numeral card. Count the objects in each group. Then color the group of objects the same color as the numeral card that it matches.

PASS Sample Size Software

Image and Multidimensional Signal Processing

Lesson 16: Relating Scale Drawings to Ratios and Rates

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES

Terrie Sandelin Miniatures in Minutes

Part 1. Using LabVIEW to Measure Current

Processing Image Pixels, Performing Convolution on Images. Preface

1. Setup Output mode. 2. Using a Fixed tile size

Picture Encoding and Manipulation. We perceive light different from how it actually is

Swirlygig. Finished Size: Approximately 76" x 100"

CSE 166: Image Processing. Overview. What is an image? Representing an image. What is image processing? History. Today

IN THIS ISSUE. Cave vs. Pentagroups

Activity 5.2 Making Sketches in CAD

Section 5: DIAGONALS

Nested Mini Right Triangle Instructions

Computer Science COMP-250 Homework #4 v4.0 Due Friday April 1 st, 2016

How to Create Animated Vector Icons in Adobe Illustrator and Photoshop

In the game of Chess a queen can move any number of spaces in any linear direction: horizontally, vertically, or along a diagonal.

Latin Squares for Elementary and Middle Grades

Rubik's Magic Transforms

IMAGELAB A PLATFORM FOR IMAGE MANIPULATION ASSIGNMENTS. as published in The Journal of Computing Sciences in Colleges, Vol.

Exercise 2-2. Antenna Driving System EXERCISE OBJECTIVE DISCUSSION OUTLINE DISCUSSION

GO! Qube 12" Laura s Kitty Quilt Finished Size: 40" x 46"

Module 2 Drawing Shapes and Repeating

CIS581: Computer Vision and Computational Photography Homework: Cameras and Convolution Due: Sept. 14, 2017 at 3:00 pm

Solution Algorithm to the Sam Loyd (n 2 1) Puzzle

PASS Sample Size Software. These options specify the characteristics of the lines, labels, and tick marks along the X and Y axes.

Engineering & Computer Graphics Workbook Using SolidWorks 2014

Preparation Part 1.1

Pointers. The Rectangle Game. Robb T. Koether. Hampden-Sydney College. Mon, Jan 21, 2013

CREO.1 MODELING A BELT WHEEL

CSE1710. Big Picture. Reminder

Computer Graphics (CS/ECE 545) Lecture 7: Morphology (Part 2) & Regions in Binary Images (Part 1)

Engineering & Computer Graphics Workbook Using SOLIDWORKS

Chapter 7 Image Processing

Shuli s Math Problem Solving Column

AgilEye Manual Version 2.0 February 28, 2007

Build the clerestory of Chartres Cathedral

Describing an Angle Bracket


Pictures and Arrays. You have been returned to where you were last working. Return to syllabus Edit preferences

Downloaded from

MATLAB Image Processing Toolbox

Dutch Sudoku Advent 1. Thermometers Sudoku (Arvid Baars)

CSC 320 H1S CSC320 Exam Study Guide (Last updated: April 2, 2015) Winter 2015

1. Use Pattern Blocks. Make the next 2 figures in each increasing pattern. a) 2. Write the pattern rule for each pattern in question 1.

GENERALIZATION: RANK ORDER FILTERS

PART 2 VARIA 1 TEAM FRANCE WSC minutes 750 points

Building Concepts: Fractions and Unit Squares

Digital Image Processing. Digital Image Fundamentals II 12 th June, 2017

Engineering Graphics Essentials with AutoCAD 2015 Instruction

How to Create Website Banners

CS431 homework 2. 8 June Question 1 (page 54, problem 2.3). Is lg n = O(n)? Is lg n = Ω(n)? Is lg n = Θ(n)?

Engineering Technology

FEATURES 24 PUZZLES, ASSORTED MIX, MOSTLY THEMED ON 24 HPC. HINTS FOR EACH PUZZLE. SOLUTIONS FOR EACH PUZZLE.

Exploring Concepts with Cubes. A resource book

More Recursion: NQueens

Unit 5 Shape and space

MID-LEVEL EXERCISES. Project Management Life Cycle. Project Management

Manipulative Mathematics Using Manipulatives to Promote Understanding of Math Concepts

Project One Report. Sonesh Patel Data Structures

CSE1710. Big Picture. Reminder

S206E Lecture 6, 5/18/2016, Rhino 3D Architectural Modeling an overview

Pixilation and Resolution name:

DeltaCad and Your Cylinder (Shepherd s) Sundial Carl Sabanski

Inside Outside Circles Outside Circles Inside. Regions Circles Inside Regions Outside Regions. Outside Inside Regions Circles Inside Outside

Module 1H: Creating an Ellipse-Based Cylindrical Sheet-metal Lateral Piece

WPF PUZZLE GP 2014 COMPETITION BOOKLET ROUND 1 WPF SUDOKU/PUZZLE GRAND PRIX 2014

INTRODUCTION TO COMPUTER SCIENCE I PROJECT 6 Sudoku! Revision 2 [2010-May-04] 1

Illustration 2 Illustration 3 Illustration 4. Illustration 5 Illustration 6 Illustration 7

Perfect Points Sampler

Lesson 12: The Scale Factor as a Percent for a Scale Drawing

Motion illusion, rotating snakes

G 1 3 G13 BREAKING A STICK #1. Capsule Lesson Summary

For Exercises 1 7, find the area and perimeter of each parallelogram. Explain how you found your answers for parallelograms 2, 6, and 7.

2. Use the Mira to determine whether these following symbols were properly reflected using a Mira. If they were, draw the reflection line using the

Pictorial Drawings. DFTG-1305 Technical Drafting Prepared by Francis Ha, Instructor

3. Create a rectangle that is 10mm W by 4mm H and place it in the middle of the big rectangle on the edge like this:

Martha s Quilted Wall Hanging

Hexagons for Art and Illusion Part II Get ready Start a new project FILE New Open Faced Cube Import the hexagon block LIBRARIES

GIMP (GNU Image Manipulation Program) MANUAL

JUNIOR CERTIFICATE 2009 MARKING SCHEME TECHNICAL GRAPHICS HIGHER LEVEL

EE 210 Lab Exercise #3 Introduction to PSPICE

Lane Detection in Automotive

Ornamental Pro 2010 Component Drawing Manual

Orthographic Projection 1

Transcription:

TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 2 2 Outline Using nested loops to process data in a matrix (2- dimensional array) More advanced ways of manipulating pictures in Java programs Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson. Nested Loops 3 3 Suppose we want to print a line of 40 dots. We can do this with a for loop: 4 4 Now suppose we want to print 5 rows of 40 dots each We can use a for loop to count the rows: for (int row = 1; row <= 5; row ++) // print 40 dots in a row for (int i=1; i<=40; i++) System.out.print(. ); System.out.println(); for (int i=1; i<=40; i++) System.out.print(. ); System.out.println(); 5 5 The for loop to print a row of dots is part of the body of the loop that counts the rows This is an example of nested loops The loop that counts the rows is called the outer loop The loop that prints each row of dots is called the inner loop 6 6 Another example: print a triangle of dots...... The outer loop will count the rows The inner loop will print the appropriate number of dots

77 for (int row = 1; row <= 5; row ++) // print dots in a row for (int i=1; i<=??; i++) System.out.print(. ); System.out.println(); 8 8 Exercise What would you change in the code of the previous slide so that it prints...... Picture manipulation More advanced picture manipulation 9 9 So far, we have used a single loop when modifying a picture We had a one-dimensional array of Pixels returned by the method getpixels() But this technique is only useful for simple picture manipulations that change every pixel the same way For example, decreasered(), negate(), etc. 10 We can only go so far in processing pictures without knowing where certain pixels are in an image, for example: Cropping a picture Copying just part of a picture Performing reflections Performing rotations We will now consider a picture as a matrix or two dimensional array Review: Pictures as a grid of pixels Pictures as 2-D arrays 11 Recall that pictures are organized as a grid of pixels The grid has columns and rows Each pixel has an (x, y) position in the grid x specifies the column y specifies the row We will now call this grid a matrix or 2-D array Y X 12 To access each pixel in the picture (i.e. in the 2-D array), we will use nested loops We can access pixels row by row: The outer loop moves horizontally along rows Then to get each pixel in a row, the inner loop moves vertically along columns Or we can access pixels column by column: The outer loop moves vertically along columns Then to get each pixel in a column, the inner loop moves horizontally along rows

Nested loop template (row-wise) 13 14 To get all the pixels in a picture using their x and y values row-wise (left to right, top to bottom) x=0, y=0 x=1, y=0 x=2, y=0 x=0, y=1 x=1, y=1 x=2, y=1 x=0, y=2 x=1, y=2 x=2, y=2 We have nested loops: The outer loop counts rows: y from 0 to (height 1) The inner loop counts columns: x from 0 to (width 1) // Loop through the rows (y direction) // Loop through the columns (x direction) for (int x = 0; x < this.getwidth(); x++) // Get the current pixel pixelobj = this.getpixel(x,y); // Do something to its color // Set the new color pixelobj.setcolor(colorobj); 15 Alternative nested loops To get all the pixels in a picture using their x and y values column-wise (top to bottom, left to right ) x=0, y=0 x=0, y=1 x=0, y=2 x=1, y=0 x=1, y=1 x=1, y=2 x=2, y=0 x=2, y=1 x=2, y=2 We again have nested loops: The outer loop counts columns: x from 0 to (width 1) The inner loop counts rows: y from 0 to (height 1) 16 Nested loop template (column-wise) // Loop through the columns (x direction) for (int x = 0; x < this.getwidth(); x++) // Loop through the rows (y direction) // Get the current pixel pixelobj = this.getpixel(x,y); // Do something to its color // Set the new color pixelobj.setcolor(colorobj); 17 Lightening an image Earlier, we saw how to lighten an image by accessing pixels through getpixels() This time, we will use nested loops We will do a column-wise implementation Exercise: write the row-wise version Lightening an image public void lighten2() Pixel pixelobj = null; Color colorobj = null; // loop through the columns (x direction) for (int x = 0; x < this.getwidth(); x++) // loop through the rows (y direction) 18

19 Continued // get pixel at the x and y location pixelobj = this.getpixel(x,y); // get the current color colorobj = pixelobj.getcolor(); // get a lighter color colorobj = colorobj.brighter(); 20 Exercise: Changing to Nested Loops Change the method clearblue() to use nested for loops to loop through all the pixels Check that the blue values are all 0 using the explore() method // set the pixel color to the lighter color pixelobj.setcolor(colorobj); //end of inner loop // end of outer loop 21 More advanced picture manipulations We will now consider image manipulations that do not alter all the pixels in a picture: Horizontal mirroring Others (textbook) Cropping Rotating Scaling 22 We place a mirror in the middle of a picture 23 To do this, we want to take the mirror image of the left half of the caterpillar and copy it to the right half 24 Left half: Copy to right half:

25 Width is 329 X ranges from 0 to 328 26 Bad approach: copy column 0 to column 164, column 1 to column 165, etc. Height is 150 Y ranges from 0 to 149 Left half: Width is 164 X ranges from 0 to 163 Midpoint = 329/2=164 Algorithm Algorithm to code 27 Loop through the rows (y values) Loop from x starting at 0 and going to just before the midpoint (mirror) value Get the left pixel, at x and y Get the right pixel, at (width -1 - x) and y Set the color for the right pixel to be the color of the left pixel (0,0) (1,0) (2,0) (3,0) (4,0) (0,1) (1,1) (2,1) (3,1) (4,1) (0,2) (1,2) (2,2) (3,2) (4,2) (0,0) (1,0) (2,0) (3,0) (4,0) (0,1) (1,1) (2,1) (3,1) (4,1) (0,2) (1,2) (2,2) (3,2) (4,2) 28 We are going to need the midpoint int midpoint = this.getwidth() / 2; Loop through the rows (y values) Loop through x values (starting at 0) for (int x = 0; x < midpoint; x++) Set right pixel color to left pixel color Pixel leftpixel = this.getpixel(x, y); Pixel rightpixel = this.getpixel(this.getwidth() - 1 - x, y); rightpixel.setcolor(leftpixel.getcolor()); Mirror vertical method Continued 29 public void mirrorvertical() int mirrorpoint = this.getwidth() / 2; Pixel leftpixel = null; Pixel rightpixel = null; // loop through the rows // loop from 0 to just before the mirror point for (int x = 0; x < mirrorpoint; x++) 30 leftpixel = this.getpixel(x, y); rightpixel = this.getpixel(this.getwidth() 1 x, y); rightpixel.setcolor(leftpixel.getcolor());

Trying the method Horizontal mirroring 31 Create the picture Picture p1 = new Picture( FileChooser.getMediaPath("caterpillar.jpg")); Call the method on the picture 32 Next: mirroring horizontally, i.e. around a mirror held horizontally in the vertical center of the picture p1.mirrorvertical(); Show the picture p1.show(); 33 Algorithm We will need the horizontal midpoint this time Loop through the columns (x values ) Loop from y=0 to y < midpoint Get the top pixel, at x and y Get the bottom pixel, at x and (height -1 - y) Set the color for the bottom pixel to be the color of the top pixel (0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2) (2,2) (0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2) (2,2) 34 Exercise Write the method to mirror the top half of the picture to the bottom half This is the motorcycle image in redmotorcycle.jpg Next: mirroring bottom to top Useful Mirroring Useful Mirroring 35 The Temple of Hephaistos in Athens, Greece has a damaged pediment. Goal: fix the temple. 36 We can t just mirror the left half onto the right We don t mirror all pixels How can we accomplish this? Choose a point to mirror around vertically Start with the row of pixels at the top of the pediment End with the row of pixels at the bottom of the pediment

37 Determining the region 37 38 Result Before and after: how can you tell that the picture has been digitally manipulated? Summary 39 Pictures as 2-D arrays of pixels Algorithms on Pictures: Horizontal mirroring