Detect Specific Color From Image using Python OpenCV

Theory of Computation
In this previous article, we have mentioned to detect a specific color (say blue) from a captured video (webcam or video file) and in this article, you will learn to detect a specific color from an image using the Python OpenCV. Color detection is important to recognize objects, it is also utilized 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, like Object Identification, color detection, optical character recognition, photo editing, and so on. It provides lots of functions for image processing.
The color detection process is mostly in demand in computer vision. A color detection algorithm identifies pixels in an image that match a specified color or color range. The color of detected pixels can then be changed to distinguish them from the rest of the image. This process can be easily done using OpenCV.
In this article, we will import two modules - cv2 and numpy. After this, we will load the image using imread, we will convert the color-space from BGR to HSV using cv2.cvtColor(), like the following -
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
Next, we will define the lower and upper color(blue) rgb value in two variables, i.e.-
light_blue = np.array([110,50,50])
dark_blue = np.array([130,255,255])
The OpenCV provides cv2.inRange() method to set the color range. It accepts three parameters, the source image in the first parameter and lower and upper color boundary of the threshold region. This function returns a binary mask, which we will pass in the bitwise AND operator.
mask = cv2.inRange(hsv, light_blue, dark_blue)

output = cv2.bitwise_and(image,image, mask= mask)
Above, we have explained the code flow of color 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 -

Jan 3
Stateful vs Stateless
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

Dec 29
Best programming language to learn in 2021
In this article, we have mentioned the analyzed results of the best programming language for 2021...

In this article, we have mentioned the analyzed results of the best programming language for 2021...

Dec 20
How is Python best for mobile app development?
Python has a set of useful Libraries and Packages that minimize the use of code...

Python has a set of useful Libraries and Packages that minimize the use of code...

July 18
Learn all about Emoji
In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

Jan 10
Data Science Recruitment of Freshers
In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...






eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.