Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with...

32
Chapter 3 Modifying pictures with loops We are now ready to work with the pictures. From a programming perspective. So far, the only structure we have done is sequential. For example, the following function def pickAndShow(): myFile=pickAFile() myPict=makePicture(myFile) show(myPict) is executed as a one-pass sequence. Some- times, we need more general and flexible struc- tures, such as conditional and loop. Let’s have a look. 1

Transcript of Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with...

Page 1: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Chapter 3Modifying pictures with loops

We are now ready to work with the pictures.

From a programming perspective. So far, the

only structure we have done is sequential. For

example, the following function

def pickAndShow():

myFile=pickAFile()

myPict=makePicture(myFile)

show(myPict)

is executed as a one-pass sequence. Some-

times, we need more general and flexible struc-

tures, such as conditional and loop.

Let’s have a look.

1

Page 2: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Control Structures

There are three basic structures in any high-

level programs: sequential, conditional and repet-

itive.

Below is an example of a sequential structure.

Get up;

Having breakfast;

Having a class;

Having the next class;

Having lunch;

Having another class;

Having one more class;

Having supper;

Watching TV;

Go to bed;

2

Page 3: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Conditional operation

The conditional operation lets the algorithm

to ask a question, and, based on the answer,

selects the next operation to perform. Below

is the most commonly used structure.

If "a true-false" condition is true Then

first set of operations

Else second set of operations.

It evaluates the condition first to see if it is

true or false, and then executes the first, or

the second set, of operations, accordingly. In

either case, continues with the next operation.

3

Page 4: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

An example

1. Get values for gallons, start and end

2. Set the value of distance to (end − start)

3. Set the value of average to (distance/gallons)

4. Print the value of average

5. If average > 25.0 Then

6. Print the massage “You are getting good

gas mileage.”

7. Else Print the massage “You are not getting

a good gas mileage.”

8. Stop

4

Page 5: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Iterative operation

An iterative operation repeats a block of op-

erations, i.e.,

Repeat step i to step j until a "condition"

becomes true

step i: operation

step i+1: operation

...

step j: operation

It performs all operations from step i to step

j, inclusive, until the condition becomes true.

The block from step i to step j is called the

loop body, and the condition is called a termi-

nation condition, which is used to control the

loop.

5

Page 6: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

An example

1. Repeat step 2 to step 10 until response is

no

2. Get values for gallons, start and end

3. Set the value of distance to (end − start)

4. Set the value of average to (distance/gallons)

5. Print the value of average

6. If average > 25.0 Then

7. Print the massage “You are getting good

gas mileage.”

8. Else Print the massage “You are not get-

ting a good gas mileage.”

9. Print the massage “Do you want to re-

peat?”

10. Get a value for response from the user

11. Stop

6

Page 7: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

More iterative structure

The following prints out 1 through 10:

int count = 1;

while (count <= 10){

cout << count;

count = count + 1;

}

It can also be done with the following:

for (count=1; count <= 10; count++){

cout << count;

}

7

Page 8: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Image representation

Before discussing media processing, we have

to understand better how a picture is repre-

sented. JPEG is one way to represent an im-

age, which is an international standard to store

images with high quality but less space. It is

a lossy representation in the sense that it does

not store 100% of the original information by

throwing away some of the unnecessary infor-

mation. For all the practical purposes, a JPEG

file works just fine.

In a bit more details, a JPEG file is kept in a

2 dimensional matrix, which is a sequence of

elements each with an associated index num-

ber. When we use such a matrix to represent

a picture, each element is called a pixel, i.e.,

an element of picture, which is identified with

a coordinate, such as (231,125).

8

Page 9: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

An example

Below shows the cursor, and the close-up viewof the cursor and that of the line below thecursor.

The following Python expression identifies thepixel (10,100) of a picture pict:

getPixl(pict, 10, 100)

We can also use the built-in Picture Tool toidentify the coordinate of any pixel within achosen picture.

9

Page 10: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

How could this happen?

It turns out that our sensory apparatus can’t

distinguish small bits out of a whole. We sim-

ply can’t see much details as well as an eagle.

We also have a different system for processing

a color image from that for processing a black-

and-white image (luminance). We use the lat-

ter to detect motion and size of an object.

This lack of resolution is what makes picture

digitization possible: We simply break up (digitize)

an image to a whole bunch of pixels, and can’t

really tell the difference between the two, when

there are enough of them.

10

Page 11: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

How about the color?

When we use a matrix to represent a color

picture, each pixel is also associated with a

color, which is a combination of three basic

ones, red, green, and blue, according to the

RGB Color model.

