Color Identification in Images using Python and OpenCV
Have you ever wondered how computer programs are able to identify colors from an image? This can be done easily with Python and OpenCV. In this article, we will teach you how to identify colors from an image using Python and OpenCV.
Installing the Required Libraries
Before we start the coding part, we need to install a few required libraries to work with OpenCV. We can install these libraries using the pip command in Python.
pip install opencv-python
pip install pandas
Reading an Image
The first step in identifying colors from an image is to read the image. We can use the imread()
method from the OpenCV library to read an image.
import cv2
# Read the image
image = cv2.imread('sample.jpg')
The imread()
method reads an image from the specified file path and returns it as a NumPy array.
Converting the Image to the RGB Color Space
Now that we have read the image, the next step is to convert it to the RGB color space. The RGB color space is the most commonly used color space in digital images.
# Convert the image to RGB color space
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
We can use the cvtColor()
method from the OpenCV library to convert the image to the desired color space. In this case, we are converting it to the RGB color space.
Identifying Colors from the Image
Once we have converted the image to the RGB color space, we can start identifying the colors present in the image.
import numpy as np
import pandas as pd
# Define the color boundaries
boundaries = [
([17, 15, 100], [50, 56, 200]), # Red
([86, 31, 4], [220, 88, 50]), # Blue
([25, 146, 190], [62, 174, 250]), # Yellow
]
# Loop over the boundaries
for (lower, upper) in boundaries:
# Create NumPy arrays from the boundaries
lower = np.array(lower, dtype=np.uint8)
upper = np.array(upper, dtype=np.uint8)
# Find the colors within the specified boundaries and apply the mask
mask = cv2.inRange(rgb_image, lower, upper)
output = cv2.bitwise_and(rgb_image, rgb_image, mask=mask)
# Calculate the percentage of pixels in the output image that are within the boundaries
percentage = (np.count_nonzero(output) / (output.shape[0] * output.shape[1])) * 100
# Print the percentage of pixels
print(f"{percentage:.2f}% of the image is within the {str(lower)} - {str(upper)} color boundaries")
In this code, we first define the color boundaries for each color we want to identify. We then loop over each of the color boundaries and create NumPy arrays from them.
Next, we use the inRange()
method to find all the colors within the specified boundaries and create a mask that only shows those colors. We then use the bitwise_and()
method to apply the mask to the RGB image and get the output image that only shows the colors within the boundaries.
Finally, we calculate the percentage of pixels in the output image that are within the boundaries and print it to the console.
Conclusion
In this article, we learned how to identify colors from an image using Python and OpenCV. We first installed the required libraries and then read the image using the imread()
method. We then converted the image to the RGB color space and identified the colors present in the image using color boundaries and the inRange()
method.