python programming in context chapter 6. objectives to understand pixel based image processing to...
TRANSCRIPT
![Page 1: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/1.jpg)
Python Programming in Context
Chapter 6
![Page 2: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/2.jpg)
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
![Page 3: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/3.jpg)
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
![Page 4: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/4.jpg)
RGB Color Model
• RGB means RED, GREEN, BLUE• Each color is made up from amounts of red,
green and blue• Color intensities range from a minimum of 0
to a maximum of 255
![Page 5: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/5.jpg)
cImage module
• Pixel• ImageWin• EmptyImage• FileImage
![Page 6: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/6.jpg)
Pixel objects
• getRed• getGreen• getBlue• setRed• setGreen• setBlue
![Page 7: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/7.jpg)
FileImage and EmptyImage
• getWidth• getHeight• getPixel• setPixel• draw
![Page 8: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/8.jpg)
Negative Image
• Reverse the colors in each pixel
![Page 9: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/9.jpg)
Listing 6.1
def negativePixel(oldPixel): newred = 255 - oldPixel.getRed() newgreen = 255 - oldPixel.getGreen() newblue = 255 - oldPixel.getBlue() newPixel = Pixel(newred, newgreen, newblue) return newPixel
![Page 10: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/10.jpg)
Figure 6.1
![Page 11: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/11.jpg)
Figure 6.2
![Page 12: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/12.jpg)
Listing 6.2def makeNegative(imageFile): myimagewindow = ImageWin("Image Processing",600,200) oldimage = FileImage(imageFile) oldimage.draw(myimagewindow)
width = oldimage.getWidth() height = oldimage.getHeight() newim = EmptyImage(width,height)
for row in range(height): for col in range(width): originalPixel = oldimage.getPixel(col,row) newPixel = negativePixel(originalPixel) newim.setPixel(col,row,newPixel)
newim.setPosition(width+1,0) newim.draw(myimagewindow) myimagewindow.exitOnClick()
![Page 13: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/13.jpg)
Figure 6.3
![Page 14: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/14.jpg)
Figure 6.4
![Page 15: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/15.jpg)
Grayscale Image
• Convert each pixel to a shade of gray• Use average of RGB values
![Page 16: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/16.jpg)
Listing 6.3def grayPixel(oldpixel): intensitySum = oldpixel.getRed() + oldpixel.getGreen() + oldpixel.getBlue() aveRGB = intensitySum // 3
newPixel = Pixel(aveRGB,aveRGB,aveRGB) return newPixel
![Page 17: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/17.jpg)
Listing 6.4def makeGrayScale(imageFile): myimagewindow = ImageWin("Image Processing",600,200) oldimage = FileImage(imageFile) oldimage.draw(myimagewindow)
width = oldimage.getWidth() height = oldimage.getHeight() newim = EmptyImage(width,height)
for row in range(height): for col in range(width): originalPixel = oldimage.getPixel(col,row) newPixel = grayPixel(originalPixel) newim.setPixel(col,row,newPixel)
newim.setPosition(width+1,0) newim.draw(myimagewindow) myimagewindow.exitOnClick()
![Page 18: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/18.jpg)
Figure 6.5
![Page 19: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/19.jpg)
Generalize Pixel Mapping
• Create a function that takes a pixel manipulation function as a parameter
![Page 20: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/20.jpg)
Figure 6.6
![Page 21: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/21.jpg)
Figure 6.7
![Page 22: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/22.jpg)
Figure 6.8
![Page 23: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/23.jpg)
Listing 6.5def pixelMapper(oldimage,rgbFunction):
width = oldimage.getWidth() height = oldimage.getHeight() newim = EmptyImage(width,height)
for row in range(height): for col in range(width): originalPixel = oldimage.getPixel(col,row) newPixel = rgbFunction(originalPixel) newim.setPixel(col,row,newPixel) return newim
![Page 24: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/24.jpg)
Listing 6.6
def generalTransform(imageFile): myimagewindow = ImageWin("Image Processing",600,200) oldimage = FileImage(imageFile) oldimage.draw(myimagewindow) newimage = pixelMapper(oldimage,grayPixel) newimage.setPosition(oldimage.getWidth()+1,0) newimage.draw(myimagewindow) myimagewindow.exitOnClick()
![Page 25: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/25.jpg)
Namespaces
• Parameters, Parameter Passing, and Scope
• How are the names and object organized in Python
• Builtin• Main• Local
![Page 26: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/26.jpg)
Listing 6.7
import math def hypotenuse(a,b): c = math.sqrt(a**2 + b**2) return c
![Page 27: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/27.jpg)
Figure 6.9
![Page 28: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/28.jpg)
Figure 6.10
![Page 29: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/29.jpg)
Figure 6.11
![Page 30: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/30.jpg)
Figure 6.12
![Page 31: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/31.jpg)
Figure 6.13
![Page 32: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/32.jpg)
Enlarging an Image
• Stretching an image• Mapping pixels from original to enlargement
![Page 33: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/33.jpg)
Figure 6.14
![Page 34: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/34.jpg)
Figure 6.15
![Page 35: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/35.jpg)
Figure 6.16
![Page 36: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/36.jpg)
Listing 6.8def double(oldimage): oldw = oldimage.getWidth() oldh = oldimage.getHeight()
newim = EmptyImage(oldw*2,oldh*2)
for row in range(oldh): for col in range(oldw): oldpixel = oldimage.getPixel(col,row) newim.setPixel(2*col,2*row,oldpixel) newim.setPixel(2*col+1,2*row,oldpixel) newim.setPixel(2*col,2*row+1,oldpixel) newim.setPixel(2*col+1,2*row+1,oldpixel)
return newim
![Page 37: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/37.jpg)
Figure 6.17
![Page 38: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/38.jpg)
Figure 6.18
![Page 39: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/39.jpg)
Listing 6.9def double(oldimage): oldw = oldimage.getWidth() oldh = oldimage.getHeight()
newim = EmptyImage(oldw*2,oldh*2)
for row in range(newim.getHeight()): for col in range(newim.getWidth()): originalCol = col//2 originalRow = row//2 oldpixel = oldimage.getPixel(originalCol,originalRow)
newim.setPixel(col,row,oldpixel)
return newim
![Page 40: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/40.jpg)
Figure 6.19
![Page 41: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/41.jpg)
Flipping an Image
• Moving pixels to a new location• Flip axis
![Page 42: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/42.jpg)
Figure 6.20
![Page 43: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/43.jpg)
Listing 6.10def verticalFlip(oldimage): oldw = oldimage.getWidth() oldh = oldimage.getHeight()
newim = EmptyImage(oldw,oldh)
maxp = oldw -1 for row in range(oldh): for col in range(oldw): oldpixel = oldimage.getPixel(maxp-col,row)
newim.setPixel(col,row,oldpixel)
return newim
![Page 44: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/44.jpg)
Figure 6.21
![Page 45: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/45.jpg)
Edge Detection
• Convert to grayscale• Look for changes from light to dark or dark to
light
![Page 46: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/46.jpg)
Figure 6.22
![Page 47: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/47.jpg)
Figure 6.23
![Page 48: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/48.jpg)
Figure 6.24
![Page 49: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/49.jpg)
Listing 6.11def convolve(anImage,pixelRow,pixelCol,kernel): kernelColumnBase = pixelCol - 1 kernelRowBase = pixelRow - 1 sum = 0 for row in range(kernelRowBase,kernelRowBase+3): for col in range(kernelColumnBase,kernelColumnBase+3): kColIndex = col-kernelColumnBase kRowIndex = row-kernelRowBase apixel = anImage.getPixel(col,row) intensity = apixel.getRed() sum = sum + intensity * kernel[kRowIndex][kColIndex] return sum
![Page 50: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/50.jpg)
Listing 6.12import math
def edgeDetect(theImage): grayImage = pixelMapper(theImage,grayPixel) newim = EmptyImage(grayImage.getWidth(), grayImage.getHeight()) black = Pixel(0,0,0) white = Pixel(255,255,255) xMask = [ [-1,-2,-1],[0,0,0],[1,2,1] ] yMask = [ [1,0,-1],[2,0,-2],[1,0,-1] ]
for row in range(1,grayImage.getHeight()-1): for col in range(1,grayImage.getWidth()-1): gx = convolve(grayImage,row,col,xMask) gy = convolve(grayImage,row,col,yMask) g = math.sqrt(gx**2 + gy**2) if g > 175: newim.setPixel(col,row,black) else: newim.setPixel(col,row,white)
return newim
![Page 51: Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To](https://reader035.vdocuments.net/reader035/viewer/2022062222/5697bfc31a28abf838ca539c/html5/thumbnails/51.jpg)
Figure 6.25