CMPT 120 Lecture 22 – Unit 4 – Computer Vision Python – Nested Loops, Tuples and Fruitful Functions
to find out how to load images Last Lecture, we did … Look at the PIL documentation to find out how to load images and access image pixels. Can you print the values of the pixels in your image? https://repl.it/repls/CookedInconsequentialClient
This is how I access this pixel: Last Lecture, we saw … 2D table called my_image This is how I access this pixel: my_image[10, 8]
Write the code that prints what is at Testing … Testing … What is at coordinates (0,0)? Write the code that prints what is at coordinates (0,0)
Tuples Like a list, but not. Tuples are like read-only lists.
Let’s have a closer look at … Tuples https://repl.it/repls/AutomaticAdventurousTheory
Back to our “combining images” problem … Let’s translate this comment into Python code! A few things to keep in mind: How to go through each pixel of A? How to know if a pixel is green? How to find the corresponding pixel in B?
How to go through each pixel of A? Nested loops are useful for traversing 2D tables
How to know if a pixel is green? 255 is maximum intensity
Two ways to access a pixel tuple's rgb values # Get aPixel at (0,0) aPixel = imageKidGreen[0,0] # Way 1 - Get this pixel’s r value r = aPixel[0] # Way 2 - Get this pixel’s g value g = imageKidGreen[0,0][1] # Way 2 - Get this pixel’s b value b = imageKidGreen[0,0][2]
How to find the corresponding pixel in B?
Fruitful Functions Problem Statement: Write a function that returns True when a pixel is green and False otherwise How would we generalize this function i.e., given a pixel and a colour, the function returns True if the pixel has this colour
Let’s give this function a try! … Can you complete this function?
Can we improve our program? Tada!!! Hum… Can we improve our program?
Review How do you create or initialize a tuple? In an image contained in a table called awesome_image, how would you access the pixel located 5 pixels in, and 8 pixels down from the top left? What can nested loops be used for in the context of image processing? What are 2 differences between tuples and lists? What can computer vision be used for?
Next Lecture New practice exercises Bring your laptop!