In the previous tutorial we used python to communicate with Arduino serially. If you haven’t checked that post, here is the link: Communication between Arduino & Python.

In this tutorial, We will use Python and OpenCV to detect and track colours using a webcam. This will be very useful in future robotic projects. So let’s begin.

Installing Python:

First step is to install python in your computer. To download Python 2.7 visit www.python.org and select your operating system (Windows/Linux/Mac).

In this tutorial I have used Windows. If you use Linux/Mac OS X , your device already has Python 2.7 installed.

For window users, after you have downloaded python, Install it. Installation is pretty simple just follow the GUI. Keep the location default and you should have python installed in your C: drive. Make sure you download Python 2.7 32bit even if you have a 64 bit OS, Because I found it difficult to install some modules in 64bit version.

After installation is completed you can head to windows search and enter “IDLE”. A python IDE will show up where you can type in simple programs to test if everything works.

Installing Modules:

We need to install 2 modules for this project.

  1. OpenCV
  2. Numpy

Both can be installed using pip through windows CMD (Command Prompt).

First Open CMD and type ‘ pip ‘ and hit enter. If some error occurs you need to install pip. Now I will not go into that as there are many good tutorials out there.

Once pip is working use the command bellow to install OpenCV.

>> pip install opencv-python

To install Numpy :-

>> pip install numpy

This will install the modules and you can move to next step to start coding.

NOTE :- You require active internet connection to use pip.

Coding:

After every thing is setup and ready to use we can start programming.

Note that you need a webcam for this project. If you don’t have one you can use your Android smartphone as webcam using DroidCam app.

To write the code you can use any text editor or python IDE.

#importing Modules 
import cv2
import numpy as np

So here we import the required modules . ‘cv2’ imports opencv and numpy is imported as np.

#Capturing Video through webcam. 
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()

Here we use cv2’s videocapture method to start the webcam and record. It is stored in variable ‘cap’. Here the argument ‘0’ defines default webcam. if your device has a secondary camera you can use it by replacing ‘0’ with ‘1’.

The next while loop checks if Image is being received. When it is True video is streaming and read() function is used to read the video frames and store in variable ‘img’.

#converting frame(img) from BGR (Blue-Green-Red) to HSV (hue-saturation-value) 
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

We convert the frame from BGR to HSV . Know more about HSV here

#defining the range of Yellow color

yellow_lower = np.array([22,60,200],np.uint8)
yellow_upper = np.array([60,255,255],np.uint8)

Here we will be tracking Yellow colour. So we define range of yellow color.

#finding the range yellow colour in the image  
yellow = cv2.inRange(hsv, yellow_lower, yellow_upper)

Here we find the range of yellow colour in the frame.

#Morphological transformation, Dilation        
kernal = np.ones((5 ,5), "uint8")
blue=cv2.dilate(yellow, kernal)
res=cv2.bitwise_and(img, img, mask = yellow)

Here we compare the amount of yellow to blue as a reference.

#Tracking Colour (Yellow)  (_,contours,hierarchy)=cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)               
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour) if(area>300): x,y,w,h = cv2.boundingRect(contour) img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3)

This code tracks the colour yellow and draws a rectangle around it.

#Display results
img = cv2.flip(img,1)
cv2.imshow("Yellow",res)

cv2.flip() method flips the image or mirrors it. if you want you can remove this line to see a regular output.

cv2.imshow(res) displays the video stream in a window with only the yellow colour it eliminates every other colour.

cv2.imshow("Color Tracking",img)
if cv2.waitKey(10) & 0xFF == 27:
                cap.release()
                cv2.destroyAllWindows()
                break

cv2.imshow(img) show the normal video with yellow colour being tracked. Download the code from Color.py

Just execute the code , Make sure your webcam is connected. Below I have shared some images of the code in action:

If you have any questions, Comment bellow…

4 1 vote
Article Rating