This a guide on how to create your very own OpenCV color detector and tracker in the simplest way possible.
If you are here just for the code, go ahead! But, if you wanna understand what goes on under the hood then keep scrolling!
The code I have written tracks yellow colored objects in well lit surroundings (lighting is very important!). If you wanna track some other color then you can use the bgr2hsv file to convert any BGR value into HSV color space.
The rest of the instructable includes expanation of the code line-by-line.
- Python 3
- OpenCV for Python (use: pip install opencv-python)
- Numpy (use: pip install numpy)
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Importing Libraries
- We are including OpenCV library at first. It's called cv2 in Python.
- Next up, we have included numpy library as np. True to it's name, "as" allows us to use np instead of typing numpy again and again and again. Phew! What a relief.
Step 2: Connecting to the Camera
Here all we are doing is just connecting to your PC's camera. If you have more than one camera hooked into your PC then you might have to play around with this line a bit 'cause we are establishing a connection with the default camera (cv2.VideoCapture(0), 0 - means default). Try changing 0 to 1, 2...... (Who on earth would have some many cameras connected to their PC!) and see what works out for you.
Step 3: Converting the Input Into HSV Format
Before we convert the input into HSV format we need to first create a while loop.
That's just because the cap.read() function only reads one image at a time and putting it in a while loop is equivalent to queuing up images/frames so as to display it as a video (remember that a video is nothing but just a collection of images/frames timed to fake our eyes).
Step 4: Filtering Out Yellow Pixels
- We are trying to filter out only the yellow pixels.
- cv2.inRange() expects you to enter the starting and ending HSV value of the color that you are trying to filter out of your image(here we are providing that through lower_yellow and higher_yellow).
Step 5: Finding Contours
What is this strange thing called contour?
Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity and this code helps us do the same.
Side note: You must be wondering why I'm using "_" - underscores. They are used when you don't need a specific return value of a function.e.g. findCountours() returns 3 values but here we only need to know about the coordinates of the contour, so we mark rest of them with underscores.
Step 6: Getting the Coordinates of the Rectangle Bounding the Contour
- We run a for loop.
- Store the area of the contour in a variable called area.
- If the area exceeds our threshold value then we get it's coordinates, length and breadth. (Thresholding is important 'cause we don't wanna get the coordinates of false positives.)
- We construct a rectangle on the output image using the coordinates, length and breadth that we got.
Step 7: Output and Termination
- cv2.imshow() is like the print() function of python. Here we are outputting the frame with rectangle drawn on it.
- cv2.waitKey() terminates the program when a specific key is pressed. Right now it's set to esc key.