Inft1004 Lec4 Selection

  • Upload
    mrzaggy

  • View
    232

  • Download
    0

Embed Size (px)

Citation preview

  • 7/27/2019 Inft1004 Lec4 Selection

    1/57

    INFT1004Visual Programming

    Lecture 4

    Selection

    (Guzdial & Ericson chapter 5)

  • 7/27/2019 Inft1004 Lec4 Selection

    2/57

    3

    INFT1004 - SEMESTER 1 - 2012

    Week 1

    Week 2

    Week 3

    Week 4

    Week 10

    Week 11

    Week 9

    Week 12

    Mar 4

    Mar 11

    Mar 18

    Mar 25

    May 6

    May 13

    May 20

    May 27

    Assignment due3:00 Tuesday May 21

    Programs, Arrays and Iteration

    Working with x and y coordinates

    Selection

    Web, Representations, Steganography

    Lists, Files and Modules

    Turtles and Other Classes

    Introduction

    Week 13 Jun 3

    Revision and Look Ahead

    No formal classes

    Mid Year Examination Period - MUST be available normal & supplementary period

    Recess Apr 1 - Apr 7 Mid Semester Recess Period

    Practical Test 2in Lab class

    Week 8 Apr 29

    More Sound and ArraysWeek 7 Apr 22

    Program Design and Strings

    Lecture Topics and Lab topics are the same for each week

    LECTURE TOPICS

    Week 5 Apr 8 Practical Test 1in Lab class

    More Picture Techniques

    Week 6 Apr 15 Sound and Arrays

  • 7/27/2019 Inft1004 Lec4 Selection

    3/57

    4

    INFT1004 - SEMESTER 1 - 2012

    Week 1

    Week 2

    Week 3

    Week 4

    Week 10

    Week 11

    Week 9

    Week 12

    Mar 4

    Mar 11

    Mar 18

    Mar 25

    May 6

    May 13

    May 20

    May 27

    Assignment due3:00 Tuesday May 21

    Programs, Arrays and Iteration

    Working with x and y coordinates

    Selection

    Web, Representations, Steganography

    Lists, Files and Modules

    Turtles and Other Classes

    Introduction

    Week 13 Jun 3

    Revision and Look Ahead

    No formal classes

    Mid Year Examination Period - MUST be available normal & supplementary period

    Recess Apr 1 - Apr 7 Mid Semester Recess Period

    Practical Test 2in Lab class

    Week 8 Apr 29

    More Sound and ArraysWeek 7 Apr 22

    Program Design and Strings

    Lecture Topics and Lab topics are the same for each week

    LECTURE TOPICS

    Week 5 Apr 8 Practical Test 1in Lab class

    More Picture Techniques

    Week 6 Apr 15 Sound and Arrays

    This week in the lab there will be a

    practice practical so you can practice forweek 5s exam.

    But dont forget you still need to do the tut

    material for this week

  • 7/27/2019 Inft1004 Lec4 Selection

    4/57

    5

    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

  • 7/27/2019 Inft1004 Lec4 Selection

    5/57

    6

    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

    newFile

    myFile

  • 7/27/2019 Inft1004 Lec4 Selection

    6/57

    7

    Revision - Functions

    Programs are made up of one of more functions

    If we want a function to take arguments, we includecorrespondingparameters when defining it

    Whenever we find the same piece of code appearingseveral times in a program, we should extract it, define it as

    a function, and replace the multiple occurrences withmultiple calls to the function

    If there are variations between the occurrences, we useparameters and arguments to deal with the variations

  • 7/27/2019 Inft1004 Lec4 Selection

    7/57

    8

    Revision - sequence, iteration

    The statements in a function or a loop will beexecuted in the order theyre written

    When working out what the code does, be sure tonote the effect of each statement before going on

    to consider the next

    sequence

  • 7/27/2019 Inft1004 Lec4 Selection

    8/57

    9

    Revision - sequence, iteration

    Within a sequence of statements, a loop iterates

    (repeats) the statements in its body, as manytimes as specified

    sequence loop

  • 7/27/2019 Inft1004 Lec4 Selection

    9/57

    10

    Revision - Comments

    Your programs must include three kinds of comments:

    1.A comment at the start of every program (collection offunctions), saying who wrote it, and when, and why

    2.A comment at the start of every function, explaining brieflywhat it does3.A comment with every bit of code that another programmermight find easier to understand if its explained

    Do not write comments to explain what will be

    obvious to a reasonable programmer!

  • 7/27/2019 Inft1004 Lec4 Selection

    10/57

    11

    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

    sequence loop

    selection?

  • 7/27/2019 Inft1004 Lec4 Selection

    11/57

    12

    Sequence, selection, iteration

    For example, we might want to change the colourof 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

  • 7/27/2019 Inft1004 Lec4 Selection

    12/57

    13

    The ifstatement

    Selection is done with the if statement. At itssimplest, the statement takes the form

    if

    selection?

  • 7/27/2019 Inft1004 Lec4 Selection

    13/57

    14

    The ifstatement

    Selection is done with the if statement. At itssimplest, the statement takes the form

    where means some condition to be tested

    and is one or more statements suitably indented

    if

    selection?

  • 7/27/2019 Inft1004 Lec4 Selection

    14/57

    15

    The ifstatement

    if

    selection?

    Either way, any

    following statementsare then executed

    If the condition is true when tested, the body will beexecuted; otherwise, the body will be ignored

    loop

  • 7/27/2019 Inft1004 Lec4 Selection

    15/57

    16

    ConditionsA condition is something that, when evaluated,

    gives a value of true or false

    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

    if

  • 7/27/2019 Inft1004 Lec4 Selection

    16/57

    17

    Conditions

    Unfortunately, Python implements booleans asintegers, which can lead to some confusion:

    The int 0 is regarded as the boolean falseany other int is regarded as the boolean true

    false 0

    true any other int (e.g. 1,6,10)

  • 7/27/2019 Inft1004 Lec4 Selection

    17/57

    18

    Boolean expressions

    Expression Value

    3 > 5 false

    3 < 5 true

    3 == 5 false

    3 5 true

    3 >= 5 false

    3

  • 7/27/2019 Inft1004 Lec4 Selection

    18/57

    19

    Boolean expressions

    Expression Value

    3 > 5 false

    3 < 5 true

    3 == 5 true

    3 5 false

    3 >= 5 false

    3

  • 7/27/2019 Inft1004 Lec4 Selection

    19/57

    20

    Boolean expressions

    Note that the check for equality

    uses a double = sign - becausethe single one is reserved forassignment

    Expression Value

    3 > 5 false

    3 < 5 true

    3 == 5 true

    3 5 false

    3 >= 5 false

    3

  • 7/27/2019 Inft1004 Lec4 Selection

    20/57

    21

    Distance Between Colours

    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

    distance(colour1, colour2)

  • 7/27/2019 Inft1004 Lec4 Selection

    21/57

    22

    Distance Between Colours

    Lets enhance the green of colours close to white

    (we will need to work out the distance between a pixelcolour and the colour white)

  • 7/27/2019 Inft1004 Lec4 Selection

    22/57

    23

    Distance Between Colours

    Lets enhance the green of colours close to white

    (we will need to work out the distance between a pixelcolour 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

    ?

  • 7/27/2019 Inft1004 Lec4 Selection

    23/57

    24

    Irradiating a swan

    This method works well with swan.jpg

    24

    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)

  • 7/27/2019 Inft1004 Lec4 Selection

    24/57

    25

    Irradiating a swan

    25

  • 7/27/2019 Inft1004 Lec4 Selection

    25/57

    26

    Irradiating a swan

    26

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

    ?

  • 7/27/2019 Inft1004 Lec4 Selection

    26/57

    27

    Not as easy as it looks

    This approach is of limited value

    With most pictures, more (or fewer) pixels than youwould 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 canspecify that a sequence of statements should be carried

    out only when some particular condition applies

  • 7/27/2019 Inft1004 Lec4 Selection

    27/57

    28

    Red-eye reductionTo reduce the red that a camera flash puts in the eyes,

    without reducing the red in the rest of the picture, we need tofirst note the range of pixels in which the eyes are located.

  • 7/27/2019 Inft1004 Lec4 Selection

    28/57

    29

    Red-eye reduction

    Instead of

    reducing the redin the wholepicture - we

    reduce it just in

    the range with

    the eyes in it

    To reduce the red that a camera flash puts in the eyes,

    without reducing the red in the rest of the picture, we need tofirst note the range of pixels in which the eyes are located.

  • 7/27/2019 Inft1004 Lec4 Selection

    29/57

    30

    Red-eye reductionfor 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)

    endXstartX

    startY

    endY

  • 7/27/2019 Inft1004 Lec4 Selection

    30/57

    31

    Red-eye reductionfor 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)

    endXstartX

    startY

    endY

    usually a

    blackish

    colour

  • 7/27/2019 Inft1004 Lec4 Selection

    31/57

    32

    ifwith else

    Weve seen the simplest form of the ifstatementif :

    ?

    true

  • 7/27/2019 Inft1004 Lec4 Selection

    32/57

    33

    ifwith 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 :

    else:

    ?true

    false

  • 7/27/2019 Inft1004 Lec4 Selection

    33/57

    34

    ifwith 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 :

    else:

    ?true

    false

  • 7/27/2019 Inft1004 Lec4 Selection

    34/57

    35

    ifwith else

    When we follow an ifand its body with an else and its

    body..

    the body of the ifwill be done if the condition is true, and

    the body of the else will be done if the condition is false

    ?true

    false

    if distance(colour, white) < 270:

    else:

  • 7/27/2019 Inft1004 Lec4 Selection

    35/57

    36

    ifwith else

    Guzdial & Ericson prefer to achieve the same effect with

    two separate ifs

    if distance(colour, white) < 270:

    if distance(colour, white >= 270:

    ?

    true

    ?

    true

  • 7/27/2019 Inft1004 Lec4 Selection

    36/57

    37

    ifwith else

    if distance(colour, white) < 270:

    else # distance(colour, white >= 270:

    But most programmers prefer the proper use ofelse

    ?true

    false

  • 7/27/2019 Inft1004 Lec4 Selection

    37/57

    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 = 95elif value < 192:

    value = 159

    else:

    value = 223

    elifis short forelse if; be sure that you understand theelse bit

  • 7/27/2019 Inft1004 Lec4 Selection

    38/57

    39

    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 = 95elif value < 192:

    value = 159

    else:

    value = 223

    elifis short forelse if; be sure that you understand theelse bit

    Note that only the first

    condition to be true will be

    executed.

    If none of the conditions aretrue then the else part will be

    executed.

    (There doesnt need to be an

    else part but usually there is)

  • 7/27/2019 Inft1004 Lec4 Selection

    39/57

    40

    Posterising

    A posterised picture is one that uses a highly reduced set

    of colours (suitable for poster printing)

  • 7/27/2019 Inft1004 Lec4 Selection

    40/57

    41

    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

    2231599531

    64-127 128-191 192-255

  • 7/27/2019 Inft1004 Lec4 Selection

    41/57

    42

    Posterising

    The posterise function is typically used for each colourchannel (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 inpicture = makePicture(picfile)

    for px in getPixels(picture):

    setRed(px, midRange(getRed(px)))

    setGreen(px, midRange(getGreen(px)))

    setBlue(px, midRange(getBlue(px)))

    repaint(picture)

  • 7/27/2019 Inft1004 Lec4 Selection

    42/57

    43

    Posterisingdef 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

    2231599531

    64-127 128-191 192-255

  • 7/27/2019 Inft1004 Lec4 Selection

    43/57

    44

    Posterisingdef 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

    2231599531

    64-127 128-191 192-255

  • 7/27/2019 Inft1004 Lec4 Selection

    44/57

    45

    Posterisingdef 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

    2231599531

    64-127 128-191 192-255

  • 7/27/2019 Inft1004 Lec4 Selection

    45/57

    46

    Posterisingdef 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

    2231599531

    64-127 128-191 192-255

  • 7/27/2019 Inft1004 Lec4 Selection

    46/57

    47

    Posterisingdef 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

    2231599531

    64-127 128-191 192-255

  • 7/27/2019 Inft1004 Lec4 Selection

    47/57

    48

    Posterising

    Our approach (using elif) is basically the same as the

    version in the textbook, but our program is a lot smallerand neater

    Lecture4demo.py shows this posterise function and the

    one from the book

  • 7/27/2019 Inft1004 Lec4 Selection

    48/57

    49

    The program in the book

    The one using elifonly 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

  • 7/27/2019 Inft1004 Lec4 Selection

    49/57

    50

    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.

  • 7/27/2019 Inft1004 Lec4 Selection

    50/57

    51

    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)

  • 7/27/2019 Inft1004 Lec4 Selection

    51/57

    52

    Return or not return

    In the lecture demo program, some picture-processing functions simply repaint the picture,

    while others repaint and return it; why the

    difference?

    repaint(picture) repaint(picture)

    return(picture)

  • 7/27/2019 Inft1004 Lec4 Selection

    52/57

    53

    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.

  • 7/27/2019 Inft1004 Lec4 Selection

    53/57

    54

    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 operatorsand, or, and not

  • 7/27/2019 Inft1004 Lec4 Selection

    54/57

    55

    Combining conditions

    and

    ,or

    , andnot

    mean pretty much what they mean in English

    if age > 16 and < 25

  • 7/27/2019 Inft1004 Lec4 Selection

    55/57

    56

    Combining conditions

    and

    ,or

    , andnot

    mean pretty much what they mean in English

    if age > 16 and < 25

    An important difference is that they must joincomplete conditions, not partial ones

    if age > 16 and age < 25

  • 7/27/2019 Inft1004 Lec4 Selection

    56/57

    57

    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

  • 7/27/2019 Inft1004 Lec4 Selection

    57/57

    58

    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 ofelsemeans we dont need to

    use not