Color quantization in an image using K-means in OpenCV Python
If you are someone who loves to play around with images and want your image to look as if it has been taken from a different time period, then this article is for you. In this article, we will discuss color quantization in an image using K-means in OpenCV Python.
What is color quantization?
Color quantization is a process of reducing the number of colors in an image while maintaining the overall visual quality. In other words, it is a process of representing an image with a smaller number of colors. This process is used in many domains like image processing, computer graphics, and computer vision.
What is K-means?
K-means is a clustering algorithm that is used to divide a set of data points into clusters. The goal of K-means is to minimize the sum of the squared distances between the data points and their assigned cluster centroids. K-means is widely used in image processing, computer vision, and machine learning.
How to perform color quantization using K-means in OpenCV Python?
To perform color quantization using K-means in OpenCV Python, we will need to follow the following steps:
- Read the image using the OpenCV imread() function.
- Convert the image to a 2D array.
- Apply K-means clustering to the 2D array.
- Assign each pixel to its nearest cluster centroid.
- Replace each pixel value with its cluster centroid value.
- Convert the image back to its original color space.
- Save the image using the OpenCV imwrite() function.
Sample Code
import cv2
import numpy as np
# Read the image
img = cv2.imread('input.jpg')
# Convert the image to a 2D array
pixels = np.float32(img.reshape(-1, 3))
# Apply K-means clustering to the 2D array
kmeans = cv2.Kmeans(pixels, K=8, criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0), attempts=10, flags=cv2.KMEANS_RANDOM_CENTERS)
# Assign each pixel to its nearest cluster centroid
labels = np.uint8(kmeans[1].flatten())
# Replace each pixel value with its cluster centroid value
quantized = kmeans[0][labels].reshape(img.shape)
# Convert the image back to its original color space
quantized = cv2.cvtColor(quantized, cv2.COLOR_BGR2RGB)
# Save the image
cv2.imwrite('output.jpg', quantized)
In the above code, we first read the image using the imread()
function of OpenCV. Then we convert the image to a 2D array using the reshape()
function of numpy. Next, we apply K-means clustering to the 2D array using the Kmeans()
function of OpenCV. We assign each pixel to its nearest cluster centroid using the flatten()
function of numpy. Then we replace each pixel value with its cluster centroid value using another reshape()
function of numpy. Finally, we convert the image back to its original color space and save it using the cvtColor()
and imwrite()
functions of OpenCV, respectively.
Conclusion
In this article, we discussed color quantization in an image using K-means in OpenCV Python. We learned what color quantization is, what K-means is, and how to perform color quantization using K-means in OpenCV Python. We also provided sample code and explained each step in detail. We hope this article was helpful to you, and you successfully learned how to perform color quantization using K-means in OpenCV Python.