Overview
In this activity you will practice with using the findContours function and the various other functions that operate on contours. A contour, remember, is a closed polygon that lies along an edge in an image: a series of pixel coordinates that outline an edge in an image.
The Github repository for this assignment will contain a starter code file, activ14.py. Put your code in this file, and create others, as directed below and according to the TODO comments in the file.
Using findContours on threshold images
In an earlier activity, you tried the threshold function and the inRange function to isolate coins and balls in still images (and in video as well).
Examine the program called findPink in activ14.py. This program is designed to find the bright pink lacrosse ball from my gray bag of computer vision supplies. The program relies on two tuples, pinkLow and pinkHigh, which define the color range for the pink ball.
Try this program, borrowing my pink ball, or choosing an object of your own.
To determine color ranges for yourself:
- Take a picture of the object with the webcam on your computer (Photobooth for Mac, not sure for Windows)
- Use a color picker tool to read the RGB values from various spots on the ball (Digital Color Meter on Mac)
- Use an online converter to translate those into HSV values
- To get OpenCV’s values, divide the Hue value by 2, and scale the other two to the range from 0 to 255, instead of 0 to 100
Isolating the ball contour
Using a combination of the contour’s area and its shape, pick one contour that seems most likely to be the ball. You can eliminate any contours that have very small area, and then compare the area of each of the remaining contours to the area of the minimum enclosing circle. Or you could see which contour has the most points that lie on the boundary of the minimum enclosing circle (a bit more math to doing that).
Do these calculations in the thresholdPink function, and only draw the most likely ball contour on the original image.
Trying examples on your own
Go back to the programs I provided, or the ones you wrote, that used thresholding to isolate balls or coins in images. Try applying findContours to the best results you got from thresholding (possibly incorporating morphing or blurring). Can you determine where the balls or coins are in the images, using size or shape?
Optional Challenge
Try using thresholding and findContours to find an outline around your hand held up in front of the camera. You might need an external camera for this, because you might need to use as simple and blank a background as possible (like the whiteboard, or a blank wall).
From the size or shape of the contour, can you determine whether your hand is in a fist or a flat palm, or holding up one or two fingers?
What to hand in
Put all of your function definitions for this activity into the activ14.py file to be submitted. Make sure you format your code appropriately:
- At the top of the file is a triple-quoted string describing the file
- Next you include all import statements
- Next you have your function definitions, visually separated by blank lines, and maybe comments with dashed or other visual horizontal lines
- Each function should have a triple-quoted descriptive comment right after the
defline - All calls to all functions should be in a script at the bottom of the file, ideally inside an
if __name__ ...block
Use commit and push to copy your code to Github to submit this work.