CMPT 120 Machine that can see! Lecture 21 – Unit 4 – Computer Vision Python – Files, Nested Loops and Tuples
Unit 4 - Computer Vision We can make machines understand our world visually Facebook’s automatic photo captions for the blind iPhone X recognizes faces Instagram filters Medical imaging Gesture recognition New Visual Computing M.Sc.
Unit 4 - Computer Vision APPLICATIONS In this unit, we’ll learn about the computing science field of computer vision, that allows machines to process images and understand them ALGORITHMS We’ll learn about more advanced functions, nested (embedded) loops, etc… Python We’ll learn about tuples, file input/output (I/O), etc…
Puzzle 1 What would the Python code fragment below output? import random def random_animal(animals): default = "cat" animal = random.choice(animals) return animal # Main part of program print(default) print(random_animal(["dog", "bird"]))
Puzzle 2 What would the Python code fragment below output? def special(numbers): for num1 in numbers: for num2 in numbers: if num1 < num2: return num1 + num2 # Main part of program print(special([3,1,2]))
Image Processing
Combining Images
This week, we shall … … make a program that can merge a green screen image with another background, using the following concepts: Importing a package module using from … import Looking up and understanding module documentation Pixel representations of images in 2D arrays, e.g. image[x,y] Colours in RGB space -> we already know this! Review functions that return values (e.g. boolean and string)
Let’s get started! Problem Statement: Write a program that combines two images ----------------------------------------------------------------------- Let’s get started by downloading these two files: kid-green.jpg from https://goo.gl/cju79e beach.jpg from https://goo.gl/nCgxdQ
Working with images in Repl.It Upload image files to Repl.it here
For the sake of simplicity, let’s use the same sized image here x <--- 450 pixels --> <--- 450 pixels --> y For the sake of simplicity, let’s use the same sized image here
module inside the Python Imaging Let’s get started! Image is a module inside the Python Imaging Library (PIL) package, which is a collection of modules. The Image module contains pre-built functions that will help us do image processing. https://repl.it/repls/CookedInconsequentialClient
to find out how to load images Opening Image Files 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
Accessing each pixel This is a 2-dimensional (2D) table containing our image (in other programming languages, sometimes it is called a 2D array) Accessing each pixel You can access the value of this 2D table element (pixel) similar to list access: my_image[c, r] E.g. image_green[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
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 all pixels of A? How to know if a pixel is green? How to find the corresponding pixel in B?
How to go through all pixels of A?
How to know if a pixel is green? 255 is maximum intensity
How to find the corresponding pixel in B?
Next Lecture Let’s have an other look at our program! Can we improve it?