Download presentation
Presentation is loading. Please wait.
1
CMPT 120 Lecture 24 – Unit 4 – Computer Vision
Python – Creating Modules and Introducing Lists of Lists
2
Last Lecture, we got into trouble!
3
… creating an output image
We fixed it by … … creating an output image We did this by calling open() (without chaining a call to load() afterward)
4
open( ) versus load( ) open() gives you access to metadata information about the image such as its width, height, etc. load() can be run once open() has been executed load() gives you access to the pixel values
5
Last Lecture, we also saw …
How to go through each pixel of A? Nested loops are useful for traversing 2D tables -> for loops
6
A Word about Comments! Consider the Python code fragment below, which comment would be the most helpful Comment A or Comment B (assuming imageKidGreen is A)? Comment A: Comment B:
7
Last Lecture, we had a homework!
Write a Python statement to discover if a pixel is green Various ways of doing this: if g == 255 : if g > 230 and g <= 255 : if r < 180 and r >= 0 and g > 230 and g <= 255 and b < 120 and b >= 0 : How do we figure this out?
8
How to find the corresponding pixel in B?
9
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 colour, the function returns True if the given pixel has this colour
10
Let’s give this function a try!
… Can you complete this function?
11
Let’s make our own Module!
A module’s name is its filename with the .py removed Create a new file Paste your function definitions in here, and remove them from your main program
12
Let’s use our own Module!
In the main program … Let’s use our own Module! Import the module … Use the module’s name when calling its functions
13
Let’s have another look at lists
14
Lists – so far We saw that a list can contain elements of various data types prices = [1.20, 0.75, 4.50] names = ["Mike", "Xinghua", "Lise"] somePrimes = [1, 3, 5, 7, 11, 13] underTheBed = [3, "old socks"] What if a list contains lists? stdInfo = ["Mike", [112, "B Street"], "YVR"] stdGrades = [ [3,4.5,4], [3.5,5,"-"] ]
15
Creating lists Here are ways of creating lists:
16
Another way of creating a list
List comprehension Example 1: max = 5 list1 = ["*" for number in range(max)] Example 2: length = 4 list2 = [number for number in range(length)] Example 3: operandList = ["4", "5"] operandList = [int(operandList[i]) for i in range(len(operandList))]
17
Another 2D Data Structure: Lists of Lists
Question: What if a problem statement ask us to manipulate a matrix? Answer: In our solution, we could use a list of lists to represent a matrix the Python code representing the above data would look like: myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
18
Access elements in a list of lists
myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
19
Modify elements in a list of lists
myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
20
Slicing a list of lists myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
21
What else could a list of lists represent in our programs?
22
Next Lecture Practice Exam 6 Bring your laptop!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.