Sei sulla pagina 1di 57

INFT1004 Visual Programming

Lecture 4 Selection (Guzdial & Ericson chapter 5)

INFT1004 - SEMESTER 1 - 2012


Week 1 Week 2 Week 3 Week 4 Recess Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13 Mar 4 Mar 11 Mar 18 Mar 25 Apr 1 - Apr 7 Apr 8 Apr 15 Apr 22 Apr 29 May 6 May 13 May 20 May 27 Jun 3 Introduction Programs, Arrays and Iteration Working with x and y coordinates Selection

LECTURE TOPICS

Mid Semester Recess Period More Picture Techniques Sound and Arrays More Sound and Arrays Program Design and Strings Lists, Files and Modules Web, Representations, Steganography Turtles and Other Classes Revision and Look Ahead No formal classes - MUST be available normal & supplementary period
Assignment due 3:00 Tuesday May 21 Practical Test 2 in Lab class Practical Test 1 in Lab class

Mid Year Examination Period

Lecture Topics and Lab topics are the same for each week

INFT1004 - SEMESTER 1 - 2012


Week 1 Week 2 Week 3 Week 4 Recess Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13 Mar 4 Introduction

LECTURE TOPICS

Programs, Arrays and Iteration Mar 11 This week in the lab there will be a Working with x andcan y coordinates Mar 18 practical practice so you practice for Selection Mar5s 25 exam. week Apr 1 - Apr 7 Mid Semester Recess Period
Practical Test 1 in Lab class

ButApr dont forget you still need to do the tut 8 More Picture Techniques material week Apr 15 for this Sound and Arrays
Apr 22 Apr 29 May 6 May 13 May 20 May 27 Jun 3 More Sound and Arrays Program Design and Strings Lists, Files and Modules Web, Representations, Steganography Turtles and Other Classes Revision and Look Ahead No formal classes

Practical Test 2 in Lab class Assignment due 3:00 Tuesday May 21

Mid Year Examination Period

- MUST be available normal & supplementary period

Lecture Topics and Lab topics are the same for each week

Revision Simple & Object Types


With simple types (eg int, float, string) Assignment gives the value of the expression on the right to the variable on the left
number = 8 sum = 10 + number

Revision Simple & Object Types


With object types Assignment makes the variable on the left a name (or another name) for the object on the right
myFile = newFile

myFile newFile
6

Revision - Functions
Programs are made up of one of more functions If we want a function to take arguments, we include corresponding parameters when defining it Whenever we find the same piece of code appearing several times in a program, we should extract it, define it as a function, and replace the multiple occurrences with multiple calls to the function If there are variations between the occurrences, we use parameters and arguments to deal with the variations
7

Revision - sequence, iteration


The statements in a function or a loop will be executed in the order theyre written When working out what the code does, be sure to note the effect of each statement before going on to consider the next

sequence

Revision - sequence, iteration


Within a sequence of statements, a loop iterates (repeats) the statements in its body, as many times as specified

sequence

loop

Revision - Comments
Your programs must include three kinds of comments:
1.A comment at the start of every program (collection of functions), saying who wrote it, and when, and why 2.A comment at the start of every function, explaining briefly what it does 3.A comment with every bit of code that another programmer might find easier to understand if its explained

Do not write comments to explain what will be obvious to a reasonable programmer!


10

Sequence, selection, iteration


Programming has three essential building blocks Weve already met sequence and iteration Selection determines which code to execute depending on specified conditions
?

selection loop

sequence

11

Sequence, selection, iteration


For example, we might want to change the colour of some pixels: not all the pixels in a picture not all the pixels in a predefined block but all the pixels satisfying a particular condition, such as all the red pixels, for red-eye reduction

12

The if statement
Selection is done with the if statement. At its simplest, the statement takes the form if <condition> <body>
?

selection

13

The if statement
Selection is done with the if statement. At its simplest, the statement takes the form if <condition> <body>
?

selection

where <condition> means some condition to be tested and <body> is one or more statements suitably indented

14

The if statement
If the condition is true when tested, the body will be executed; otherwise, the body will be ignored if <condition> <body>
?

