110 likes | 208 Views
Computer Science 111. Fundamentals of Programming I More Digital Image Processing. Morph of the Day. Lightening and Darkening. Higher RGB values generally produce a brighter image, whereas lower RGB values produce a darker image
E N D
Computer Science 111 Fundamentals of Programming I More Digital Image Processing
Lightening and Darkening • Higher RGB values generally produce a brighter image, whereas lower RGB values produce a darker image • Changing the RGB values of each pixel by the same amount will lighten or darken an image • The functions lighten and darken expect an image and an integer factor as arguments and modify the brightness or darkness of the image by that amount lighten(image, 20)
Lightening and Darkening • Higher RGB values generally produce a brighter image, whereas lower RGB values produce a darker image • Changing the RGB values of each pixel by the same amount will lighten or darken an image • The functions lighten and darken expect an image and an integer factor as arguments and modify the brightness or darkness of the image by that amount darken(image, 20)
Color Filtering • Lightening and darkening are special cases of color filtering • A color filter is a triple of RGB factors that is applied to each pixel to change the overall color of an image in a consistent way • Thus, the triple (0, 0, 0) indicates no change, whereas the triple (22, -10, 100) adds those amounts to the RGB values of each pixel • The image on the right shows the result of enhancing just the red value with the triple (20, 0, 0)
Using a Color Filter colorFilter(image, (20, 0, 0)) # Increase red colorFilter(image, (0, 0, -20)) # Decrease blue def lighten(image, amount): colorFilter(image, (amount, amount, amount)) def darken(image, amount): colorFilter(image, (-amount, -amount, -amount)) The functions lighten and darken also use colorFilter, where the amounts in the triples are equal
Defining colorFilter def colorFilter(image, (r, g, b)): def baseValue(value, offset): if offset == 0: return value elif offset < 0: return max(value + offset, 0) else: return min(value + offset, 255) for … for … (red, green, blue) = image.getPixel(x, y) (red, green, blue) = (baseValue(red, r), baseValue(green, g), baseValue(blue, b)) image.setPixel(x, y, (red, green, blue)) The nested functionbaseValue keeps the result of the addition or subtraction within the allowable range (0-255)
Blurring an Image • Helps to remove raggedy edges (pixelation) in some images by softening them • Build a new image in which each pixel contains the average of the RGB values of it and its neighboring pixels • Begin with position (1, 1) and end with position (width - 2, height - 2) • Won’t alter the pixels on the perimeter, but then we won’t have to check for the perimeter as we traverse the image
The blur Function def blur(image): width = image.getWidth() - 1 height = image.getHeight() - 1 newImage = image.clone() for y in xrange(1, height): for x xrange(1, width): get the current pixel and its 4 neighbors from image create a new tuple with the averages of r, g, b set the pixel at (x, y) in newImage to the result return newImage This function creates and returns a new image
Edge Detection • Shows what a sketch of an image would look like • Examine neighbors below and to the left of each pixel • If the luminance of the pixel differs from either of these neighbors by a significant amount, we have detected an edge, so set its color to black • Otherwise, set that pixel’s color to white • Function detectEdges expects the image and a difference threshold as arguments and returns a new black and white image detectEdges(image, 20)
Shrinking an Image • Make a copy whose width and height are a percentage of the width and height of the original image • Transfer color values from the original to the copy, skipping rows and columns as needed shrink(image, 0.5)