This action might not be possible to undo. Are you sure you want to continue?
Lecture 5 More Image Manipulation (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
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 Mar 4 Mar 11 Mar 18 Mar 25 Apr 1 - Apr 7 Apr 8 Apr 15 Apr 22 Introduction Programs, Arrays and Iteration Working with x and y coordinates Selection
Mid Semester Recess Period More Picture Techniques Sound and Arrays More Sound and Arrays
Practical Test 1 in Lab class
29 Program Design and lab Strings you are You Apr must attend the Week 9 May 6 Lists, Files and Modules registered in – or you will not be Week 10 May 13 Web, Representations, Steganography ableMay to do the practical test Week 11 20 Turtles and Other Classes Week 12 Week 13 May 27 Jun 3 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
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 .
define it as a function. we use parameters and arguments to deal with the variations 7 .Revision . and replace the multiple occurrences with multiple calls to the function If there are variations between the occurrences.Functions Programs are made up of one of more functions If we want a function to take arguments. we should extract it. we include corresponding parameters when defining it Whenever we find the same piece of code appearing several times in a program.
sequence. be sure to note the effect of each statement before going on to consider the next sequence 8 .Revision . selection and iteration The statements in a function or a loop will be executed in the order they’re written When working out what the code does.
sequence. as many times as specified sequence loop 9 .Revision . a loop iterates (repeats) the statements in its body. selection and iteration Within a sequence of statements.
selection and iteration In a selection statement.sequence.Revision . the program chooses which bits of code to execute depending on the value of a boolean condition ? selection loop sequence 10 .
the body of the else is executed if the condition is false 11 .Revision – forms of selection Selection is done with the if statement The body of an if statement is executed if the condition is true If there’s an else part.
an abbreviation of else if The first body of an elif that has a true condition is executed (all preceding conditions must be false) After any body (if. else) is executed. the rest of the if sequence is skipped 12 . we generally use elif.Revision – forms of selection If we want to test multiple options. elif.
explaining briefly what it does 3. A comment at the start of every program (collection of functions). saying who wrote it. A comment at the start of every function. and why 2. and when.Revision .Comments Your programs must include three kinds of comments: 1. A comment with every bit of code that another programmer might find easier to understand if it’s explained Do not write comments to explain what will be obvious to a reasonable programmer! 13 .
changing the original Sometimes one might want to keep the original and work with a copy 14 .Oversight – makeEmptyPicture() Earlier lectures overlooked something that’s pretty important in image manipulation We’ve always manipulated an image itself.
Oversight – makeEmptyPicture() Once you have the height and width of a picture. 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 15 . makeEmptyPicture(width.
height) 16 .Oversight – makeEmptyPicture() width height makeNewPicture(width.
Oversight – makeEmptyPicture() So you can use this to: 1) create a new picture and 2) copy (the colours of) the pixels from the original to the copy 3) manipulate the copy without altering the original 17 .
More Image Manipulation There’s very little new programming in this lecture Instead we revise the programming we’ve done by seeing some more approaches to manipulating images We’ll see how to blend images. use chromakey. and draw and write on images 18 . subtract background.
looking through one somewhat transparent image at another image.Blending images To get the effect of transparency. all we have to do is average the colours of the corresponding pixels. 19 .
Blending images To overlay picture 1 and picture 2. different proportions give different apparent transparency 20 . we change the pixels of 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.
we change the pixels of 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.5) newPic1Pixel 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 oldPic1Pixel 0 1 2 3 4 5 0 1 2 3 4 5 pic2Pixel 0 1 2 3 4 5 21 .5) + (pic2Pixel*0.Blending images To overlay picture 1 and picture 2. different proportions give different apparent transparency newPic1Pixel = (oldPic1Pixel*0.
Transparent overlay Let’s overlay a picture of a butterfly on a picture of a bridge 480 422 640 497 butterfly1.jpg bridge.jpg 22 .
Transparent overlay We 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 23 .
Conditions 70% transparent 40% transparent 24 .
3) 25 .Conditions If we want picture 2 to be 70% transparent. we blend the colour channels to be 70% of the pixel from picture 1 and 30% from picture 2 70% picture 1 30% 70% transparent picture 2 (oldPic1Pixel*0.7) + (pic2Pixel*0.
6) 26 . we blend the colour channels to be 40% of the pixel from picture 1 and 60% from picture 2 40% 60% 40% transparent picture 2 (oldPic1Pixel*0.4) + (pic2Pixel*0.Conditions picture 1 If we want picture 2 to be 40% transparent.
we blend the colour channels to be n% of the pixel from picture 1 and (100 – n)% from picture 2 picture 1 n% (100 .n)% n% transparent picture 2 (oldPic1Pixel*(n/100)) + (pic2Pixel*(100-n)/100) 27 . if we want picture 2 to be n% transparent.Conditions In general.
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 n% (100 .80)% 80% transparent picture 2 (oldPic1Pixel*(80/100)) + (pic2Pixel*(100-80)/100) (oldPic1Pixel* 0.2) 28 .Example e.8)) + (pic2Pixel* 0.g.
ystart + y) pix2 = getPixel(pic2.proportion) / 100 newGreen = getGreen(pix1) * proportion / 100 + getGreen(pix2) * (100 . newBlue) # And apply this new blended colour to the pixel of pic1 setColor(pix1. proportion): # Overlay pic2 on pic1. newColour) repaint(pic1) 29 .proportion) / 100 newColour = makeColor(newRed. y) # Blend the channels: proportion% of pic1 and (100-proportion)% of pic2 newRed = getRed(pix1) * proportion / 100 + getRed(pix2) * (100 . x. add xstart and ystart to get the matching coords in pic1 pix1 = getPixel(pic1.proportion) / 100 newBlue = getBlue(pix1) * proportion / 100 + getBlue(pix2) * (100 . getWidth(pic2)): for y in range(0.py def transparency(pic1. # with proportion determining the transparency (0-100) of the overlay for x in range(0. getHeight(pic2)): # x and y are coordinates in pic2. pic2. xstart + x. starting at coordinates xstart and ystart of pic1. newGreen.Example See transparency() in lecture5demo. xstart. ystart.
picture 2. and the new picture (They paint to a new picture. 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.Fading The book blends one picture into another by doing a section of picture 1 alone. whereas we’re altering picture 1) 30 .
Fading 0% picture 2 transparency 100% But we’re going to do something quite different: A full fade. from 0 transparency at the top of picture 2 to 100% transparency at the bottom 31 .
Fading 0% 0 transparency y coordinate 100% getHeight(picture2) Every y value needs its own transparency level 32 .
Fading 0% 0 transparency y coordinate 100% getHeight(picture2) How do the transparency levels relate to the coordinates? proportion = 100 * y / getHeight(pic) 33 .
py 34 .Varying transparency levels How do we work that out? By problem solving – looking at what we’re given and using it to work out what we want You can’t avoid problem solving in programming! See fadeIn() in lecture5demo.
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 .
the picture of the butterfly was a rectangle that included background It might be good to get rid of the background. and only overlay the butterfly itself (the foreground) That is. we need to know which pixels are background and which are foreground 36 .Subtracting Background When we overlaid the picture of the butterfly on the bridge. we want to subtract the background from the picture To do this.
.Subtracting Background If we have one picture of the background alone. and one of the background with foreground . and choose only the pixels that are different background background + foreground 37 . . We can compare the two.
background + foreground foreground 38 .Subtracting Background That way. we get just the foreground pixels.
Subtracting Background Which we can overlay on a third picture. (a new background) foreground foreground + new background 39 .
Where it falls over I’ve 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. foreground + background background 40 .
Where it falls over Shadow is really background. but it’s different from the background picture. so it registers as foreground The problem with shadow can be fixed with direct lighting shadow 41 .
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 42 .
Chromakey Chromakey is a technique for subtracting background that relies on the background being as close as possible to a single colour . . . And a colour that doesn’t occur very often in the foregrounds that we tend to work with Why don’t weather forecasters wear blue shirts? 43 .
and blue is then eliminated from 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) 44 .Chromakey Because they’re filmed against a blue background.
. .Chromakey If the foreground is photographed against a blue background . . The function becomes simpler. because it’s always comparing with the same colour . . And the result is a lot cleaner 45 .
w. y1. y1) a filled oval All of these can take an additional argument. y1) a filled rectangle an oval that fits in a rectangle of width w and height h starting at (x1. x1. w. h) addOval(pic. y1) to (x2. y1. y1. h) addOvalFilled(pic. y2) a rectangle of width w and height h starting at (x1. x1.Drawing on pictures Like most programming languages. x1. y1. JES lets us add certain features directly to a picture addLine(pic. w. w. x1. y2) addRect(pic. h) a line from (x1. colour 46 . x1. y1. x2. h) addRectFilled(pic.
Drawing on pictures 47 .
colour Does the top of the string or the bottom of the string line up with the vertical coordinate y? How would you find out? To alter the font or size – see Ch16 page 398 – and look at the JES Pictures function addTextWithStyle() 48 . y.Writing on pictures addText(pic. too. y) This. can take an additional argument. string) the string (of text) starting at location (x. x.
Writing on pictures 49 .
now would be a good time 50 . Next week we’ll start working with sound files The MediaTools program will be very useful for that.Moving on from pictures That’s all for pictures for the time being. so if you haven’t yet installed it.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.