Color Checker with OpenCV
Colors are an important part of life and isn’t it amazing to know that now we can have our machine to see if a particular color is present in a image or not. Also we can use the machine t to guess if the color is present in the live video or not and in today’s blog we will be seeing that how we can do it.
Lets dive straight into it.
Firstly we will install the essentials by the command which is given below.
pip install opencv-contrib-python pip install numpy
If numpy doesn’t gets installed with the first command then you can write the second command as well.
Now importing the essentials in our main python file.
import cv2 import numpy as np
Initializing our camera to capture live video with the help of VideoCapture module available in openCV.
cap = cv2.VideoCapture(0)
Now starting the loop which will capture the video.
Now taking each frame which will be analyzed with the help of our camera. We have a ‘_’ also which says that we don’t whatever the variable is we just want to have the value for it.The frames are read with the head of a read function.The ‘_’ usually has a boolean value which is used t determine if we have things being captured or not.Frames are basically combination of pixels in the form of numpy arrays.
_, frame = cap.read() frame=np.flip(frame,axis=1)
Now we will be converting the frames to HSV format from the original color format.
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
Further we will be defining the range of the color which wee want to analyze. We will set the upper boundary and the lower boundary of the color.
lower_blue = np.array([110,50,50]) upper_blue = np.array([130,255,255])
Fixing the threshold the HSV images to get only the blue colors.
mask = cv2.inRange(hsv, lower_blue, upper_blue)
The above line of code will check if the following HSV images are in the range or not.
Next we will use morphologyEx which will help us in performing some operations of the detected color.
mask_all=cv2.morphologyEx(mask,cv2.MORPH_DILATE, np.ones((3,3),np.uint8))
I has taken the mask which we have obtained after checking, a MORPH function which is used to extract out a particular thing from a image and a numpy arrays of all ones.
Next we will be performing two bit wise operation on the mask which we have obtained.
mask2=cv2.bitwise_not(mask) streamA = cv2.bitwise_and(frame,frame, mask= mask2)
The first operation takes the mask we have obtained from above and passing it to the bitwise_not operator. As the mask is in the form of numpy array we are able to perform a bitwise operation on it.Next operation takes that mask2 we have obtained and performs a bitwise _and operation on the frames and the mask2.
Next we will find the points of the mask which means the points where the color is present.
points = cv2.findNonZero(mask)
It returns us the point where the values are no zero.I will return us a json output.
Next taking the mean of those points with a numpy mean function and printing the points.
avg = np.mean(points, axis=0) print(points)
Next we will have a rectangle drawn if possible on the points which we have found out to be non zero.
cv2.rectangle(streamA,(points[0][0][0],points[0][0][1]),(700,700),(255,0,0),2)
Rectangle function contains the streamA which we have obtained after the last bitwise operation, the initial coordinates from the json output which we have obtained from the points, the final coordinates which are (700,700), the color and the thickness of the lines of the rectangle.
cv2.rectangle(streamA,(points[0][0][0],points[0][0][1]),(700,700),(255,0,0),2)
Now showing the the final output with the imshow method.
cv2.imshow('frame',streamA)
Finally setting a way through which we can stop the live video to be shown after the press of a key.
k = cv2.waitKey(5) & 0xFF if k == 27: break
This basically breaks us out of the while loop which we have started above.
Finally destroying all the windows which have been generated in this process.
cv2.destroyAllWindows()
The complete code which i have used above is given below for a better look.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
background=0
while(1): # Take each frame
_, frame = cap.read()
frame=np.flip(frame,axis=1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue) mask=cv2.morphologyEx(mask,cv2.MORPH_OPEN,np.ones((3,3),np.uint8)) mask_all=cv2.morphologyEx(mask,cv2.MORPH_DILATE,np.ones((3,3),np.uint8)) mask2=cv2.bitwise_not(mask) streamA = cv2.bitwise_and(frame,frame, mask= mask2) points = cv2.findNonZero(mask) avg = np.mean(points, axis=0) print(points) cv2.rectangle(streamA,(points[0][0][0],points[0][0][1]),(700,700),(255,0,0),2) cv2.imshow('frame',streamA) k = cv2.waitKey(5) & 0xFF if k == 27: break cv2.destroyAllWindows()
I have also attached the github repo for a better look at the code.It also has a another method which can be used to find the color but the only difference is that this code masks the desired color here blue but that code will only show the desired color rest all the code will be masked.