Color Identification in Images using Python – OpenCV
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?
Color Recognition 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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])
How to identify color in OpenCV?
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.
Finding HSV code from RGB code
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].
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]