CS 354 Autonomous Robotics
Fall 2020
OpenCV
The purpose of this lab is to gain experience working with the Python bindings for OpenCV.
Resources
Exercises
- Download and read the file cv_demo.py and the file pencils.webm.
Read over
cv_demo.py
and run it in the terminal:python3 cv_demo.py
Or, if you don't have a camera:
Note thatpython3 cv_demo.py pencils.webm
cv_demo.py
is not a ROS node. This is pure python code that can be executed without starting ROS.- On line 49 of cv_demo.py channel 2 is displayed in a window. Can you tell by looking at the displayed image which color channel this is? (Don't assume that the channels are in RGB order.)
- Download the file detect_red.py.
- Complete and test the function
find_reddest_pixel
according to the docstring. You should complete this function by creating a nested for loop to access all pixels in the image. Note that you will need to convert the pixel values to integers. They are stored in the image as 8-bit unsigned integers. Performing arithmetic on 8-bit unsigned integers will always result in 8-bit unsigned integers. Complete and test the function
find_reddest_pixel_fast
according to the docstring. You should complete this function without any loops. (This OpenCV method will be helpful: cv2.minMaxLoc The completed function should be much faster than the previous version.
If You Have Extra Time
- The code in detect_red.py finds a
single red pixel. It might also be useful to label ALL pixels
in the image that exceed some redness threshold. Create a new program
named
mark_red.py
that displays a black and white version of the camera image, where all red pixels are white, and all non-red pixels are black. The examples in the Image Thresholding Tutorial are likely to be useful.
Submitting
Submit your completed version of detect_red.py
through Canvas.