Color Detection using OpenCV in Python
This article will help in color detection in Python using OpenCV through both videos and saved images. So let’s start learning how to detect color using OpenCV in Python. Firstly set up the python environment and make sure that OpenCV and NumPy are being installed on your PC as NumPy is also a need for working with OpenCV. Let’s start with the program.
Detect color in Python using OpenCV
1) Detection of colors in saved images:
- Import the OpenCV and NumPy libraries so that you can use their parameters as
import cv2 #old interface in old OpenCV versions was named as cv import numpy as np
2. Read the image by providing a proper path else save the image in the working directory and just give the name of an image. Here we are creating a variable that will store the image and input is taken by cv2.imread (OpenCV function to read an image).
3. Use cv2.cvtColor() to convert the image from BGR to HSV(Hue,saturation,value). Conversion can also be done from BGR to GrayScale.
cvtColor() takes two parameters as (input_image, type of conversion)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
4. Finally, we’ll create the mask of the image(which will indicate only the desired color or the one whose input is given and rest all the colors will be erased). This will be done by using inRange function which will take the input as (image, lower_range,higher_range). The range will give the color to be shown. Hence we’ve to give specify the lower and upper range of the color we want to detect.For example range of red color we are detecting is [0,100,100] to [5,255,255]. The code will be as shown:
lower_range = np.array([0,100,100]) upper_range = np.array([5,255,255]) mask = cv2.inRange(hsv, lower_range, upper_range)
5. Now show the input image as well as the mask one using cv2.imshow() which takes two parameters as (window_name, variable_holding_image)
cv2.imshow('image_window_name', image) cv2.imshow('mask_window_name', mask)
6. Now decide the image availability time through cv2.waitkey() which takes the time parameters in milliseconds. 0 will wait infinitely.At last close the window through cv2.destroyAllWindows() function.
Here is the whole code for color detection using OpenCV in python in Image
import cv2 import numpy as np image = cv2.imread('img.jpg') hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) lower_range = np.array([0,100,100]) upper_range = np.array([5,255,255]) mask = cv2.inRange(hsv, lower_range, upper_range) cv2.imshow('image_window_name', image) cv2.imshow('mask_window_name', mask) cv2.waitKey(0) cv2.destroyAllWindows()
The output is given below:
Detection of colors in Live Videos in Python using OpenCV
1. Everything is the same except the reading of the input, as we are reading the video so instead of cv2.imread we have to use cv2.VideoCapture.Here vid is the variable holding the input coming through videos and vid.read will return the frame coming through a camera which will be stored in the image and boolean value (true/false) to indicate its working or not.
Also, color modification is done. We are detecting the blue color and accordingly the range is specified.
Here is the whole code for color detection using OpenCV in python in Video:
import cv2 import numpy as np vid = cv2.VideoCapture(0) while(1): _, image = vid.read() hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) lower_range = np.array([110,50,50]) upper_range = np.array([130,255,255]) mask = cv2.inRange(hsv,lower_range,upper_range) cv2.imshow('image_window_name',image) cv2.imshow('mask_window_namw',mask) cv2.waitKey(5) cv2.destroyAllWindows()
Output image link is given here:
You may also read: