Color Identification in Images using Python - OpenCV - GeeksforGeeks

03 Mar, 2021
An open-source library in Python, OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Don’t you know how to find these colors in images?
A monitor or TV screen basically generates three types of colors, i.e., red, green, and yellow. But the combination and intensities of these three colors make various colors. Thus, each color has its unique HSV color code. For finding the specified color in the given image, we need to use the lower and upper bound of that color.
Example: For finding the green color in the image, we need to specify the lower and upper HSV color code for green color as follows.
lower=np.array([50, 100,100])
upper=np.array([70, 255, 255])
Let’s try to practically implement this method. For instance, consider the below-given page source. In this source code, we are finding the green color in the shapes.jpg image. The image used in the example given below 
You can download this and save it in the same folder where you have saved the python file in which you are currently writing. 
Implementation:
Python
# Python program to identify#color in images  # Importing the libraries OpenCV and numpyimport cv2import numpy as np  # Read the imagesimg = cv2.imread("Resources/shapes.jpg")  # Resizing the imageimage = cv2.resize(img, (700, 600))  # Convert Image to Image HSVhsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)  # Defining lower and upper bound HSV valueslower = np.array([50, 100, 100])upper = np.array([70, 255, 255])  # Defining mask for detecting colormask = cv2.inRange(hsv, lower, upper)  # Display Image and Maskcv2.imshow("Image", image)cv2.imshow("Mask", mask)  # Make python sleep for unlimited timecv2.waitKey(0)
Output:
Don’t you know how to find this unique HSV color code? Just write this below-given program to find out the HSV color code from RGB color code. You can choose the RGB color code of the color you want from here. After finding the RGB code for the color you want to find, write this program to HSV code for that color. 
Example: In the below source code, we have found the HSV value for the green color. The RGB value for the green color is [0, 255, 0].
Python
# Python programs to find# unique HSV code for color  # Importing the libraries openCV & numpyimport cv2import numpy as np  # Get green colorgreen = np.uint8([[[0, 255, 0]]])  # Convert Green color to Green HSVhsv_green = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)  # Print HSV Value for Green colorprint(hsv_green)  # Make python sleep for unlimited timecv2.waitKey(0)
Output:
Once, you have found the unique HSV code for a particular color, get the lower HSV bound and upper HSV bound of that color by following the below steps.
lower = [h-10, 100, 100]
upper = [h+10, 255, 255]
Example: For the green color, HSV color code is [60, 255, 255]. Hence, the lower and upper HSV bound of that color will be as follows.
lower = [50, 100, 100]
upper = [70, 255, 255]
Picked
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python