1 / 20

COMPSCI 101 S1 2014 Principles of Programming

COMPSCI 101 S1 2014 Principles of Programming. 30 Image processing. Recap. Some Key Image Functions pickAFile () writePictureTo (picture , filename ) makeEmptyPicture ( height,width ) show(picture ) getPixel ( picture,x,y ) getPixels (picture ) getHeight (picture ) getWidth (picture).

thor
Download Presentation

COMPSCI 101 S1 2014 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101 S1 2014Principles of Programming 30 Image processing

  2. Recap • Some Key Image Functions • pickAFile() • writePictureTo(picture, filename) • makeEmptyPicture(height,width) • show(picture) • getPixel(picture,x,y) • getPixels(picture) • getHeight(picture) • getWidth(picture) • makeColor(r, g, b) • makeLighter(color) • makeDarker(color) • getColor(pixel) • setColor(pixel, color) • getRed(pixel) • setRed(pixel, r) • getGreen(pixel) • setGreen(pixel, r) • getBlue(pixel) • setBlue(pixel, r) COMPSCI101

  3. Recap: Using the ImageLibrary Module • The ImageLibraryModule • The module allows students to load, transform, and save digital images. • You must install pygamein order to use this module. • http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame • How to use it? • Download the file (ImageLibrary.py) from compsci101 course website and save it in your working directory • Add an import statement in your py file. • Download our sample images to your working directory • Picture class represents a picture as a two-dimensional grid of RGB values We have installed pygame only to the machines in room 279! from ImageLibrary import * COMPSCI101

  4. Learning outcomes • At the end of this lecture, students should be able to: • Manipulate images using a provided image library • Using a list of Pixels • Using a nested loop structure • Examples and Exercises: • Case Study 1: Reducing the Red Colour • Case Study 2: Eliminating Colours • Exercise 1: Lightening an Image • Case Study 3: Mirroring an Image horizontally • Case Study 4: Adding Borders • Exercise 2: Mirroring an Image vertically COMPSCI101

  5. A Loop Pattern for Traversing a Grid • There are two approaches to move through the pixels • Using a list of Pixel objects • Using one pixel at a time based on a row and column value in a nested loop structure • Many image-processing algorithms use a nested loop structure to traverse a two-dimensional grid of pixels • Why are you learning both approaches? • To "flip" an image, it is easier to access individual pixels in specific rows and columns. • If you are making global changes in colourto your entire image, you can grab all of the pixels (in a list). COMPSCI101

  6. Simple Manipulation of Images for pixel in getPixels(pict): apply_a_function(pixel) • Using a list of Pixels • Case Study 1: Reducing the Red Colour • Apply a global change in colourto the entire image • E.g. reduce the red component by half • Exercise 1: Lightening • Apply a global change in colourto the entire image • E.g. add “40” to all colour components • Using a nested loop structure • Case Study 3: Mirroring an Image horizontally • The left-hand side of the image is mirrored onto the right-hand side. • i.e. We are taking half the picture and flipping it. • Exercise 2: Mirroring an Image vertically for x in range(width): for y in range(height): pixel = getPixel(pict, x, y) apply_a_function(pixel) COMPSCI101

  7. Case Study 1Reducing the Red Colour • Task: • Complete the reduce_red() function which takes a pixel as a parameter and reduces the red component by half. • Arguments: pixel • Example: pict = makePicture("caterpillarSmall.jpg") for pixel in getPixels(pict): reduce_red(pixel) COMPSCI101

  8. Case Study 1Reducing the Red Colour Demo 1 • Algorithm COMPSCI101

  9. Case Study 2Eliminating Colours • Task: • Complete the to_red() function which takes a pixel as a parameter and eliminates all colour components except red • Arguments: pixel • Example: • RGB of the first pixel (113, 123, 60) pict = makePicture("caterpillarSmall.jpg") for pixel in getPixels(pict): to_red(pixel) • (113, 0, 0) COMPSCI101

  10. Case Study 2Eliminating Colours Demo 2 • Algorithm COMPSCI101

  11. Exercise1Lightening • Task: • Complete the following main() function which lightens the picture. • The image library has a function called makeLighter(color) that lightens each part of the colour- in fact what it does is subtractadd 40 to the red, green and blue values • Example: • (153, 163, 100) • (113, 123, 60) def main(): pict = makePicture("caterpillarSmall.jpg") for pixel in getPixels(pict): show(pict) COMPSCI101

  12. Exercise1Lightening Ex 1 • Algorithm COMPSCI101

  13. Case Study 3Mirroring an Image horizontally • Task: • Complete the mirror_horizontally() function which takes a picture as a parameter. The left-hand side of the picture is mirrored onto the right-hand side. • Arguments: A picture • Example: pict = makePicture("caterpillarSmall.jpg") mirror_horizontally(pict) COMPSCI101

  14. Case Study 3Mirroring an Image horizontally • The left-hand side is mirrored onto the right-hand side. The pixel is "flipped" to look like a reflection. COMPSCI101

  15. Case Study 3Mirroring an Image horizontally • Algorithm How? COMPSCI101

  16. Case Study 3Mirroring an Image horizontally Demo 3 • Algorithm COMPSCI101

  17. Exercise 2Mirroring an Image Vertically • Can you adapt the mirror_horizontally() to mirror a picture vertically? • Task: • Complete the mirror_vertically() function which takes a picture as a parameter. The top side of the picture is mirrored onto the bottom side. • Arguments: A picture COMPSCI101

  18. Case Study 4Adding Borders • Task: • Complete the add_two_borders() function which takes a picture as a parameter. The function should add a top and bottom 5-pixel thick borders to the image • Arguments: A picture • Example: pict = makePicture("caterpillarSmall.jpg") add_two_borders(pict) COMPSCI101

  19. Case Study 4Adding Borders Demo 4 • Adding a top border: Adding a bottom border COMPSCI101

  20. Summary • Some Key Image Functions • pickAFile() • writePictureTo(picture, filename) • makeEmptyPicture(height,width) • show(picture) • getPixel(picture,x,y) • getPixels(picture) • getHeight(picture) • getWidth(picture) • makeColor(r, g, b) • makeLighter(color) • makeDarker(color) • getColor(pixel) • setColor(pixel, color) • getRed(pixel) • setRed(pixel, r) • getGreen(pixel) • setGreen(pixel, r) • getBlue(pixel) • setBlue(pixel, r) COMPSCI101

More Related