7/27/2019 Inft1004_Lec5_MorePictureTechniques
1/49
INFT1004Visual Programming
Lecture 5
More Image Manipulation
(Guzdial & Ericson chapter 5)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
2/49
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_Lec5_MorePictureTechniques
3/49
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
Week 5 Apr 8 Practical Test 1in Lab class
More Picture Techniques
Week 6 Apr 15 Sound and Arrays
You must attend the lab you are
registered in or you will not be
able to do the practical test
7/27/2019 Inft1004_Lec5_MorePictureTechniques
4/49
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_Lec5_MorePictureTechniques
5/49
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_Lec5_MorePictureTechniques
6/49
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_Lec5_MorePictureTechniques
7/49
8
Revision - sequence, selection
and 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 tonote the effect of each statement before going on
to consider the next
sequence
7/27/2019 Inft1004_Lec5_MorePictureTechniques
8/49
9
Within a sequence of statements, a loop iterates
(repeats) the statements in its body, as manytimes as specified
sequence loop
Revision - sequence, selection
and iteration
7/27/2019 Inft1004_Lec5_MorePictureTechniques
9/49
10
In a selection statement, the program chooses
which bits of code to execute depending on thevalue of a boolean condition
Revision - sequence, selection
and iteration
sequence loop
selection?
7/27/2019 Inft1004_Lec5_MorePictureTechniques
10/49
11
Selection is done with the ifstatement
The body of an ifstatementis executed if the
condition is true
If theres an else part, the body of the else is
executed if the condition is false
Revision forms of selection
7/27/2019 Inft1004_Lec5_MorePictureTechniques
11/49
12
If we want to test multiple options, we generally useelif, an abbreviation of else if
The first body of an elifthat has a true condition is
executed (all preceding conditions must be false)
After any body (if, elif, else) is executed, the rest ofthe ifsequence is skipped
Revision forms of selection
7/27/2019 Inft1004_Lec5_MorePictureTechniques
12/49
13
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_Lec5_MorePictureTechniques
13/49
14
Oversight makeEmptyPicture()
Earlier lectures overlooked something thats pretty
important in image manipulation
Weve always manipulated an image itself,
changing the original
Sometimes one might want to keep the original and
work with a copy
7/27/2019 Inft1004_Lec5_MorePictureTechniques
14/49
15
Oversight makeEmptyPicture()
Once you have the height and width of a picture,
makeEmptyPicture(width, height)
can give you a new picture of the required size
You can then copy (the colours of) the pixels from
the original to the copy, and then manipulate the
copy without altering the original
7/27/2019 Inft1004_Lec5_MorePictureTechniques
15/49
16
Oversight makeEmptyPicture()width
height
makeNewPicture(width, height)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
16/49
17
Oversight makeEmptyPicture()
So you can use this to:
1)create a new picture and2)
copy (the colours of) the pixels from the originalto the copy
3)manipulate the copy without altering the original
7/27/2019 Inft1004_Lec5_MorePictureTechniques
17/49
18
More Image Manipulation
Theres very little new programming in this lecture
Instead we revise the programming weve done by
seeing some more approaches to manipulating
images
Well see how to blend images, subtract
background, use chromakey, and draw and write on
images
7/27/2019 Inft1004_Lec5_MorePictureTechniques
18/49
19
Blending images
To get the effect of transparency, looking throughone somewhat transparent image at anotherimage, all we have to do is average the colours of
the corresponding pixels.
7/27/2019 Inft1004_Lec5_MorePictureTechniques
19/49
20
Blending images
To overlay picture 1 and picture 2, we change the pixelsof picture 1 to be the averages of the pixels of the two
pictures
If we do a 50/50 average we get half of each picture;
different proportions give different apparent transparency
7/27/2019 Inft1004_Lec5_MorePictureTechniques
20/49
21
Blending images
To overlay picture 1 and picture 2, we change the pixelsof picture 1 to be the averages of the pixels of the two
pictures
0 1 2 3 4 5
0
1
2
3
4
5
0 1 2 3 4 5
0
1
2
3
4
5
0 1 2 3 4 5
0
1
2
3
4
5
newPic1Pixel = (oldPic1Pixel*0.5) + (pic2Pixel*0.5)
newPic1Pixel pic2PixeloldPic1Pixel
If we do a 50/50 average we get half of each picture;
different proportions give different apparent transparency
7/27/2019 Inft1004_Lec5_MorePictureTechniques
21/49
22
Transparent overlay
Lets overlay a picture of a butterfly on apicture of a bridge
butterfly1.jpg
422
497
bridge.jpg
640
480
7/27/2019 Inft1004_Lec5_MorePictureTechniques
22/49
23
Transparent overlayWe need to specify at which coordinates of picture 1
(bridge) to start overlaying picture 2 (butterfly)
We need to specify how transparent to make picture 2
xstart
ystart 50%
transparent
7/27/2019 Inft1004_Lec5_MorePictureTechniques
23/49
24
Conditions
70%
transparent
40%
transparent
7/27/2019 Inft1004_Lec5_MorePictureTechniques
24/49
25
Conditions
If we want picture 2 to be 70%
transparent, we blend the colour
channels to be 70% of the pixel frompicture 1 and 30% from picture 2
70%
transparent
(oldPic1Pixel*0.7) + (pic2Pixel*0.3)
70%
30%
picture 1
picture 2
7/27/2019 Inft1004_Lec5_MorePictureTechniques
25/49
26
ConditionsIf we want picture 2 to be 40%
transparent, we blend the colourchannels to be 40% of the pixel frompicture 1 and 60% from picture 2
40%
transparent (oldPic1Pixel*0.4) + (pic2Pixel*0.6)
40%
60%
picture 1
picture 2
7/27/2019 Inft1004_Lec5_MorePictureTechniques
26/49
27
Conditions
(oldPic1Pixel*(n/100)) + (pic2Pixel*(100-n)/100)
n%
transparent
n%
(100 - n)%
In general, if we want picture 2 to be n% transparent,
we blend the colour channels to be n% of the pixel
from picture 1 and (100 n)% from picture 2
picture 1
picture 2
7/27/2019 Inft1004_Lec5_MorePictureTechniques
27/49
28
Example
(oldPic1Pixel*(80/100)) + (pic2Pixel*(100-80)/100)
80%
transparent
n%
(100 - 80)%
e.g. if we want picture 2 to be 80% transparent,
we blend the colour channels to be 80% of the pixel
from picture 1 and (100 80)% from picture 2
picture 1
picture 2
(oldPic1Pixel* 0.8)) + (pic2Pixel* 0.2)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
28/49
29
Example
See transparency() in lecture5demo.pydef transparency(pic1, pic2, xstart, ystart, proportion):
# Overlay pic2 on pic1, starting at coordinates xstart and ystart of pic1,
# with proportion determining the transparency (0-100) of the overlay
for x in range(0, getWidth(pic2)):
for y in range(0, getHeight(pic2)):
# x and y are coordinates in pic2; add xstart and ystart to get the matching coords in pic1
pix1 = getPixel(pic1, xstart + x, ystart + y)
pix2 = getPixel(pic2, x, y)
# Blend the channels: proportion% of pic1 and (100-proportion)% of pic2
newRed = getRed(pix1) * proportion / 100 + getRed(pix2) * (100 - proportion) / 100
newGreen = getGreen(pix1) * proportion / 100 + getGreen(pix2) * (100 - proportion) / 100
newBlue = getBlue(pix1) * proportion / 100 + getBlue(pix2) * (100 - proportion) / 100
newColour = makeColor(newRed, newGreen, newBlue)
# And apply this new blended colour to the pixel of pic1
setColor(pix1, newColour)
repaint(pic1)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
29/49
30
Fading
The book blends one picture into another by doinga section of picture 1 alone, a section of 50/50
transparency, and a section of picture 2 alone
This is worth examining to see how they manage
the coordinates of picture 1, picture 2, and the new
picture
(They paint to a new picture, whereas were
altering picture 1)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
30/49
31
Fading
But were going to do something quite different:
A full fade, from 0 transparency at the top of picture 2
to 100% transparency at the bottom
0%
100%
transparency
picture 2
7/27/2019 Inft1004_Lec5_MorePictureTechniques
31/49
32
Fading
y coordinate
Every y value needs its own transparency level
0%
100%
0
getHeight(picture2)
transparency
7/27/2019 Inft1004_Lec5_MorePictureTechniques
32/49
33
Fading
y coordinate
How do the transparency levels relate to the coordinates?
proportion = 100 * y / getHeight(pic)
0%
100%
0
getHeight(picture2)
transparency
7/27/2019 Inft1004_Lec5_MorePictureTechniques
33/49
34
Varying transparency levels
How do we work that out?
By problem solving looking at what were given
and using it to work out what we want
You cant avoid problem solving in programming!
See fadeIn() in lecture5demo.py
7/27/2019 Inft1004_Lec5_MorePictureTechniques
34/49
Subtracting Background
When we overlaid the picture of the butterfly on the bridge,the picture of the butterfly was a rectangle that included
background
35
7/27/2019 Inft1004_Lec5_MorePictureTechniques
35/49
Subtracting Background
When we overlaid the picture of the butterfly on the bridge,the picture of the butterfly was a rectangle that included
background
It might be good to get rid of the
background, and only overlay thebutterfly itself (the foreground)
That is, we want to subtract the
background from the picture
To do this, we need to know whichpixels are background and which
are foreground36
7/27/2019 Inft1004_Lec5_MorePictureTechniques
36/49
Subtracting Background
If we have one picture of the background alone,and one of the background with foreground . . .
We can compare the two, and choose only the
pixels that are different
background background + foreground 37
7/27/2019 Inft1004_Lec5_MorePictureTechniques
37/49
Subtracting Background
That way, we get just the foreground pixels.
38background + foreground foreground
7/27/2019 Inft1004_Lec5_MorePictureTechniques
38/49
Subtracting Background
Which we can overlay on a third picture, (a new
background)
39foreground + new backgroundforeground
7/27/2019 Inft1004_Lec5_MorePictureTechniques
39/49
40
Where it falls over
Ive used a gif butterfly with transparent background to
illustrate overlaying background and foreground.
If you use this in the real world say you take a picture of
something in front of a background and then a separate
picture of the background - you may encounter a couple of
problems.
backgroundforeground + background
7/27/2019 Inft1004_Lec5_MorePictureTechniques
40/49
41
Where it falls over
Shadow is really background, but its different from thebackground picture, so it registers as foreground
The problem with shadow can be fixed with direct lighting
shadow
7/27/2019 Inft1004_Lec5_MorePictureTechniques
41/49
42
Where it falls over
Another problem is that any colour in the foreground that is
very similar to the background registers as background
The problem with similarity between foreground and
background can be fixed only if we can ensure there is no
such similarity
some foreground colours
match background colours
7/27/2019 Inft1004_Lec5_MorePictureTechniques
42/49
43
Chromakey
Chromakey is a technique for subtracting background that
relies on the background being as close as possible to asingle colour . . .
And a colour that doesnt occur very often in the
foregrounds that we tend to work with
Why dont weather forecasters wear blue shirts?
7/27/2019 Inft1004_Lec5_MorePictureTechniques
43/49
44
Chromakey
Because theyre filmed
against a blue background,
and blue is then eliminatedfrom the picture in a
background subtraction
(green is also used a lot neither blue or green occur
in skin colours but watch what you wear)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
44/49
45
Chromakey
If the foreground is photographed against a blue
background . . .
The function becomes simpler, because its always
comparing with the same colour . . .
And the result is a lot cleaner
7/27/2019 Inft1004_Lec5_MorePictureTechniques
45/49
46
Drawing on picturesLike most programming languages, JES lets us add
certain features directly to a picture
addRect(pic, x1, y1, w, h)
All of these can take an additional argument, colour
addLine(pic, x1, x2, y1, y2)
addOvalFilled(pic, x1, y1, w, h)
addOval(pic, x1, y1, w, h)
addRectFilled(pic, x1, y1, w, h)
a line from (x1, y1) to (x2, y2)
a rectangle of width w andheight h starting at (x1, y1)
a filled rectangle
an oval that fits in a rectangle
of width w and height hstarting at (x1, y1)
a filled oval
7/27/2019 Inft1004_Lec5_MorePictureTechniques
46/49
47
Drawing on pictures
7/27/2019 Inft1004_Lec5_MorePictureTechniques
47/49
48
Writing on pictures
This, too, can take an additional argument, colour
Does the top of the string or the bottom of the string line upwith the vertical coordinate y?
How would you find out?
To alter the font or size see Ch16 page 398 and look atthe JES Pictures functionaddTextWithStyle()
addText(pic, x, y, string)the string (of text) starting
at location (x, y)
7/27/2019 Inft1004_Lec5_MorePictureTechniques
48/49
49
Writing on pictures
7/27/2019 Inft1004_Lec5_MorePictureTechniques
49/49
50
Moving on from picturesThats all for pictures for the time being. Next week well
start working with sound files
The MediaTools program will be very useful for that, so
if you havent yet installed it, now would be a good time
50