computer science 111 fundamentals of programming i more digital image processing

15
Computer Science 111 Fundamentals of Programming I More Digital Image Processing

Upload: mohamed-soares

Post on 14-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

Computer Science 111

Fundamentals of Programming I

More Digital Image Processing

Morph of the Day

Command Line Arguments

$ python3 mondrian.py

Allow users to specify data for a program at a terminal command prompt

$ python3 mondrian.py 5

$ python3 ccurve.py 12

Command Line Arguments

$ python3 mondrian.py 8

sys.argv is a list of strings

The first string is the script file name, and any others are the arguments entered after the file name

import sys

def main(): # Get the level from the command line if len(sys.argv) == 1: level = 0 else: level = int(sys.argv[1])

Command Line Arguments

$ python3 mondrian.py 8

Optional parameter to main allows you to run the program with an argument from the IDLE shell as well as from a terminal prompt

import sys

def main(level = 1): # Get the level from the command line or the shell if len(sys.argv) == 1: level = level else: level = int(sys.argv[1])

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 colorFilterdef colorFilter(image, colorAmount):

def baseValue(value, offset): if offset == 0: return value elif offset < 0: return max(value + offset, 0) else: return min(value + offset, 255)

(r, g, b) = colorAmount 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 function baseValue 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

• Often used after an image is enlarged

• 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

def blur(image): width = image.getWidth() - 1 height = image.getHeight() - 1 newImage = image.clone() for y in range(1, height): for x range(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

The blur Function

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)

For Wednesday

Start Chapter 8