Computer Vision with Python: Nested Loops, Tuples, and Functions

cmpt 120 n.w
1 / 16
Embed
Share

Explore the world of computer vision using Python with a focus on nested loops, tuples, and fruitful functions. Learn how to access and manipulate image pixels, work with 2D tables, and identify pixel colors. Dive into the details of tuples as read-only lists and discover ways to access pixel RGB values effectively. Get hands-on examples and tips for combining images and traversing through pixel data step by step.

  • Python
  • Computer Vision
  • Nested Loops
  • Tuples
  • Image Processing

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CMPT 120 Lecture 22 Unit 4 Computer Vision Python Nested Loops, Tuples and Fruitful Functions

  2. 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? 2 https://repl.it/repls/CookedInconsequentialClient

  3. Last Lecture, we saw 2Dtable called my_image This is how I access this pixel: my_image[10, 8] 3

  4. Testing Testing What is at coordinates (0,0)? Write the code that prints what is at coordinates (0,0) 4

  5. Tuples Like a list, but not. Tuples are like read-only lists. 5

  6. Lets have a closer look at Tuples https://repl.it/repls/AutomaticAdventurousTheory 6

  7. Back to our combining images problem Let s translate this comment into Python code! A few things to keep in mind: 1) How to go through each pixel of A? 2) How to know if a pixel is green? 3) How to find the corresponding pixel in B? 7

  8. How to go through each pixel of A? Nested loops are useful for traversing 2D tables 8

  9. How to know if a pixel is green? 255 is maximum intensity 9

  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] 10

  11. How to find the corresponding pixel in B? 11

  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 12

  13. Lets give this function a try! Can you complete this function? 13

  14. Tada!!! Hum Can we improve our program? 14

  15. Review 1. How do you create or initialize a tuple? 2. 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? 3. What can nested loops be used for in the context of image processing? 4. What are 2 differences between tuples and lists? 5. What can computer vision be used for? 15

  16. Next Lecture New practice exercises Bring your laptop! 16

More Related Content