650 likes | 667 Views
Learn the fundamentals of computer science and programming languages within the context of media manipulation and creation. Explore topics such as algorithms, data structures, human-computer interface, networking, and graphics.
E N D
Introduction to Computer Science and Media Computation Barb Ericson Georgia Institute of Technology Feb 2010 Intro CS and Media Computation
Learning Goals • What is Computer Science? • What are programming languages? • What do computers understand? • What is Media Computation? 06-Intro-Object-Oriented-Prog
What is Computer Science? • The study of process, how to specify what is to be done and define the stuff being processed. • You can say that is the study of computational recipes • Ones that can be executed on a computer • A recipe that runs on a computer is called a program 06-Intro-Object-Oriented-Prog
What do Computer Scientists do? • Study if there are better ways to write recipes • Algorithms – textual description of how to solve a problem • Study how to structure the data in the recipes • Data structures and databases • Determine if there are recipes that can't be written? That make machines intelligent? • Theory, artificial intelligence • Study how to make computers easier for people to use • Human-computer interface • Study how computers communicate with each other • Networking • Study how to create 3D models and simulations • Graphics and scientific computing 06-Intro-Object-Oriented-Prog
Multimedia CS1 in Python • Focus: Learning programming and CS concepts within the context of media manipulation and creation • Converting images to grayscale and negatives, splicing and reversing sounds, writing programs to generate HTML, creating movies out of Web-accessed content. • Computing for communications, not calculation 06-Intro-Object-Oriented-Prog
Python as the programming language Used in commercial contexts IL&M, Google, Nextel, etc. Minimal syntax Looks like other programming languages Potential for transfer Actually using Jython which is Python with Java underneath 06-Intro-Object-Oriented-Prog
Some Computer Science Topics inter-mixed algorithms across media Sampling a picture (to scale it) is the same algorithm as sampling a sound (to shift frequency) Blending two pictures (fading one into the other) and two sounds is the same algorithm. representations and mappings (Goedel) From samples to numbers (and into Excel), through a mapping to pixel colors 06-Intro-Object-Oriented-Prog
We will program in JES JES: Jython Environment for Students A simple editor (for entering in our programs or recipes): the program area A command area for entering in commands for Python to execute. 06-Intro-Object-Oriented-Prog
Using JES – Try the following >>> print 3 + 5 >>> print 23.2 / 3 >>> print 1 / 3 >>> print 1.0 / 3 >>> print 10 % 3 >>> print "Hello" >>> print "Hello" + "Barb" >>> print 10 > 3 >>> print 3 > 10 Print will print out the result from the following expression 06-Intro-Object-Oriented-Prog
Did any answer surprise you? • Integer division results in an integer answer 1 / 3 = 0 • The values after the decimal point are thrown away • If you want a floating point result using a floating point value in the expression (1.0 / 3) • You can append strings one after the other, but this doesn't add any spaces >>> print "Hello" + "Barb" HelloBarb • Python uses 0 for false and 1 for true >>> print 10 > 3 1 >>> print 3 > 10 0 06-Intro-Object-Oriented-Prog
Command Area Editing Up/down arrows walk through command history You can edit the line at the bottom and then hit Return/Enter that makes that last line execute 06-Intro-Object-Oriented-Prog
Demonstrating JES for files and sounds >>> print pickAFile() c:/ip-book/mediasources/barbara.jpg >>> print makePicture(pickAFile()) Picture, filename c:/ip-bookmediasources/barbara.jpg height 294 width 222 >>> show(makePicture(pickAFile())) None >>> print pickAFile() C:/ip-book/mediasources/hello.wav >>> print makeSound(pickAFile()) Sound of length 54757 >>> print play(makeSound(pickAFile())) None 06-Intro-Object-Oriented-Prog
Naming parts – declaring variables • You can name the result from a function call • And then use the name as input to other functions myFile = pickAFile() # name the picked file myPict = makePicture(myFile) # name the picture show(myPict) • The value associated with that name is used • = doesn't mean equals here but assign the value for myFile to the result of pickAFile() 06-Intro-Object-Oriented-Prog
Quick Calculation • What if an item is 30% off and you also have a coupon for an additional 20% off the sale price? • If the original cost was $45.00, how much is the price after the 30% and then how much do you pay with the additional 20% off? • Use the python command area to figure it out • Name the result of each calculation 06-Intro-Object-Oriented-Prog
Making our own functions To make a function, use the command def Then, the name of the function, and the names of the input values between parentheses (“(input1)”) End the line with a colon (“:”) The body of the recipe is indented (Hint: Use three spaces) That’s called a block 06-Intro-Object-Oriented-Prog
Making functions the easy way Get something working by typing commands in the command window (bottom half of JES) Enter the def command in the editing window (top part of JES) Copy-paste the right commands up into the recipe 06-Intro-Object-Oriented-Prog
A recipe for playing picked sound files def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound) Note:myFile and mySound, inside pickAndPlay(), are completely different from the same names in the command area. We say that they are in a different scope. 06-Intro-Object-Oriented-Prog
Blocking is indicated for you in JES Statements that are indented the same, are in the same block. Statements in the same block as the cursor are enclosed in a blue box. What happens when you type pickAndShow is that all statements in the block will be executed. 06-Intro-Object-Oriented-Prog
Image Processing Goals: Give you the basic understanding of image processing, including psychophysics of sight, Identify some interesting examples to use 06-Intro-Object-Oriented-Prog
We perceive light different from how it actually is Color is continuous Visible light is in the wavelengths between 370 and 730 nanometers That’s 0.00000037 and 0.00000073 meters But we perceive light with color sensors that peak around 425 nm (blue), 550 nm (green), and 560 nm (red). Our brain figures out which color is which by figuring out how much of each kind of sensor is responding One implication: We perceive two kinds of “orange” — one that’s spectral and one that’s red+yellow (hits our color sensors just right) Dogs and other simpler animals have only two kinds of sensors They do see color. Just less color. 06-Intro-Object-Oriented-Prog
Luminance vs. Color We perceive borders of things, motion, depth via luminance Luminance is not the amount of light, but our perception of the amount of light. We see blue as “darker” than red, even if same amount of light. Much of our luminance perception is based on comparison to backgrounds, not raw values. Luminance is actually color blind. Completely different part of the brain. 06-Intro-Object-Oriented-Prog
Digitizing pictures We digitize pictures into lots of little dots Enough dots and it looks like a continuous whole to our eye Our eye has limited resolution Our background/depth acuity is particularly low Each picture element is referred to as a pixel Pixels are picture elements Each pixel object knows its color It also knows where it is in its picture 06-Intro-Object-Oriented-Prog
Exploring Pictures >>> file = pickAFile() >>> beachPict = makePicture(file) >>> explore(beachPict) • Zoom in to see individual pixels • Move the cursor to see the x and y values • Look at the red, green, and blue values 06-Intro-Object-Oriented-Prog
Encoding color Each pixel encodes color at that position in the picture Lots of encodings for color Printers use CMYK: Cyan, Magenta, Yellow, and blacK. Others use HSB for Hue, Saturation, and Brightness (also called HSV for Hue, Saturation, and Value. We’ll use the most common for computers RGB: Red, Green, Blue 06-Intro-Object-Oriented-Prog
Encoding Color: RGB In RGB, each color has three component colors: Amount of redness Amount of greenness Amount of blueness Each does appear as a separate dot on most devices, but our eye blends them. In most computer-based models of RGB, a single byte (8 bits) is used for each So a complete RGB color is 24 bits, 8 bits of each 06-Intro-Object-Oriented-Prog
Making Colors with Light • Type >>> myColor = pickAColor() • Try to create • White • Black • Yellow • Red • Brown • Purple 06-Intro-Object-Oriented-Prog
Basic Picture Functions makePicture(filename) creates and returns a picture object, from the JPEG file at the filename show(picture) displays a picture in a window explore(picture) makes a copy of the picture and shows it in the explorer window We’ll learn functions for manipulating pictures like getColor, setColor, and repaint 06-Intro-Object-Oriented-Prog
Writing a recipe: Making our own functions To make a function, use the command def Then, the name of the function, and the names of the input values between parentheses (“(input1)”) End the line with a colon (“:”) The body of the recipe is indented (Hint: Use two spaces) Your function does NOT exist for JES until you load it 06-Intro-Object-Oriented-Prog
Saving Functions in Files • Click on File and then Save Program • Name it with some file name and .py at end • You can define more than one function in a file • Maybe call these pictureFunctions.py • You can later open these files up • And use the Load Program button to load all functions in the file • You can build a library of python functions for working with pictures, sounds, movies, etc 06-Intro-Object-Oriented-Prog
Modifying a Pixel Color • You can get the amount of red, green, or blue • redValue = getRed(pict) • greenValue = getGreen(pict) • blueValue = getBlue(pict) • You can change the amount of red, green, or blue • setRed(pict,value) • setGreen(pict,value) • setBlue(pictValue) 06-Intro-Object-Oriented-Prog
Modifying Colors • You can also get the color from a pixel • myColor = getColor(pixel) • You can create a new color by giving values for red, green, and blue from 0 to 255 • newColor = makeColor(255,0,0) • You can set a color using • setColor(pixel,newColor) 06-Intro-Object-Oriented-Prog
How to change lots of pixels? • If we want to change all the pixels in a picture how can we do that? • In a 640 x 480 picture that is 307,200 pixels • That sounds too long to do one at a time • Computers are very fast and can process billions of instructions per second • But we wouldn't want to name each pixel or modify the color on each one by typing in the commands 307,200 times • We can get an array of pixels to process • Using getPixels(picture) and loop through the pixels in the array • It gets the first row of pixels followed by the second row and so on 06-Intro-Object-Oriented-Prog
What is an array? • Space in memory for many values of the same type • Numbers, pictures, pixels, etc • You can refer to the elements of an array using an index • Starting with 0 for the first element • And length – 1 for the last element 06-Intro-Object-Oriented-Prog
Processing Pixels in an Array >>> file="C:/ip -book/mediasources/barbara.jpg" >>> pict=makePicture(file) >>> show(pict) >>> pixels = getPixels(pict) >>> setRed(pixels [0], getRed(pixels [0]) * 0.5) >>> setRed(pixels [1], getRed(pixels [1]) * 0.5) >>> setRed(pixels [2], getRed(pixels [2]) * 0.5) >>> setRed(pixels [3], getRed(pixels [3]) * 0.5) >>> setRed(pixels [4], getRed(pixels [4]) * 0.5) >>> setRed(pixels [5], getRed(pixels [5]) * 0.5) >>> repaint(pict) 06-Intro-Object-Oriented-Prog
Use a loop!Our first picture recipe def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) Used like this: >>> file="c:/ip-book/mediasources/katie.jpg" >>> picture=makePicture(file) >>> explore(picture) >>> decreaseRed(picture) >>> explore(picture) 06-Intro-Object-Oriented-Prog
It’s not iteration—it’s a set operation Research in the 1970’s found that people are better at set operations than iteration. For all records, get the last name, and if it starts with “G” then… => HARD! For all records where the last name starts with “G”… => Reasonable! Because the Python for loop is a forEach, we can start out with treating it as a set operation: “For all pixels in the picture…” 06-Intro-Object-Oriented-Prog
How do you make an omelet? Something to do with eggs… What do you do with each of the eggs? And then what do you do? All useful recipes involve repetition - Take four eggs and crack them…. - Beat the eggs until… We need these repetition (“iteration”) constructs in computer algorithms too - Today we will introduce one of them 06-Intro-Object-Oriented-Prog
Decreasing the red in a picture Recipe: To decrease the red Ingredients: One picture, name it pict and pass it to the function where we will call it picture Step 1: Get all the pixels of picture. For each pixel p in the set of pixels… Step 2: Get the value of the red of pixel p, and set it to 50% of its original value 06-Intro-Object-Oriented-Prog
Use a for loop!Our first picture recipe def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) The loop - Note the indentation! 06-Intro-Object-Oriented-Prog
How for loops are written foris the name of the command An index variable is used to hold each of the different values of a sequence The word in A function that generates a sequence The index variable will be the name for one value in the sequence, each time through the loop A colon (“:”) And a block (the indented lines of code) def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) 06-Intro-Object-Oriented-Prog
What happens when a for loop is executed The index variable is set to an item in the sequence The block is executed The variable is often used inside the block Then execution loops to the for statement, where the index variable gets set to the next item in the sequence Repeat until every value in the sequence was used. 06-Intro-Object-Oriented-Prog
getPixels returns a sequence of pixels Each pixel knows its color and place in the original picture Change the pixel and you change the picture So the loops here assign the index variable p to each pixel in the picture picture, one at a time. def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) 06-Intro-Object-Oriented-Prog
Do we need the variable value? No We can calculate the original red amount right when we are ready to change it. It’s a matter of programming style. The meanings are the same. def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) def decreaseRed(pict): for p ingetPixels(pict): setRed(p, getRed(p) * 0.5) 06-Intro-Object-Oriented-Prog
Let’s walk that through slowly… def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) Here we take a picture object in as a parameter to the function and call it pict pict 06-Intro-Object-Oriented-Prog
Now, get the pixels def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) We get all the pixels from the picture, then make p be the name of each one one at a time pict getPixels() Pixel, color r=135 g=131b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … p 06-Intro-Object-Oriented-Prog
Get the red value from pixel def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) We get the red value of pixel p and name it value pict getPixels() Pixel, color r=135 g=131b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … … value = 135 p 06-Intro-Object-Oriented-Prog
Now change the pixel def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) Set the red value of pixel p to 0.5 (50%) of value pict getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 135 p 06-Intro-Object-Oriented-Prog
Then move on to the next pixel def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) Move on to the next pixel and name itp pict getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 135 p 06-Intro-Object-Oriented-Prog
Get its red value Set value to the red value at the new p, then change the red at that new pixel. def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) pict getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 133 p p p 06-Intro-Object-Oriented-Prog
And change this red value def decreaseRed(pict): for p ingetPixels(pict): value=getRed(p) setRed(p,value*0.5) Change the red value at pixel p to 50% of value pict getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=66g=114 b=46 Pixel, color r=134 g=114b=45 … value = 133 p p p 06-Intro-Object-Oriented-Prog