computer science 111 fundamentals of programming i introduction to digital image processing
TRANSCRIPT
![Page 1: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/1.jpg)
Computer Science 111
Fundamentals of Programming I
Introduction to Digital Image Processing
![Page 2: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/2.jpg)
Digital Images
• Input devices: – scanners – cameras – camcorders
• Output devices: – display screens – printers
• Processing: – file compression– various transformations
![Page 3: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/3.jpg)
Transformations
Convert from color to grayscale
Adjust the brightness
Adjust the contrast
Adjust the size
Rotate
Morph into another image
![Page 4: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/4.jpg)
Morph of the Day
![Page 5: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/5.jpg)
Representing Images
• An image can be represented as a two-dimensional grid of RGB values (pixels)
• To capture an image (via camera or scanner), a continuous range of color info is sampled as a set of discrete color values
• All processing works with the grid of RGB values
• Output maps the grid to a display or a printer
![Page 6: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/6.jpg)
The images Module
• A non-standard, open source module that includes a set of classes and methods for processing images
• Can edit scripts in IDLE, then run them from IDLE or a terminal prompt
python testimages.py
![Page 7: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/7.jpg)
The Image ClassImage(fileName)
Image(width, height)
draw()
clone()
getWidth()
getHeight()
getPixel(x, y)
setPixel(x, y, (r, g, b))
save(<optional file name>)
Represents a grid of pixels
Methods for display, examining the dimensions, examining or resetting pixels, and saving changes to a file
A pixel is just a tuple of 3 integers
![Page 8: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/8.jpg)
A Simple Session
Uses a terminal prompt to launch python
Import the relevant class from the module
The images library must be in the current working directory
>>> from images import Image
![Page 9: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/9.jpg)
A Simple Session
The image file must be in the current working directory
>>> from images import Image
>>> image = Image('smokey.gif')
![Page 10: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/10.jpg)
A Simple Session
draw must be run to display the image
>>> from images import Image
>>> image = Image('smokey.gif')
>>> image.draw()
![Page 11: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/11.jpg)
A Simple Session
>>> from images import Image
>>> image = Image('smokey.gif')
>>> image.draw()
>>> image.getWidth(), image.getHeight()(300, 225)
The image window must be closed to continue testing
The comma creates a tuple of results
![Page 12: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/12.jpg)
A Simple Session
> python
>>> from images import Image
>>> image = Image('smokey.gif')
>>> image.draw()
>>> image.getWidth(), image.getHeight()(300, 225)
>>> image.getPixel(0, 0)(206, 224, 122)
![Page 13: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/13.jpg)
Transformations: Black and White
• Compute the average of the three color components in a pixel
• If the average is less than 128, then set the pixel’s three color components to 0s (black)
• Otherwise, set them to 255s (white)
![Page 14: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/14.jpg)
blackPixel = (0, 0, 0) whitePixel = (255, 255, 255) for y in xrange(image.getHeight()): for x in xrange(image.getWidth()): (r, g, b) = image.getPixel(x, y) average = (r + g + b) / 3 if average < 128: image.setPixel(x, y, blackPixel) else: image.setPixel(x, y, whitePixel)
Transformations: Black and White
![Page 15: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/15.jpg)
Transformations: Inversion
• Should turn black into white or white into black
• Reset each color component of a pixel to 255 minus that component’s value
![Page 16: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/16.jpg)
for each pixel in the image: subtract each color component from 255
Transformations: Inversion
![Page 17: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/17.jpg)
Transformations: Grayscale
• Compute the average of the three color components in a pixel
• Reset each color component of the pixel to this average value
![Page 18: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/18.jpg)
Transformations: Grayscale
for y in xrange(image.getHeight ()) for x in xrange(image.getWidth()): (r, g, b) = image.getPixel(x, y) ave = (r + g + b) / 3 image.setPixel(x, y, (ave, ave, ave))
![Page 19: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/19.jpg)
A Better Grayscale Algorithm
• The simple average of the RGB values does not take account of the human retina’s different sensitivities to the luminance of those values
• The human eye is more sensitive to green, then red, and finally blue
• Psychologists have determined the exact sensitivities
• Multiply each value by a weight factor and then add them up
![Page 20: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/20.jpg)
A Better Grayscale Algorithm
red = int(red * 0.299)green = int(green * 0.587)blue = int(blue * 0.114)gray = red + green + blueimage.setPixel(x, y, (gray, gray, gray))
Old
New
![Page 21: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/21.jpg)
Package Code in a Function
def grayScale(image): for y in xrange(image.getHeight ()): for x in xrange(image.getWidth()): (r, g, b) = image.getPixel(x, y) ave = (r + g + b) / 3 image.setPixel(x, y, (ave, ave, ave))
Note that this function does not return a new image, but modifies its argument
Until now, functions did not modify arguments
This is the most efficient way of modifying large data objects
![Page 22: Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1c5503460f94c32182/html5/thumbnails/22.jpg)
For Wednesday
Finish the reading the handout on image processing