Processing Digital Images Working with Images: Global vs. Local Operations CSC121, Introduction to Computer Programming ORIGINAL IMAGE DIGITAL FILTER digital images are often processed using digital filters digital filters are mathematical functions that operate on the pixels of the image FILTERED IMAGE Global vs. Local Image Processing global operations (filters) perform (potentially) the same basic process on all pixels in the image regardless of their position local operations (filters) perform some process on a pixel based on its features, position, and relations to other pixels Global Image Operations What kind of image operations are global? brightening, darkening contrast, increasing/decreasing ranges of light and dark color corrections, adjustments equalization, expanding proportionally the range of light and dark pixels pseudocoloring, mapping colors to pixel values 1
Color Corrections Equalization original (left) has a color cast that is removed for finished version (right) inset is equalized to reveal an enhanced image with improved contrast Pseudocoloring Global vs. Local Image Processing def changered(picture, amount): for pixel in getpixels(picture): value = getred(pixel) setred(pixel, value * amount) >>>changered(pict, 0.25) original (left) is pseudo- colored (right) to reveal floodplains and agricultural plots along the Mississippi River 2
Global Operations The basic template for a global operation can be written in Python this way def basicglobalfilter(pic): for pixel in getpixels(pic): r = getred(pixel) g = getgreen(pixel) b = getblue(pixel) # DO SOMETHING TO r,g,b HERE setred(pixel,r) setgreen(pixel,g) setblue(pixel,b) Global vs. Local Image Processing What kind of practical applications involve local operations? Global Operations Here is an example converting color to a grayscale version def grayfilter(pic): for pixel in getpixels(pic): r = getred(pixel) g = getgreen(pixel) b = getblue(pixel) lum = (r + g + b) / 3 setred(pixel,lum) setgreen(pixel,lum) setblue(pixel,lum) Example: Image Compositing Problem: Copy top half of a picture to its bottom half sharpening, blurring color replacements, e.g., eliminating red- eye special color effects, e.g., posterizing, sepia tones image compositing, e.g., object removal or addition, cloning objects edge detection etc. 3
Problem: Copy top half of a picture to its bottom half Problem: Copy top half of a picture to its bottom half let s walkthrough the process with a small example imagine a 2D array of pixels with integer values produces an one- dimensional array of pixels in row- order start = 0 stop = 8 counting sequence = 0 7 each pixel from pixels[8] pixels[15] is reassigned the value from pixels[1] pixels[7], respectively 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 4
the copied array back in 2D format Image Processing with Selection global and local image operations may involve selection Python makes choices with the if statement if (some_test): #This Python statement will execute if test is true print This is true! print This will print, either way. Problem: Changing Colors Problem: Changing Colors Suppose that we wanted to change the color of the CS truck from orange to purple Suppose that we wanted to change the color of the CS truck from orange to purple Considerations: standard orange in Python = Color(255, 200, 0) using explore( ), we find that the burnt orange pixels are closer to Color(185, 75, 45) a bright purple = Color(127, 0, 255) 5
Suppose that we wanted to change the color of the CS truck from orange to purple Considerations: the distance( ) function gives us the Euclidean distance between 2 colors in color space, i.e., we look for pixels that are relatively close to burnt orange and change them to purple def turnpurple(): file="/users/jtallen/desktop/cstruck1.jpg" burnt = makecolor(185, 75, 45) # burnt orange color picture = makepicture(file) # make new purple color r = 127 b = 255 g = 0 for px in getpixels(picture): color = getcolor(px) if distance(color, burnt) <75.0: setcolor(px, makecolor(r,g,b)) show(picture) return picture def turnpurple(): file="/users/jtallen/desktop/cstruck1.jpg" burnt = makecolor(185, 75, 45) # burnt orange color picture = makepicture(file) # make new purple color r = 127 b = 255 Python comments g = 0 are signified by the for px in getpixels(picture): # character color = getcolor(px) if distance(color, burnt) <75.0: setcolor(px, makecolor(r,g,b)) show(picture) return picture 6
okay, but, we have a small problem NOTE: the target pixels are in rows lower than 200 other parts of the image are also affected, Local Version def turnpurpleinrange(): burnt = makecolor(185, 75, 45) file="/users/jtallen/desktop/cstruck1.jpg" picture = makepicture(file) # new purple r = 127 b = 255 g = 0 for px in getpixels(picture): y = gety(px) if y >= 200: # pixels lower than row 200 color = getcolor(px) if distance(color, burnt)<75.0: setcolor(px, makecolor(r,g,b)) show(picture) return picture 7