We usually use one byte to represent each color.

Thus, using three bytes for three colors, we

can come up with 224, about 16 million differ-

ent colors.

11

Page 12: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Other models

CMYB is another model for color in which

any color is decomposed as a combination of

four basics ones: Cyan, Magenta, Yellow, and

Black. It is often used in the printers.

Yet another one is the HSV, in which a color is

represented as a combination of three ingredi-

ents: Hue (type of the color), Saturation (gray

to dark) and Value (brightness):

12

Page 13: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Different colors

For example, (0,0,0) represents black; (255,255,

255) represents white; (50,50,50) represents

dark gray; and (100,100,100) a bit lighter.

Below shows a matrix of 8 pixels, with different

colors and their codes.

13

Page 14: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

How big is it?

Once we have identified a picture object (How?),

we can find out its size, i.e., its width and

height in terms of the number of pixels, with

the getWidth and getHeight commands as fol-

lows:

>>>print getWidth(pict)

>>>print getHeight(pict)

In general, given a picture pict, getWidth(pict)

finds out the number of columns and getHeight

finds out the number of rows.

Let’s check it out with, e.g., arch.jpg.

14

Page 15: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Get and set the color

We manipulate a picture in Python by mak-ing a picture object out of a JPEG file, thenchanging the properties, mainly the colors, ofthe pixels of that picture.

To get the color of a pixel of a picture object,pict, we use the following expression:

>>>pixel1=getPixel(pict, 1, 100)

>>>print getColor(pixel1)

To get just the green component, we say

>>>print getGreen(getPixel(pict, 1, 100))

To set the color of the above pixel to yellow,we can do the following:

>>>setColor(pixel1, yellow)

Question: Did we do it?

15

Page 16: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Another example

Let’s see what does the following sequence do,

with the Picture Tool. Assume that we have

picked an image file to work with, we might

have the following session:

>>>pixel2=getPixel(pict, 1, 1)

>>>print pixel2

Pixel, color=color r=214 g=165 b=132

>>>print getX(pixel2)

1

>>>print getY(pixel2)

1

>>>print getRed(pixel2)

214

16

Page 17: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

>>>color=getColor(pixel2)

>>>print color

color r=214 g=165 b=132

>>>newColor=makeColor(0, 100, 0)

>>>print newColor

color r=0 g=100 b=0

>>>setColor(pixel2, newColor)

>>>print getColor(pixel2)

color r=0 g=100 b=0

>>>show(pict)

17

Page 18: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

A bit more yellow

If we want to set the colors of five adjacent

pixels to yellow, we could do the following:

def ABitYellow():

file=pickAFile()

pict=makePicture(file)

show(pict)

setColor(getPixel(pict, 11, 100), yellow)

setColor(getPixel(pict, 11, 101), yellow)

setColor(getPixel(pict, 11, 102), yellow)

setColor(getPixel(pict, 11, 103), yellow)

setColor(getPixel(pict, 11, 104), yellow)

repaint(pict)

show(pict)

It is clear that the above is just a sequential

structure.

Let’s check out if it works.18

Page 19: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

How to work on the whole thing?

We often want to process the whole picture,

i.e., all the pixels. It will be pretty boring,

even impossible, to process one pixel at a time,

for 10,000 pixels. Now, the iterative structure

kicks right in, which takes over the boring part.

For example, we can use the following program

to make a picture less red.

def decreaseRed(picture):

for pixel in getPixels(picture):

value = getRed(pixel)

setRed(pixel, value*0.5)

The above code, for all the pixels, one by one,

gets the red component of its color, cuts it by

half, then sends it back.

Homework: 3.3, 3.4.

19

Page 20: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

A bit more details

The key instruction in the above program isthe following:

for pixel in getPixels(picture):

value = getRed(pixel)

setRed(pixel, value*0.5)

In the above the first line

for pixel in getPixels(picture):

specifies what data to process within the loop.Here we want to process all the pixels we mayget out of the picture object picture. Thevariable name pixel refers to a value returnedby the function getPixel, which is to get eachand every pixel from the picture.

Although we can use any name, such as p, x,

y, etc to refer to a pixel, pixel looks prettynatural.

20

Page 21: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

The loop structure

Moreover, the word for in this first line indi-

cates a loop.

In fact, all the pixels are lined up in a sequence,

all the pixels in the first line, followed by all in

the second line, ..., followed by all the pixels in

the last line. With the for loop, all the pixels

will be taken in in this order: the loop begins

with the first one, do something with it, then

moves to the next pixel, do exactly the same

thing, ..., until it gets the last pixel, do the