selection loop

Either way, any following statements are then executed


15

Conditions
A condition is something that, when evaluated, gives a value of true or false if <condition> <body> Technically, it is a boolean expression, which simply means an expression whose value is true or false boolean is another simple type to add to int, float, and string

16

Conditions
Unfortunately, Python implements booleans as integers, which can lead to some confusion: The int 0 is regarded as the boolean false any other int is regarded as the boolean true false true 0 any other int (e.g. 1,6,10)

17

Boolean expressions
We can make simple boolean expressions by comparing numbers with relational operators

Expression 3>5 3<5 3 == 5 3 <> 5 3 >= 5 3 <= 5

Value false true false true false true

18

Boolean expressions
We can make simple boolean expressions by comparing numbers with relational operators Note that the check for equality uses a double = sign - because the single one is reserved for assignment

Expression 3>5 3<5 3 == 5 3 <> 5 3 >= 5 3 <= 5

Value false true true false false true

19

Boolean expressions
We can make simple boolean expressions by comparing numbers with relational operators Note that the check for equality uses a double = sign - because the single one is reserved for assignment With the other 2-character operators, the order of the characters is important

Expression 3>5 3<5 3 == 5 3 <> 5 3 >= 5 3 <= 5

Value false true true false false true

20

Distance Between Colours


distance(colour1, colour2)

This is a function that measures how close together two colours are It goes up to about 450 Experiment with different colours to see what sorts of values it produces

21

Distance Between Colours


Lets enhance the green of colours close to white (we will need to work out the distance between a pixel colour and the colour white)

22

Distance Between Colours


Lets enhance the green of colours close to white (we will need to work out the distance between a pixel colour and the colour white)

For all the pixels in the picture, if the colour of that pixel is close to white enhance the green of a pixel

23

Irradiating a swan
This method works well with swan.jpg def irradiate(picture): # Take pixels that are near enough to # white and make them very green for px in getPixels(picture): colour = getColor(px) if distance(colour, white) < 270: # Found distance by trial & error setBlue(px, getBlue(px) / 2) setRed(px, getRed(px) / 2) setGreen(px, 190) repaint(picture)
24

Irradiating a swan

25

Irradiating a swan

For all the pixels in the picture, if the colour of that pixel is close to white enhance the green of a pixel

26

Not as easy as it looks


This approach is of limited value With most pictures, more (or fewer) pixels than you would expect are within a given distance of a given colour It works best where the colours in a picture are quite distinct While it does show a new way of processing a picture, the real point is to show you how the if statement can specify that a sequence of statements should be carried out only when some particular condition applies
27

Red-eye reduction
To reduce the red that a camera flash puts in the eyes, without reducing the red in the rest of the picture, we need to first note the range of pixels in which the eyes are located.

28

Red-eye reduction
To reduce the red that a camera flash puts in the eyes, without reducing the red in the rest of the picture, we need to first note the range of pixels in which the eyes are located. Instead of reducing the red in the whole picture - we reduce it just in the range with the eyes in it

29

Red-eye reduction
for x in range(startX, endX): for y in range(startY, endY): pix = getPixel(pic, x, y) if distance(red, getColor(pix) < 165): setColor(pix, replacementColour)
startX endX

startY endY

30

Red-eye reduction
for x in range(startX, endX): for y in range(startY, endY): pix = getPixel(pic, x, y) if distance(red, getColor(pix) < 165): setColor(pix, replacementColour)
startX endX usually a blackish colour startY endY

31

if with else
Weve seen the simplest form of the if statement if <condition>: true <body>
?

32

if with else
What if we want to do something when the condition is true and something different if the condition is false? For example, we want to turn the white pixels green and the other pixels purple?

if <condition>: <statements> else: <alternative statements>

?
true false

33

if with else
What if we want to do something when the condition is true and something different if the condition is false? For example, we want to turn the white pixels green and the other pixels purple?

if <condition>: <statements> else: <alternative statements>

?
true false

34

if with else
When we follow an if and its body with an else and its body.. the body of the if will be done if the condition is true, and the body of the else will be done if the condition is false

