simple color tracking using opencv(no machine learning mombo jambo!)
In this method we are using opencv and python for identifying a color and hilight it. this method uses the simple Bitwise and operation and image "HSV(hue,saturation,value)" manupulation of colors.
The HSV value of image is represent the original colors of image though the RGB or BGR cannot tell the difference between the levels of colors hence this method only to work with HSV images
since the HUE tells the color and saturation tells the level of color.
here i'm tracking blue levels of colors in HSV u can track any color u want you need the hsv value for that, i suggest to use max value of upper color and min value for lower colorat start and play around the values untill u get statisfied results or just google it!.
To get started u need few things, and just few line of codes you get ur face detection. we need the following things to get started:
1.python 3 recent would be nice!
2.opencv lib package installed using pip
3.laptop/desktop with webcam
step 1:
go to www.python.org and download latest version of python3 choose the install python along with pip and Option says add path variable to system. if ur already installed python 3 u can skip to step 2. if you prefer using any online or offline code editor its fine or we can use one provided with python IDLE default.
step 2:
open cmd prompt if ur in windows or terminal in linux, use cmd: "pip install opencv-python" in windows, cmd: "sudo pip install opencv-python" in linux. we can install a different package for more option i.e contributed package in opencv i.e "pip install opencv-contrib-python".
Step 3:
let's start coding, lets open idle editor start coding, the code looks like the following one. the following code identify's the blue color an displays as binary inversed imaging(I.e is 1 or 0)
code:
import cv2 import numpy as np cap = cv2.VideoCapture(0) while(1): #read frame _, frame = cap.read() #cvt bgr to hsv hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) #apply median filter hsv = cv2.medianBlur(hsv,7) #apply opening hsv = cv2.morphologyEx(hsv,cv2.MORPH_CLOSE,(8,8),3) # define range of blue color in HSV lower_blue = np.array([100,50,50]) upper_blue = np.array([130,255,255]) # Threshold the HSV image to get only blue colors mask = cv2.inRange(hsv,lower_blue,upper_blue) # Bitwise-AND mask and original image res = cv2.bitwise_and(frame,frame, mask= mask) cv2.imshow('frame',frame) cv2.imshow('mask',mask) cv2.imshow('res',res) if cv2.waitKey(5) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
Comments
Post a Comment