Digital Image Manipulation UCA

Slides:



Advertisements
Similar presentations
Introduction to Computing CS A109. Course Objectives Goal is to teach computation in terms relevant to non-CS majors Students will be able to read, understand,
Advertisements

Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CS2984: Introduction to Media Computation Drawing directly on images.
Introduction to compositing. What is compositing?  The combination of two images to produce a single image  Many ways we can do this, especially in.
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
CS1315: Introduction to Media Computation Introduction to Programming.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
Python: Making colors and Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
01-IntroToMediaComp1 Barb Ericson Georgia Institute of Technology Oct 2010 Introduction to Computer Science and Media Computation.
CS 102 Computers In Context (Multimedia)‏ 02 / 18 / 2009 Instructor: Michael Eckmann.
James Tam Programming: Part II In this section of notes you will learn about more advanced programming concepts such as looping, functions.
CS 102 Computers In Context (Multimedia)‏ 02 / 25 / 2009 Instructor: Michael Eckmann.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Graphics in Python using the JES environment
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
TOPIC 4 INTRODUCTION TO MEDIA COMPUTATION: DIGITAL PICTURES Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Image Processing & Perception Sec 9-11 Web Design.
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
Download JES Tool JES: Jython Environment for Students
Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
Replacing colors using if We don’t have to do one-to-one changes or replacements of color We can use if to decide if we want to make a change.  We could.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
CS1315: Introduction to Media Computation Referencing pixels directly by index number.
CS1315: Introduction to Media Computation Introduction to Programming.
CS 100 Introduction to Computing Introduction to JES Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
01-IntroToMediaComp1 Barb Ericson Georgia Institute of Technology Feb 2010 Introduction to Computer Science and Media Computation.
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
Learning PowerPoint Presenting your ideas as a slide show… …on the computer!
Yet Another Version. More Careful Edge Detection def lineDetect(filename): orig = makePicture(filename) makeBw = makePicture(filename) for x in range(0,getWidth(orig)-1):
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Changing colors in an area Part 3: Chromakey.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
1 CS 177 Week 2 Recitation Slides Variables, Files and Functions.
CS1315: Introduction to Media Computation Introduction to JES.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Ways to Check for Divisibility Dividing by 2 All even numbers are divisible by 2 Even numbers are numbers that end with either 2, 4, 6, 8, or 0.
CS1315: Introduction to Media Computation Introduction to Programming.
Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select.
1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions.
Python programming Using the JES picture functions and defining new functions.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS1315: Introduction to Media Computation Introduction to JES.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Multimedia Summer Camp
Image Processing & Perception
CS1315: Introduction to Media Computation
CS1315: Introduction to Media Computation
Multimedia Summer Camp
Chapter 5 Working with Images
Chapter 2: Introduction to Programming
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
A Media Computation Cookbook
Chair Professor Chin-Chen Chang (張真誠) National Tsing Hua University
CS 177 Week 3 Recitation Slides
CSIS110 - Introduction to Computer Science
CS 101: Introduction to Computing
CS1315: Introduction to Media Computation
Multimedia Summer Camp
CS1315: Introduction to Media Computation
CSIS110 - Introduction to Computer Science
Presentation 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

Outline of tasks 0. JES Introduction 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

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

JES 4.3 7/10/2012 2012 CS4HS @ UCA

7/10/2012 2012 CS4HS @ UCA

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

Pack a file to work with 7/10/2012 2012 CS4HS @ UCA

Let us pick the beach.jpg 7/10/2012 2012 CS4HS @ UCA

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

View the picture Let us make myFile an picture object myPict = makePicture(myFile) Does print myPict work? print(myPict) View myPict with show(myPict) 7/10/2012 2012 CS4HS @ UCA

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

pickAndShow() 7/10/2012 2012 CS4HS @ UCA

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

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

7/10/2012 2012 CS4HS @ UCA

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

(0, 0) (639, 0) x y (639, 479) (0, 479) 7/10/2012 2012 CS4HS @ UCA

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

Get a pixel and find its color value 7/10/2012 2012 CS4HS @ UCA

Change color value of a pixel 7/10/2012 2012 CS4HS @ UCA

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

JES Functions 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 7/10/2012 2012 CS4HS @ UCA

7/10/2012 2012 CS4HS @ UCA

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

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

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

Change values of Green and Blue changeGreen( pict, alpha) changeBlue ( pict, alpha) 7/10/2012 2012 CS4HS @ UCA

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

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

Solution Zero out redness Increase greenness 20 times for all pixels Increase the blueness 20 times for all pixels 7/10/2012 2012 CS4HS @ UCA

Solve two more puzzles iron-puzzle.png, and canvas.png Hint: Increase redness 15 times for all pixels canvas.png Hint: Increase blueness 16 times 7/10/2012 2012 CS4HS @ UCA

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

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

Hiding a message in a picture (steganography) A scenic picture (original) This picture with a hidden message 7/10/2012 2012 CS4HS @ UCA

7/10/2012 2012 CS4HS @ UCA

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

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

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

Implementation: encode def 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

Implementation: decode def 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

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

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 image secret = stegano (coverPic, msg) Save it with JES Functions -> Pictures -> writePictureTo Note: use the .png extension not .jpg 7/10/2012 2012 CS4HS @ UCA

Recover the message Load the picture that you just saved pic = pickAndShow() The, decipher it with deStega(pic) 7/10/2012 2012 CS4HS @ UCA

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

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

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

Try to fix it with reflection 7/10/2012 2012 CS4HS @ UCA

Mirroring a picture -vertically 7/10/2012 2012 CS4HS @ UCA

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

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

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

Implementation def 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

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

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

A sample main function def 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

Outline of tasks 0. JES Introduction: 1. Load and investigate details of an image 2. Alternate pixel RGB values 3. Solve image puzzles 4. Embed information in a picture 5. Fix the Temple of Hephaestus virtually 6. Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

Landing Dr. Mark Guzdial on the moon From Mark Guzdial’s book: Introduction to computing and programming in Python 7/10/2012 2012 CS4HS @ UCA

Application specification Provided two pictures: background and foreground Replace the background through background subtraction 7/10/2012 2012 CS4HS @ UCA

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

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

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

Implementation def 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

Land Mark on the moon 7/10/2012 2012 CS4HS @ UCA

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

7/10/2012 2012 CS4HS @ UCA