if distance(colour, white) < 270: <make the pixel more green> else: <make the pixel more purple>

?
true false

35

if with else
Guzdial & Ericson prefer to achieve the same effect with two separate ifs if distance(colour, white) < 270: <make the pixel more green> if distance(colour, white >= 270: <make the pixel more purple>
?
true

?
true

36

if with else
if distance(colour, white) < 270: <make the pixel more green> else # distance(colour, white >= 270: <make the pixel more purple> But most programmers prefer the proper use of else

?
true false

37

The oddly named elif


A common use of selection is to test for several mutually exclusive conditions if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 elif is short for else if; be sure that you understand the else bit
38

The oddly named elif


A common use of selection is to test for several mutually exclusive conditions if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223
Note that only the first condition to be true will be executed. If none of the conditions are true then the else part will be executed. (There doesnt need to be an else part but usually there is)

elif is short for else if; be sure that you understand the else bit
39

Posterising
A posterised picture is one that uses a highly reduced set of colours (suitable for poster printing)

40

Posterising
if-elif-else statements form the basis of a function that can be used to posterise a picture It divides the possible values into four ranges, then replaces every value with the midpoint of that range

0-63 31

64-127 95

128-191 159

192-255 223
41

Posterising
The posterise function is typically used for each colour channel (red, blue, green)
def posterise(picfile): # Posterise a picture by replacing each channel of every pixel # with the middle of the range of values it lies in picture = makePicture(picfile) for px in getPixels(picture): setRed(px, midRange(getRed(px))) setGreen(px, midRange(getGreen(px))) setBlue(px, midRange(getBlue(px))) repaint(picture)

42

Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value

0-63 31

64-127 95

128-191 159

192-255 223

43

Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value

0-63 31

64-127 95

128-191 159

192-255 223

44

Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value

0-63 31

64-127 95

128-191 159

192-255 223

45

Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value

0-63 31

64-127 95

128-191 159

192-255 223

46

Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value

0-63 31

64-127 95

128-191 159

192-255 223

47

Posterising
Our approach (using elif) is basically the same as the version in the textbook, but our program is a lot smaller and neater Lecture4demo.py shows this posterise function and the one from the book

48

The program in the book


The one using elif only needs to test the upper end of each range; the one in the book has to test both ends of each range
if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 if redVal < 64: setRed(p, 31) if redVal > 63 and redVal < 128: setRed(p, 95) if redVal > 127 and redVal < 192: setRed(p, 159) if redVal > 191 and redVal < 256: setRed(p, 223)

Be sure you understand why

49

The program in the book


See how the book function repeats the same code (with variations) three times. This should clearly be rewritten as a function, which should be called three times.

50

The program in the book


And the book function uses the names red, green, and blue as variables, which is not good (there are already defined constants called red, green and blue)

51

Return or not return


In the lecture demo program, some pictureprocessing functions simply repaint the picture, while others repaint and return it; why the difference?

repaint(picture)

repaint(picture) return(picture)

52

Return or not return


If all we want to do is see the resulting picture, repaint is fine If we might want to do something else, such as explore it or save it, return is better, because we can then assign it to a variable and do these things. With big pictures on a small screen, repaint makes them too big to see properly with no scroll bars! This is one reason we might want a different approach.

53

Combining conditions
Sometimes a simple condition isnt enough We might want to combine multiple conditions in various ways We can do this with the logical operators and, or, and not

54

Combining conditions
and, or, and not mean pretty much what they mean in English if age > 16 and < 25

55

Combining conditions
and, or, and not mean pretty much what they mean in English if age > 16 and < 25

An important difference is that they must join complete conditions, not partial ones if age > 16 and age < 25

56

Edge detection
We can turn a colour picture into a black-and-white line drawing if we can find edges and make them black An edge is where theres a sharp difference in luminance (brightness) between neighbouring pixels

57

Edge detection
Carefully read and understand the edge detection program in the book Carefully read and understand the edge detection program in lecture4demo.py Try to understand the differences; for example, theres no need to have two copies of the picture, as in the book And the proper use of else means we dont need to use not
58

Potrebbero piacerti anche