Download presentation
Presentation is loading. Please wait.
1
CMPT 120 Lecture 22 – Unit 4 – Computer Vision
Python – Nested Loops, Tuples and Fruitful Functions
2
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?
3
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]
4
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)
5
Tuples Like a list, but not. Tuples are like read-only lists.
6
Let’s have a closer look at …
Tuples
7
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?
8
How to go through each pixel of A?
Nested loops are useful for traversing 2D tables
9
How to know if a pixel is green?
255 is maximum intensity
10
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]
11
How to find the corresponding pixel in B?
12
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
13
Let’s give this function a try!
… Can you complete this function?
14
Can we improve our program?
Tada!!! Hum… Can we improve our program?
15
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?
16
Next Lecture New practice exercises Bring your laptop!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.