digital image manipulation -2012 cs4hs @ uca references: introduction to computing and programming...
Post on 17-Dec-2015
217 Views
Preview:
TRANSCRIPT
Digital Image Manipulation -2012 CS4HS @ UCA
References: • Introduction to computing and programming in Python – a multimedia
approach by Mark Guzdial and Barbara Ericson• JES: Jython Environment for Students,
http://code.google.com/p/mediacomp-jes/• http://nifty.stanford.edu/2011/parlante-image-puzzle/ by Nick Parlante
2012 CS4HS @ UCA 2
Outline of tasks
0. JES Introduction 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 3
Outline of tasks
0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 4
JES: Jython Environment for Students
• JES has a set of functions to manipulate media data (digital image and sounds)
• Let us open the environment
7/10/2012
2012 CS4HS @ UCA 5
JES 4.3
7/10/2012
2012 CS4HS @ UCA 67/10/2012
2012 CS4HS @ UCA 7
Outline of tasks
0. JES Introduction:1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 8
Pack a file to work with
7/10/2012
2012 CS4HS @ UCA 97/10/2012
Let us pick the beach.jpg
2012 CS4HS @ UCA 10
Let the computer remember it
• Assign a name to the file we can then refer and use it later.myFile = pickAFile()
7/10/2012
2012 CS4HS @ UCA 11
View the picture
• Let us make myFile an picture objectmyPict = makePicture(myFile)
• Does print myPict work?print(myPict)
• View myPict withshow(myPict)
7/10/2012
2012 CS4HS @ UCA 12
Make a function: pick and show
• We do not want to repeatedly type the same commands to load and show a picture every time
• Let us create it once and be able to use it whenever needed
7/10/2012
2012 CS4HS @ UCA 13
pickAndShow()
7/10/2012
2012 CS4HS @ UCA 14
Let us save it
• File -> Save Program• Specify the location (your USB drive), and give
it a name (say prog1)• Exit JES• Open JES again• File -> Open Program• Select prog1• It comes back
7/10/2012
2012 CS4HS @ UCA 15
Load program and use it
• Click “Load Program”• Then, in the command line area type
pict1 = pickAndShow()pict2 = pickAndShow()
7/10/2012
2012 CS4HS @ UCA 167/10/2012
2012 CS4HS @ UCA 17
Investigate the details
• Now let us investigate the details of a digital picture
• Pick “MediaTools” from the menu bar, then select “Picture Tool …”
7/10/2012
2012 CS4HS @ UCA 187/10/2012
(0, 0) (639, 0)
(0, 479)
(639, 479)
x
y
2012 CS4HS @ UCA 19
Outline of tasks
0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 20
Get a pixel and find its color value
7/10/2012
2012 CS4HS @ UCA 21
Change color value of a pixel
7/10/2012
2012 CS4HS @ UCA 22
Image manipulation
• It is all about changing color values of pixels • The process should be:– get a specific pixel first, and – then reset its color.
• Do we want to type all the command lines above for every pixel again and again? – The picture has 640 x 480 = 307,200 pixels.
• How should we do it? • For example, how do we zero out red values for all
pixels?7/10/2012
2012 CS4HS @ UCA 23
JES Functions
7/10/2012
• JES Functions -> Pictures -> getPixels• By using the function getPixels, we can get all
pixels at once. • We then loop through each pixel to zero out
its red value.• Let us open our prog1 and create another
function named ZeroRed
2012 CS4HS @ UCA 247/10/2012
2012 CS4HS @ UCA 25
Now let us use it
• Load Program• Pick a picture• Zero red out for all pixels in the picture• View the changes with Picture Tool …
7/10/2012
2012 CS4HS @ UCA 26
Work with the pictureModule
• We provide you a collection of functions in the pictureModule.py
• Open and load the module• Here are some available functions:– pickAndShow()– changeRed( pict, alpha )– changeGreen( pict, alpha )– changeBlue( pict, alpha )– ……
7/10/2012
2012 CS4HS @ UCA 27
chageRed(pict, alpha)
def changeRed( pict, alpha ): allPixels = getPixels( pict ) for p in allPixels: value = alpha * getRed( p ) if value > 255: value = 255 setRed( p, value)
7/10/2012
2012 CS4HS @ UCA 28
Change values of Green and Blue
• changeGreen( pict, alpha)• changeBlue ( pict, alpha)
7/10/2012
2012 CS4HS @ UCA 29
Outline of tasks
0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 30
Solve a puzzle
• Load the image copper-puzzel.png pic = pickAndShow()
• A picture is hidden in.• Let’s find it?
7/10/2012
2012 CS4HS @ UCA 31
Solution
1. Zero out redness 2. Increase greenness 20 times for all pixels3. Increase the blueness 20 times for all pixels
7/10/2012
2012 CS4HS @ UCA 32
Solve two more puzzles
• iron-puzzle.png, andHint: Increase redness 15 times for all pixels
• canvas.pngHint: Increase blueness 16 times
7/10/2012
2012 CS4HS @ UCA 33
How do we create a puzzle?
• Zero out R, G, or B• Weak the remaining color• Add randomly generated values for the zero
out color
7/10/2012
2012 CS4HS @ UCA 34
Outline of tasks
0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 35
Hiding a message in a picture (steganography)
7/10/2012
A scenic picture (original) This picture with a hidden message
2012 CS4HS @ UCA 367/10/2012
2012 CS4HS @ UCA 37
Problem specification
• Inputs:– A cover image– A message
• Outputs: – An image seems exactly as the original but with
the message embedded– From the image, one can recover the embedded
message
7/10/2012
2012 CS4HS @ UCA 38
Solution design
• To embed the message, we need to identify the position of each pixel of the message and remember them.
• To remember the position of a pixel, we only need to change the original very little such that the altered image seem exactly as the original
• When we need to reveal the message, we only need to print the pixels at the positions remembered.
7/10/2012
2012 CS4HS @ UCA 39
Solution design continue
• To identify the position of each pixel of the message, we check if a pixel is close to black (or whatever color the text is).
• To remember the position of a pixel, we do the followings:– Preparation: Make the red (or green, blue) value of each pixel
even. – Memorization: For each message pixel, add one to the red
value (become odd now). – Note: These two steps change the original very little
• To reveal the message, we only need to print the pixels whose red value is odd.
7/10/2012
2012 CS4HS @ UCA 40
Implementation: encodedef stegano(msgPic, coverPic): for pxl in getPixels(coverPic): r = getRed(pxl) if r%2 ==1: setRed(pxl, r-1) for x in range(1, getWidth(coverPic)): for y in range(1, getHeight(coverPic)): msgPxl = getPixel(msgPic, x, y) coverPxl = getPixel(coverPic, x, y) if distance(getColor(msgPxl), black) < 100.0: setRed(coverPxl, getRed(coverPxl) +1) return coverPic
7/10/2012
2012 CS4HS @ UCA 41
Implementation: decodedef deStega(segaPic): w = getWidth(segaPic) h = getHeight(segaPic) msg = makeEmptyPicture(w, h) for x in range(0,w): for y in range(0, h): encPxl = getPixel(segaPic, x, y) msgPxl = getPixel(msg, x, y) if (getRed(encPxl)%2) ==1: setColor(msgPxl, black) show(msg) return msg
7/10/2012
2012 CS4HS @ UCA 42
Integration and maintenance
• The application should be either to encode a message in a cover image or discover the message from an encoded image.
• For the former, we need to pick a cover image, then a message as an image. Then, encode it.
• For the later, we need to recover the message from its cover image.
7/10/2012
2012 CS4HS @ UCA 43
Let us do it
• Open and load the pictureModule• Load two pictures, one is the cover image and the
other is the text image, with coverPic = pickAndShow()msg = pickAndShow()
• Embed the message in the cover imagesecret = stegano (coverPic, msg)
• Save it with JES Functions -> Pictures -> writePictureTo
• Note: use the .png extension not .jpg7/10/2012
2012 CS4HS @ UCA 44
Recover the message
• Load the picture that you just savedpic = pickAndShow()
• The, decipher it with deStega(pic)
7/10/2012
2012 CS4HS @ UCA 45
Outline of tasks
0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 46
Let us fix the temple in Athens
• What is the problem specification?
• How do we design a solution?
• Implement it• Test and refine the
computational solution
7/10/2012
2012 CS4HS @ UCA 47
How do we fix it?
• Due the symmetry, we may flip the left half to the right
• How do we do it?
7/10/2012
2012 CS4HS @ UCA 48
Try to fix it with reflection
7/10/2012
2012 CS4HS @ UCA 49
Mirroring a picture -vertically
7/10/2012
2012 CS4HS @ UCA 50
Specification
• The center line is x = midpoint = width/2.• The x-coordinate of a pixel on the left half is:
width/2 - offset • The x-coordinate of a pixel on the right half is:
width/2 + offset
7/10/2012
2012 CS4HS @ UCA 51
Further specification - symmetrically reflecting from left to right
• Stay on the same row, i. e., the value of y does not change
• The x-distance to the center should be the same
• We need to get the color value of a pixel at (midpoint – offset, y) and assign it to the pixel located at (midpoint + offset, y)
7/10/2012
2012 CS4HS @ UCA 52
Design:
• For a given row, fixed y, loop with offset from 1 to width/2:– Get the left pixel at (midpoint – offset, y)
lpixel = getPixel(pic, midpoint – offset, y)
– Get the color value of left pixel color = getColor(lpixel)– Get the mirror (right) pixel (midpoint + offset, y)
rpixel = getPixel(pic, midpoint + offset, y)
– Make the color of right pixel the same as the color of the left pixel
setColor(rpixel, color)
• The above operation mirrors one row, we need to do it for every row. Hence, we should loop y from 1 to height)
7/10/2012
2012 CS4HS @ UCA 53
Implementationdef mirrorLR(pic): #mirror a picture from left to right midpoint = getWidth(pic)/2 for y in range(0, getHeight(pic)): for offset in range(1, midpoint): lpixel = getPixel(pic, midpoint - offset, y) color = getColor(lpixel) rpixel = getPixel(pic, midpoint + offset, y) setColor(rpixel, color)
Note: This is a nested loop, i.e., loop of loop. Why do we need it?
7/10/2012
2012 CS4HS @ UCA 54
To test the function, we• Load a picture object in computer memory
>>> mypict = pickAndShow( )• Apply the function to it
>>> mirrorLR(mypict)• Does it work?
>>> repaint(mypict)
7/10/2012
2012 CS4HS @ UCA 55
A function named main
• We have created functions to perform specific tasks.
• These functions can be assembled in solving applications.
• By writing a function named main, we can call it without typing in command line
• In fact, in C and C++, the main function is required and the only one can be executed directly
7/10/2012
2012 CS4HS @ UCA 56
A sample main functiondef main():
pict = pickAndShow( ) mirrorLtoR(pict) repaint(pict)
Note: Using a simple main function, one may easily test design ideas and subtasks. In computer science, this kind of purpose is also called a driver.
7/10/2012
2012 CS4HS @ UCA 57
Outline of tasks
0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles4. Embed information in a picture5. Fix the Temple of Hephaestus virtually6. Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 58
Landing Dr. Mark Guzdial on the moon
7/10/2012
From Mark Guzdial’s book: Introduction to computing and programming in Python
2012 CS4HS @ UCA 59
Application specification
• Provided two pictures: background and foreground
• Replace the background through background subtraction
7/10/2012
2012 CS4HS @ UCA 60
Applications of chromakey
• Weatherman makes forecasts on TV with a green (or blue) background
• In TV broadcast, they use background subtraction to combine the map
• It is also very popular in making movies.
7/10/2012
2012 CS4HS @ UCA 61
Further specification
• Background subtraction is all about replacing some pixels in foreground with those pixels in background (without the person).
• In general, it is still hard for a computer to distinguish if a pixel belongs to the foreground or background.
• Let us simplify the background with a single color (say green or blue).
7/10/2012
2012 CS4HS @ UCA 62
Where does it belong?
• Provide a single colored background• Let a person take a picture with that
background• To determine if a pixel belongs to the
background or not, we only need to compare color distance.
7/10/2012
2012 CS4HS @ UCA 63
Implementationdef chromakey(foreground, background ): for p in getPixels( foreground ): # Assume a pixel blue if the sum of its R + G < B if getRed(p) + getGreen(p) < getBlue(p): bgPixel = getPixel(background, getX(p),
getY(p)) newColor = getColor( bgPixel ) setColor( p, newColor ) show( foreground )
Note: We assume that the size of background is no less than that of foreground
7/10/2012
2012 CS4HS @ UCA 64
Land Mark on the moon
7/10/2012
2012 CS4HS @ UCA 65
Land yourself on the moon (or anywhere else you want) virtually?
• What do you need (input data)?• Modify the chromakey function• Create a program that can virtually land
yourself on the moon (or anywhere else you want to) .
7/10/2012
2012 CS4HS @ UCA 667/10/2012
top related