260 likes | 408 Views
Chap. 6 Pictures. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To implement a number of image processing algorithms To understand passing functions as parameters. Color. Color is continuous
E N D
Chap. 6 Pictures • Objectives • To understand pixel based image processing • To use nested iteration • To use and understand tuples • To implement a number of image processing algorithms • To understand passing functions as parameters
Color • Color is continuous • Visible light is in the wavelengths of 370 and 730 nm (0.00000037 and 0.00000073 meters) • We perceive colors differently
Color Perception • Human perception of light • With three color sensors • Peaks at 425 nm (blue), 550 nm (green), and 660 nm (red) • Our brain figures out color by how much of each sensor is responding • Dogs and other simpler animals have two sensors • They do see color. Just less color
Luminance vs. Color • Luminance is a measure of light intensity • Luminance allows us to perceive borders of things, motion, depth • Luminance perception is color blind. • Brightness is our perception of luminance • brightness 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. • White’s illusion • Same gray luminance • Appears brighter in • black stripe • Different parts of the brain perceive color and luminance.
Digital Pictures • Digitized as a bunch of dots (squares) • With enough dots, it looks continuous • Our eyes have limited resolution • Our background/depth acuity is particulary low • Each picture element is referred to as a pixel
Pixels • Pixels are picture elements • Each pixel object “knows” its color • e.g. given a pixel, a Python function can get the color out of it. • It also “knows” where it is in its picture • e.g. given a pixel and a picture, a Python function can find out where the pixel is located in the picture • A picture is a matrix of pixels • With rows of pixels as arrays • With two dimensions: Width and Height • We need a two-dimensional array: a matrix
Digital Image Processing • Editing and manipulating digital images • A digital image is a collection of pixels • A pixel is the smallest amount of information available in a digital picture • Each pixel represents a single color • Pixels are organized in a grid
What is a Binary number ? • What is a decimal number ? • Digits 0, 1, 2, …, 9 • What does a decimal number 213 mean ? • Digit position has a weight associated with it • Binary ? • 0,1 • What is 1010 ? • Other number systems (bases) ? • 12: month, foot/inch, • 16: fluid oz • 60
3-bit Binary Number • Binary Number System
Decimal Binary Hex 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 4-bit Binary Number • Hexadecimal • Base 16 number representation • Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’
8-bit Binary Number • Byte = 8 bits • Binary 000000002 to 111111112 • Decimal: 010 to 25510 • Hexadecimal 0016 to FF16
Color Encoding • Each pixel encodes color at that position in the picture • Most common for computers • RGB (Red, Green, Blue) • 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 • lEach color component has a value between 0 and 255 • Total RGB color is 24 bits
Encoding RGB • Colors go from (0,0,0) to (255,255,255) • If all components have the same values -> grayscale • (50,50,50) at (2,2) • (0,0,0) at (1,2) is black • (255,255,255) is white • Colors are represented in 24 bits • That is 16,777,216 (224) possible colors • Our eyes can discern millions of colors -> close • But, we don’t get 16 million colors from computer monitors
JES Tool Python development tool developed by Georgia Tech Download and install JES at C:/100 https://github.com/gatech-csl/jes
Download media files • Create a new folder C:\100\pics • Download www.cs.uml.edu/~kim/100/pics.zip • Save into C:\100\pics • Right click and select ‘Extract here.” • Under C:\100, you should have • C:\100\jes-4-2-1-windows • C:\100\pics
Python/JES • JES IDE (Integrated Development Environment) • Incorporates editing environment • Program pane • Command pane • Watcher button to view debugging
What is a Picture in JES ? • A picture object in JES • Is an encoding that represents a picture • Knows its height and width • Knows how many pixels it has in both directions • Knows its file name • A picture is not a file • Only when you call makePicture(), it becomes a picture • But it knows which file it came from • Knows its window when opened by show() or repaint()
Picture Functions in JES • pickAFile() opens a file browser to select a file • makePicture(filename) creates and returns a picture object, from the JPEG file at the filename • show(pic) displays a picture in a window • repaint(picture) to re-display after changing it FileImage() pic.draw()
Other Pixel Functions • getPixel(pic,x,y) to get a single pixel ----- pic.getPixelt(x,y) • getRed, getGreen, and getBlue are functions that • return a color value (between 0 and 255) at a specified pixel • setRed, setGreen, and setBlue are functions that • set its color value at a specified pixel • We can also get, set, and make colors • getColor returns a Color object with three color values at a pixel • setColor sets the pixel to the specified color • makeColor returns a Color object with specified three color values • pickAColor lets you use a color chooser and returns the chosen color
In Python def negativeImg(img): width = img.getWidth() height = img.getHeight() for col in range(0,height): for row in range(0,width): newColor = negativePixel(img.getPixel(row, col)) img.setPixel(row, col, newColor) return img def negativePixel(oldPixel): newre = 255 –oldPixel.getRed() newgreen = 255 - oldPixel.getGreen() newblue = 255 - oldPixel.getBlue() newPixel = Pixel(newred, newgreen, newblue) return newPixel
Example >>> thisPixel = getPixel(myPic,1,1) >>> print thisPixel Pixel, color=color r=168 g=131 b=105 # get/set individual color values >>> print getRed(thisPixel) 168 >>> setRed(thisPixel,255) >>> print getRed(thisPixel) 255 >>> color=getColor(thisPixel) >>> print color color r=255 g=131 b=105 >>> setColor(thisPixel,color) >>> newColor=makeColor(0,100,0) >>> print newColor color r=0 g=100 b=0 >>> setColor(thisPixel,newColor) >>> print getColor(thisPixel) color r=0 g=100 b=0 >>> print color color r=168 g=131 b=105 >>> print makeDarker(color) color r=117 g=91 b=73 >>> print color color r=117 g=91 b=73 >>> newcolor=pickAColor() >>> print newcolor color r=255 g=51 b=51
Change Colors Directly >>> file=“C:/mediasources/barbara.jpg" >>> pict=makePicture(file) >>> show(pict) >>> setColor(getPixel(pict,10,100),yellow) >>> setColor(getPixel(pict,11,100),yellow) >>> setColor(getPixel(pict,12,100),yellow) >>> setColor(getPixel(pict,13,100),yellow) >>> repaint(pict)
A Negative def negative(pic): for row in range(0,getWidth(pic)): for col in range(0, getHeight(pic)): pix = getPixel(pic, row, col) red=getRed(pix) green=getGreen(pix) blue=getBlue(pix) negColor=makeColor( 255-red, 255-green, 255-blue) setColor(pix,negColor) return pic
Greyscale def greyScale(pic): for row in range(0,getWidth(pic)): for col in range(0, getHeight(pic)): p = getPixel(pic, row, col) intensity = (getRed(p)+getGreen(p)+getBlue(p))/3 gray = makeColor(intensity,intensity,intensity)) setColor(px,gray) return picture
Reduce Red Colors in Every Pixel • Process (Recipe) • Given a picture • For each row • For every pixel in the row • Get what red value it has • Reduce its value by x % • Replace (set) the red value by the reduced one Def increaseRed(): pic= makePicture(pickAFile()) for col in range(1,getHeight(picture)): for row in range(1, getWidth(picture)): thisPix = getPixel(pic, row, col) redVal=getRed(thisPix) setRed(thisPix,redVal*1.2)
A Sunset Effect • For each row, increase red in every pixel => too red • For each pixel, decrease both green and blue def makeSunset(pic): ratio = 0.8 for row in range(0,getWidth(pic)): for col in range(0, getHeight(pic)): thisPixel = getPixel(pic, row, col) pixG=getGreen(thisPixel) pixB=getBlue(thisPixel) setGreen(thisPixel,pixG*ratio) setBlue(thisPixel,pixB*ratio) return pic