Detect Specific Color From Image using Python OpenCV
In this article, you will learn how to detect a specific colour or similar colour from an image using the Python OpenCV. Colour detection is important to recognise objects. It is also used as a tool in various image editing and drawing applications.
The OpenCV module is being used for a very wide range of image processing and analysis tasks, like object identification, colour detection, optical character recognition, photo editing, and so on. It provides lots of functions for image processing.
The colour detection process is mostly in demand in computer vision. A colour detection algorithm identifies pixels in an image that match a specified colour or colour range. The colour of detected pixels can then be changed to distinguish them from the rest of the image. This process can be easily done using the OpenCV.
Project Necessity
- Python 3.x
- OpenCV 4.5
- Numpy 1.20.3
In this article, we import two modules- cv2 and numpy. After this, we load the image using the imread. Then, we convert the color-space from BGR to HSV using the cv2.cvtColor(), like the following-
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
Next, we define the lower and upper color(blue) rgb values in two variables, i.e.,-
light_blue = np.array([110,50,50])
dark_blue = np.array([130,255,255])
The OpenCV provides the cv2.inRange() method to set the colour range. It accepts three parameters, the source image in the first parameter and the lower and upper colour boundaries of the threshold region. This function returns a binary mask, which we will pass into the bitwise AND operator.
mask = cv2.inRange(hsv, light_blue, dark_blue)
output = cv2.bitwise_and(image,image, mask= mask)
Complete Code
Above, we have explained the code flow of colour detection. Here is the complete code-
import cv2
import numpy as np
image = cv2.imread("blue_flowers.jpg")
# Convert BGR to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# define blue color range
light_blue = np.array([110,50,50])
dark_blue = np.array([130,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, light_blue, dark_blue)
# Bitwise-AND mask and original image
output = cv2.bitwise_and(image,image, mask= mask)
cv2.imshow("Color Detected", np.hstack((image,output)))
cv2.waitKey(0)
cv2.destroyAllWindows()
The above code returns the following output-
Related Articles
OpenCV Logical Operators- Bitwise AND, OR, NOR, XORArithmetic Operations on Images using Python OpenCV
Adaptive Thresholding in Python OpenCV
Geometric Transformation OpenCV Python
Python OpenCV Erosion and Dilation
Color detection OpenCV Python
How to capture a video in Python OpenCV and save
Python overlay two images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Body tracking OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples