Last active
April 5, 2024 00:00
-
-
Save sambbhavgarg/af1c362c40b5f62ce1364e0472a10427 to your computer and use it in GitHub Desktop.
A Colour Detection Module using OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
cam=cv2.VideoCapture(0) | |
while (1): | |
_,im=cam.read() | |
#converting video streamed frames to HSV. which we will learn further | |
hsv=cv2.cvtColor(im,cv2.COLOR_BGR2HSV) | |
#Red Color | |
red_light=np.array([165,87,111],np.uint8) | |
red_dark=np.array([180,255,255],np.uint8) | |
#Blue Color | |
blue_light=np.array([99,115,150],np.uint8) | |
blue_dark=np.array([120,255,255],np.uint8) | |
#Yellow Color | |
yellow_light=np.array([55,60,200],np.uint8) | |
yellow_dark=np.array([60,255,255],np.uint8) | |
#Green Color | |
green_light=np.array([65, 50, 50],np.uint8) | |
green_dark=np.array([77,255,255],np.uint8) | |
#Range Specification | |
red=cv2.inRange(hsv,red_light,red_dark) | |
blue=cv2.inRange(hsv,blue_light,blue_dark) | |
yellow=cv2.inRange(hsv,yellow_light,yellow_dark) | |
green=cv2.inRange(hsv,green_light,green_dark) | |
#Morphological Transformation using dilation | |
kernel=np.ones((15,15),"uint8") | |
red=cv2.dilate(red,kernel) | |
res_red = cv2.bitwise_and(im,im, mask=red) | |
blue=cv2.dilate(blue,kernel) | |
res_blue = cv2.bitwise_and(im,im, mask=blue) | |
yellow=cv2.dilate(yellow,kernel) | |
res_yellow = cv2.bitwise_and(im,im, mask=yellow) | |
green=cv2.dilate(green,kernel) | |
res_green = cv2.bitwise_and(im,im, mask=green) | |
#Tracking Red | Contour is used to recognize colored object in a video feed., hierarchy sets the ek ke andar ek wala thing for objects in a feed which is 2D | |
#enumerate fn: https://www.geeksforgeeks.org/enumerate-in-python/ | |
(_,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) | |
for _,contour in enumerate(contours): | |
area=cv2.contourArea(contour) | |
if area>300: | |
x,y,w,h=cv2.boundingRect(contour) | |
cnt=contours[len(contours)-1] | |
cv2.drawContours(im,cnt,-1,(0,0,255),3) | |
cv2.putText(im,"Red",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255)) | |
#Tracking Blue | |
(_,contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) | |
for _,contour in enumerate(contours): | |
area=cv2.contourArea(contour) | |
if area>300: | |
x,y,_,_=cv2.boundingRect(contour) | |
cnt=contours[len(contours)-1] | |
cv2.drawContours(im,cnt,-1,(255,0,0),2) | |
cv2.putText(im,"Blue",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,0,0)) | |
#Tracking Yellow | |
(_,contours,hierarchy)=cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) | |
for _,contour in enumerate(contours): | |
area=cv2.contourArea(contour) | |
if area>300: | |
x,y,w,h=cv2.boundingRect(contour) | |
cnt=contours[len(contours)-1] | |
cv2.drawContours(im,cnt,-1,(0,255,255),2) | |
cv2.putText(im,"Yellow",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,255)) | |
#Tracking Green | |
(_,contours,hierarchy)=cv2.findContours(green,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) | |
for _,contour in enumerate(contours): | |
area=cv2.contourArea(contour) | |
if area>300: | |
x,y,w,h=cv2.boundingRect(contour) | |
cnt=contours[len(contours)-1] | |
cv2.drawContours(im,cnt,-1,(0,255,0),2) | |
cv2.putText(im,"Green",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0)) | |
blur=cv2.medianBlur(res_blue,15) | |
cv2.imshow("Blur",blur) | |
cv2.imshow("Color Tracker",im) | |
cv2.imshow("mask",res_blue) | |
#cv2.imshow("median_red",median1) | |
if cv2.waitKey(33)==27 and 0xFF!= ord('q'): | |
break | |
cam.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment