k均值法颜色聚类
Color Quantization is the process of reducing the number of colors in an image while keeping the visual appearance of the image intact. This is an useful image compression technique which is quite useful for the devices that can show limited number of colors due to memory restriction.
颜色量化是在保持图像外观不变的同时减少图像颜色数量的过程。 这是一种有用的图像压缩技术,对于由于内存限制而只能显示有限数量的颜色的设备非常有用。
Every image can be represented by three features which is the B,G,R value for each pixel. Considering that our image has pixel values from 0 to 255 we can say that for each image we have 256 * 256 * 256 colors. Now our aim is to reduce this number of colors to a desired amount.
每个图像可以由三个特征表示,这三个特征是每个像素的B,G,R值。 考虑到我们的图像的像素值为0到255,我们可以说每个图像都有256 * 256 * 256种颜色。 现在,我们的目标是将这种颜色数量减少到所需的数量。
Here we shall apply quantization to the below image.
在这里,我们将量化应用于下图。
图像量化步骤: (Steps In Image Quantization:)
In the first step we shall try to choose the number of clusters for our image. Here number of cluster basically represents the number of colors we want to represent our image with. In our problem we would see how to represent the image with 3 and 50 colors.
在第一步中,我们将尝试选择图像的簇数。 这里的簇数基本上代表了我们要用来表示图像的颜色数。 在我们的问题中,我们将看到如何用3和50种颜色表示图像。
After choosing the number of colors now it is the time to choose the cluster centroids which would be the color representative of the clusters. For example for 3 colors let C1= (120,140,180) ,C2= (125,180,170) ,C3= (122,145,182) be the three cluster centers.
在选择了颜色数量之后,现在该是选择簇质心的时候了,它可以代表簇的颜色。 例如,对于3种颜色,令C1 =(120,140,180),C2 =(125,180,170),C3 =(122,145,182)是三个聚类中心。
In the next step we shall compute the distance of each point from the cluster centroids. And based on the distance we shall assign each point to the center with shortest distance. To compute the distance we shall consider the euclidean distance. We would continue this process till the maximum number of iteration is reached or the center doesn’t change at all.
在下一步中,我们将计算每个点到群集质心的距离。 然后根据距离,我们将每个点分配给最短距离的中心。 为了计算距离,我们将考虑欧氏距离。 我们将继续此过程,直到达到最大迭代次数或中心完全不变为止。
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('6.jpeg')
def quant(img,k):
data = np.float32(img).reshape(-1,3)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,20,1.0)
ret,label,center = cv2.kmeans(data, k , None, criteria,10,cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
result = center[label.flatten()]
result = result.reshape(img.shape)
return result
plt.imshow(quant(img,5))
plt.show()
#print(type(color_3))
The above code snippet shows how to do image quantization using kmeans clustering. Initially we would read the image and transform it into 3 features which is the B,G,R value of the image. Next we have declared the kmeans clustering algorithm using opencv. To do so we have initialized the stopping criterion where it says if the number of iteration is 20 or if all cluster centroid moves within a euclidean of one we shall stop the running.
上面的代码段显示了如何使用kmeans聚类进行图像量化。 最初,我们将读取图像并将其转换为3个特征,即图像的B,G,R值。 接下来,我们使用opencv声明kmeans聚类算法。 为此,我们初始化了停止标准,该标准指出如果迭代次数为20,或者如果所有聚类质心在一个欧几里德内移动,我们将停止运行。
In the 10th line of the code we are storing the label array and the center matrix. The label array stores the label value for each point whereas the center matrix stores the center of each cluster. In the next line we have replaced the pixel values with their center values.
在代码的第10行中,我们存储了标签数组和中心矩阵。 标签数组存储每个点的标签值,而中心矩阵存储每个簇的中心。 在下一行中,我们将像素值替换为其中心值。
Output for 5 cluster is as below for the given image
给定图像的5个簇的输出如下
Similarly output for 50 colors is as below
50种颜色的类似输出如下
Here we can see as the value of K increases the image becomes more realistic.
在这里我们可以看到,随着K值的增加,图像变得更加逼真。
This was an overview of image quantization using kmeans clustering.
这是使用kmeans聚类进行图像量化的概述。
Add me to your network https://www.linkedin.com/in/aditya-mohanty-7982451a9/
将我添加到您的网络https://www.linkedin.com/in/aditya-mohanty-7982451a9/
翻译自: https://medium.com/swlh/color-quantization-using-k-means-clustering-999278d0889e
k均值法颜色聚类