How-To: OpenCV和Python的K-Means颜色聚类法

Jurassic Park Movie Poster

Take a second to look at the Jurassic Park movie poster above.

花一点时间看看上面的《侏罗纪公园》电影海报。

What are the dominant colors? (i.e. the colors that are represented most in the image)

什么是主色调?(即在图像中表现得最多的颜色)

Well, we see that the background is largely black. There is some red around the T-Rex. And there is some yellow surrounding the actual logo.

好吧,我们看到,背景基本上是黑色的。T-Rex周围有一些红色。还有一些黄色围绕着实际的标志。

It’s pretty simple for the human mind to pick out these colors.

对于人类的大脑来说,挑选出这些颜色是非常简单的。

But what if we wanted to create an algorithm to automatically pull out these colors?

但如果我们想创建一个算法来自动拉出这些颜色呢?

You might think that a color histogram is your best bet…

你可能认为颜色直方图是你最好的选择...

But there’s actually a more interesting algorithm we can apply — k-means clustering.

但实际上,我们可以应用一种更有趣的算法--K-means聚类法。

In this blog post I’ll show you how to use OpenCV, Python, and the k-means clustering algorithm to find the most dominant colors in an image.

在这篇博文中,我将向你展示如何使用OpenCV、Python和K-means聚类算法来寻找图像中最主要的颜色。

OpenCV and Python versions:
This example will run on Python 2.7/Python 3.4+ and OpenCV 2.4.X/OpenCV 3.0+.

OpenCV和Python版本:
这个例子将在Python 2.7/Python 3.4+OpenCV 2.4.X/OpenCV 3.0+上运行。

K-Means Clustering

K-均值聚类法

So what exactly is k-means?

那么究竟什么是K-means?

K-means is a clustering algorithm.

K-means是一种聚类算法

The goal is to partition n data points into k clusters. Each of the n data points will be assigned to a cluster with the nearest mean. The mean of each cluster is called its “centroid” or “center”.

目标是将n个数据点划分为k个聚类。n个数据点中的每一个都将被分配到一个与平均数最接近的聚类。每个聚类的平均值被称为其 "中心点 "或 "中心"。

Overall, applying k-means yields k separate clusters of the original n data points. Data points inside a particular cluster are considered to be “more similar” to each other than data points that belong to other clusters.

总的来说,应用k-means可以得到原始n个数据点的k个独立聚类。一个特定聚类内的数据点被认为比属于其他聚类的数据点彼此 "更相似"。

In our case, we will be clustering the pixel intensities of a RGB image. Given a MxN size image, we thus have MxN pixels, each consisting of three components: Red, Green, and Blue respectively.

在我们的案例中,我们将对一个RGB图像的像素强度进行...

开通本站会员,查看完整译文。

ホーム - Wiki
Copyright © 2011-2024 iteam. Current version is 2.139.0. UTC+08:00, 2024-12-27 19:38
浙ICP备14020137号-1 $お客様$