same thing, and quits.

Question: How does the computer know what

to do with a pixel when it is taken in?

Answer: The next part takes care of it.

21

Page 22: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

The loop body

The next segment is the loop body.

value = getRed(pixel)

setRed(pixel, value*0.5)

This part tells what to do in each iteration of

this loop. For this specific example, it states

that, for each value of pixel, or for each pixel,

we want to get out its red component, chop it

off by 50%, then set this value back.

This is equivalent to say we decrease the red

component of each pixel by half.

Let’s see what it does.

22

Page 23: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

How does it work?

Below is the original picture of a fish pond:

Now the changed picture

Homework: 3.2, 3.5.

23

Page 24: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Take out the blue

We can also clear away the blue component ofall the pixels, by setting the blue componentof all the pictures to 0, as follows:

def clearBlue(picture):

for p in getPixels(picture):

setBlue(p, 0)

Question: What will the picture look like?

Answer: Let’s see.

Homework: 3.7 and 3.8.24

Page 25: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Add something on

It is pretty easy to use Python to do someimage doctoring. For example, we might wantto add a sunset effect to a beach picture, usingthe following code.

def makeSunset(picture):

for p in getPixels(picture):

value=getBlue(p)

setBlue(p, value*0.7)

value=getGreen(p)

setGreen(p, value*0.7)

The idea is to take off 30% of the blue andgreen to let the red stand out.

Let’s see what it does to a picture.

Question: What happens if we simply add onmore red, rather than taking off both greenand blue, say, by 30%?

Homework: 3.6.25

Page 26: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Add a bit sunshine

Below is the original picture of a beach:

and the same one at sunset:

26

Page 27: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

An alternative way

An important concept in programming is called

modular programming, which means that one

function does one and only one thing. Thus,

we might solve the sunset problem as follows:

def makeSunset(picture):

reduceBlue(picture)

reduceGreen(picture)

def reduceBlue(picture):

for p in getPixels(picture):

value=getBlue(p)

setBlue(p, value*0.7)

def reduceGreen(picture):

for p in getPixels(picture):

value=getGreen(p)

setBlue(p, value*0.7)

27

Page 28: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

A couple of notes

1. We call such an idea hierarchical decompo-

sition, or divide and conquer. This is a pretty

useful concept. To solve something big and/or

complicated, we cut it into a bunch of smaller

and/or easier ones, solve them one by one, and

then combine the solution.

2. Although the same name p occurs in all the

three functions, they have nothing to do with

each other. They are local in each function.

3. The three functions constitute one pro-

gram, thus, they should be entered in the same

file, and get loaded together.

28

Page 29: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Python also provides two functions, makeLighter

and makeDarker that lighten and darken the

color of a pixel, respectively. Below shows a

program that makes use of the makeLighter

feature.

def lighten(picture):

for px in getPixels(picture):

color=getColor(px)

makeLighter(color)

setColor(px, color)

29

Page 30: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Create a negative image

It is pretty easy to write a program to get the

negative image of a picture object, as follows.

It simply get the “negative value” of the color

of each and every pixel of the involved picture.

def negative(picture):

for px in getPixels(picture):

red=getRed(px)

green=getGreen(px)

blue=getBlue(px)

negColor=makeColor(255-red,255-green,255-blue)

setColor(px, negColor)

30

Page 31: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

Convert to grayscale

Nowadays, when we take a picture, we of-ten use a color camera. But, sometimes wereally miss the old-fashioned black-and-whitepicture. It turns out pretty easy to do such aconversion if you have digitized that picture.

What we could do is to average the three colorcomponents for all the pixels as follows:

def grayScale1(picture):

for px in getPixels(picture):

in=(getRed(px)+getGreen(px)+getBlue(px))/3

setColor(px, makeColor(in, in, in))

It does work, but it turns out that we could dobetter, if we take into consideration that weperceives blue to be darker than red, even ifthey share the same amount. Thus, we shouldgive blue less weight, and more to red, andeven more to green, when calculating the av-erage.

31

Page 32: Chapter 3 Modifying pictures with loopszshen/Webfiles/notes/CS130/Note3.pdfModifying pictures with loops We are now ready to work with the pictures. From a programming perspective.

A better way

We thus come to the following revised program

for the gray scale conversion.

def grayScale(picture):

for px in getPixels(picture):

newRed=getRed(px)*0.299

newGreen=getGreen(px)*0.587

newBlue=getBlue(px)*0.114

luminance = newRed+newGreen+newBlue

setColor(px, makeColor

(luminance, luminance, luminance))

Homework: 3.9 and 3.10.

32