Computer Science 111 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Computer Science 111

Description:

camcorders. Output devices: display screens. printers. Processing: file compression ... To capture an image (via camera or scanner), a continuous range of color ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 23
Provided by: KenLa2
Category:
Tags: computer | science

less

Transcript and Presenter's Notes

Title: Computer Science 111


1
Computer Science 111
  • Fundamentals of Programming I
  • Introduction to Digital Image Processing

2
Digital Images
  • Input devices
  • scanners
  • cameras
  • camcorders
  • Output devices
  • display screens
  • printers
  • Processing
  • file compression
  • various transformations

3
Transformations
Convert from color to grayscale Adjust the
brightness Adjust the contrast Adjust the
size Rotate Morph into another image
4
Morph of the Day
5
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

6
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
7
The Image Class
Image(fileName) Image(width, height) draw() clo
ne() getWidth() getHeight() getPixel(x,
y) setPixel(x, y, (r, g, b)) save(ltoptional
file namegt)
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
8
A Simple Session
gtgtgt from images import Image
Uses a terminal prompt to launch python Import
the relevant class from the module The images
library must be in the current working directory
9
A Simple Session
gtgtgt from images import Image gtgtgt image
Image('smokey.gif')
The image file must be in the current working
directory
10
A Simple Session
gtgtgt from images import Image gtgtgt image
Image('smokey.gif') gtgtgt image.draw()
draw must be run to display the image
11
A Simple Session
gtgtgt from images import Image gtgtgt image
Image('smokey.gif') gtgtgt image.draw() gtgtgt
image.getWidth(), image.getHeight() (300, 225)
The image window must be closed to continue
testing The comma creates a tuple of results
12
A Simple Session
gt python gtgtgt from images import Image gtgtgt image
Image('smokey.gif') gtgtgt image.draw() gtgtgt
image.getWidth(), image.getHeight() (300,
225) gtgtgt image.getPixel(0, 0) (206, 224, 122)
13
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
    pixels three color components to 0s (black)
  • Otherwise, set them to 255s (white)

14
Transformations Black and White
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 lt 128
image.setPixel(x, y, blackPixel)
else image.setPixel(x, y,
whitePixel)
15
Transformations Inversion
  • Should turn black into white or white into black
  • Reset each color component of a pixel to 255
    minus that components value

16
Transformations Inversion
for each pixel in the image subtract each
color component from 255
17
Transformations Grayscale
  • Compute the average of the three color components
    in a pixel
  • Reset each color component of the pixel to this
    average value

18
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))
19
A Better Grayscale Algorithm
  • The simple average of the RGB values does not
    take account of the human retinas 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

20
A Better Grayscale Algorithm
Old
  • red int(red 0.299)
  • green int(green 0.587)
  • blue int(blue 0.114)
  • gray red green blue
  • image.setPixel(x, y, (gray, gray, gray))

New
21
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
22
For Wednesday
  • Finish the reading the handout on image processing
Write a Comment
User Comments (0)
About PowerShow.com