OpenCV and Python K-Means Color Clustering(图像像素聚类)

OpenCV and Python K-Means Color Clustering(图像像素聚类)

英文原文

translator:aaron-clark-aic

简单讲,聚类就是将一组数据按照相识度分割成几类,也就是归类算法

k-means算法是一种简单的迭代型聚类算法

Python opencv中的k-mean聚类算法有现成的实现

需要注意的是,对于图像聚类我们需要将图像矩阵转换为像素的列表

# load the image and show it
image_base = cv.imread(".././img/base.jpg")

# reshape the image to be a list of pixels
image = image_base.reshape((image_base.shape[0] * image_base.shape[1], 3))

调用聚类算法的实现,实现聚类

k = 5 #聚类的类别个数
iterations = 4 #并发数4
iteration = 200 #聚类最大循环次数

clt = KMeans(n_clusters = k, n_jobs = iterations, max_iter = iteration) 
clt.fit(image)

调用直方图算法的实现,统计出图像的聚类分布

def centroid_histogram(clt):
    # grab the number of different clusters and create a histogram
    # based on the number of pixels assigned to each cluster
    numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
    (hist, _) = np.histogram(clt.labels_, bins=numLabels)

    # normalize the histogram, such that it sums to one
    hist = hist.astype("float")
    hist /= hist.sum()

    # return the histogram
    return hist


def plot_colors(hist, centroids):
    # initialize the bar chart representing the relative frequency
    # of each of the colors
    bar = np.zeros((50, 300, 3), dtype="uint8")
    startX = 0

    # loop over the percentage of each cluster and the color of
    # each cluster
    for (percent, color) in zip(hist, centroids):
        # plot the relative percentage of each cluster
        endX = startX + (percent * 300)
        cv.rectangle(bar, (int(startX), 0), (int(endX), 50),
                      color.astype("uint8").tolist(), -1)
        startX = endX

    # return the bar chart
    return bar

hist = centroid_histogram(clt)
bar = plot_colors(hist, clt.cluster_centers_)

完整代码

OpenCV and Python K-Means Color Clustering(图像像素聚类)_第1张图片

# import the necessary packages
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
import cv2 as cv
import numpy as np


def centroid_histogram(clt):
    # grab the number of different clusters and create a histogram
    # based on the number of pixels assigned to each cluster
    numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
    (hist, _) = np.histogram(clt.labels_, bins=numLabels)

    # normalize the histogram, such that it sums to one
    hist = hist.astype("float")
    hist /= hist.sum()

    # return the histogram
    return hist


def plot_colors(hist, centroids):
    # initialize the bar chart representing the relative frequency
    # of each of the colors
    bar = np.zeros((50, 300, 3), dtype="uint8")
    startX = 0

    # loop over the percentage of each cluster and the color of
    # each cluster
    for (percent, color) in zip(hist, centroids):
        # plot the relative percentage of each cluster
        endX = startX + (percent * 300)
        cv.rectangle(bar, (int(startX), 0), (int(endX), 50),
                      color.astype("uint8").tolist(), -1)
        startX = endX

    # return the bar chart
    return bar

# load the image and show it
image_base = cv.imread(".././img/base.jpg")

# reshape the image to be a list of pixels
image = image_base.reshape((image_base.shape[0] * image_base.shape[1], 3))

k = 5 #聚类的类别个数
iterations = 4 #并发数4
iteration = 200 #聚类最大循环次数

clt = KMeans(n_clusters = k, n_jobs = iterations, max_iter = iteration)
clt.fit(image)

hist = centroid_histogram(clt)
bar = plot_colors(hist, clt.cluster_centers_)

# show our color bart
fig = plt.figure()
ax = fig.add_subplot(211)
ax.imshow(image_base)
ax = fig.add_subplot(212)
ax.imshow(bar)
plt.show()

cv.waitKey(0)


你可能感兴趣的:(机器视觉,python,opencv)