CSC1401 Viewing a picture as a 2D image 1 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CSC1401 Viewing a picture as a 2D image 1

Description:

Row C seat 20. Nested Loop ... First row: x=0 and y=0, x=1 and y=0, x=2 and y=0, ... Loop through all the rows (y starts at 0, increments by 1, and is less ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 18
Provided by: Wanda6
Category:

less

Transcript and Presenter's Notes

Title: CSC1401 Viewing a picture as a 2D image 1


1
CSC1401Viewing a picture as a 2D image - 1
2
Review from the last week
  • We used a getPixels() to get all of the pixels of
    a picture
  • But this has been somewhat misleading
  • The pixels are in a 2-dimensional array
  • Getting the pixels has been placing them into a
    1-dimensional list

3
What is a two-dimensional array?
  • The pixels in a picture are really stored in a
    two-dimensional array
  • Each pixel has a x value (horizontal location)
  • Each pixel has a y value (vertical location)
  • pictureObj.getPixel(x,y) returns the pixel at
    that location

x
y
4
Example Two-Dimensional Arrays
  • Maps
  • That street is in D-5
  • Battleship
  • Try I-5
  • Hit or miss
  • Chairs at a theater or game
  • Row C seat 20

5
Nested Loop
  • How would you get all the pixels in a picture
    using their x and y values
  • First row x0 and y0, x1 and y0, x2 and y0,
  • Second row x0 and y1, x1 and y1, x2 and
    y1,
  • Third row x0 and y2, x1 and y2, x2 and y2,
  • We need to have one loop inside another
  • The outer loop counts y from 0 to height - 1
  • The inner loop counts x from 0 to width 1
  • We could also nest the loops the other way around

6
Recall Alice
7
Nested Loop Template
  • // loop through the rows
  • for (int y 0 y lt this.getHeight() y)
  • // loop through the columns
  • for (int x 0 x lt this.getWidth() x)
  • // get the current pixel
  • pixelObj this.getPixel(x,y)
  • // do something to the color
  • // set the new color
  • pixelObj.setColor(colorObj)

8
Mirroring
  • What if we want to pretend to place a mirror in
    the middle of the picture
  • We would see the left side of the picture
    mirrored on the right side

9
Mirroring
0
1
2
3
4
0
  • If we just think of a number at each x and y
    location instead of a color
  • The mirroring would look like this
  • Can you find the algorithm to do this?

1
2
10
What is the Mirror for this?
0
1
2
  • Try the solve the problem for small samples
  • If you cant solve it on a small sample
  • You cant write a program to solve it

0
1
2
0
1
2
0
1
2
11
Mirror Algorithm
  • Loop through all the rows (y starts at 0,
    increments by 1, and is less than the picture
    height)
  • Loop with x starting at 0 and x less than the
    midpoint (mirror point) value
  • Get the left pixel at x and y
  • Get the right pixel at width 1 - x
  • Set the color for the right pixel to be the color
    of the left pixel

12
Mirror Algorithm to Code
  • We are going to need the midpoint
  • int midpoint this.getWidth() / 2
  • Loop through the rows (y values)
  • for (int y 0 y lt this.getHeight() y)
  • Loop through x values (starting at 1)
  • for (int x 0 x lt 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())

13
Mirror Method
  • public void mirrorVertical()
  • int mirrorPoint this.getWidth() / 2
  • Pixel leftPixel null
  • Pixel rightPixel null
  • // loop through the rows
  • for (int y 0 y lt this.getHeight() y)
  • // loop from 0 to just before the mirror
    point
  • for (int x 0 x lt mirrorPoint x)

14
Mirror Method - Continued
  • leftPixel this.getPixel(x, y)
  • rightPixel this.getPixel(this.getWidth()
    1 x, y)
  • rightPixel.setColor(leftPixel.getColor())

15
Mirroring in the other direction
  • What about mirroring around a mirror held
    horizontally in the vertical center of the
    picture?
  • Like a reflection in a lake?

16
Mirror Horizontal Exercise
  • Write the method to mirror the top half of the
    picture to the bottom half.
  • This is a motorcycle redMotorcycle.jpg
  • How about mirroring bottom to top?

17
Summary
  • A two-dimensional array has columns and rows
  • Use nested loops to work with 2-d arrays
  • One loop for the x direction and one for the y
  • for (int x 0 x lt this.getWidth() x)
  • // loop through the rows (y direction)
  • for (int y 0 y lt this.getHeight() y)

18
Assignment
  • Review Media Computation Chapter 5, Section 1
Write a Comment
User Comments (0)
About PowerShow.com