K-means在OpenCV的Python界面中

时间:2010-10-13 13:02:54

标签: python opencv

我正在使用内置python接口的v2.1。 我正在尝试从文件加载图像,将其转换为实验室 并从ab平面获得集群。

我有一个有效的matlab代码,但不知道如何在opencv中做同样的事情。 如何重塑jpeg或png图像并将其提供给kmeans?

由于

我得到的错误:

OpenCV Error: Assertion failed (labels.isContinuous() && labels.type() == CV_32S && (labels.cols == 1 || labels.rows == 1) && labels.cols + labels.rows - 1 == data.rows) in cvKMeans2, file /build/buildd/opencv-2.1.0/src/cxcore/cxmatrix.cpp, line 1202
Traceback (most recent call last):
File "main.py", line 24, in <module>
(cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0))
cv.error: labels.isContinuous() && labels.type() == CV_32S && (labels.cols == 1 || labels.rows == 1) && labels.cols + labels.rows - 1 == data.rows

由于

使用matlab代码:

im=imread(fName);
cform = makecform('srgb2lab');
lab_im = applycform(im,cform);
ab = double(lab_im(:,:,2:3));
ab = reshape(ab,nrows*ncols,2);
nColors = 2;
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',3,'start', 'uniform');

python-opencv(不工作)

img = cv.LoadImage("test.jpg")
clusters = cv.CreateImage((img.width*img.height, 1), img.depth, 1)
lab_img = cv.CreateImage(cv.GetSize(img), img.depth, 3)
cv.CvtColor(img, lab_img, cv.CV_RGB2Lab)

ab_img = cv.CreateImage(cv.GetSize(img), img.depth, 2)
cv.MixChannels([lab_img], [ab_img], [
    (1, 0),
    (2, 1)
])

cv.Reshape(ab_img, ab_img.channels, ab_img.width*ab_img.height)
cluster_count = 3
cv.KMeans2(ab_img, cluster_count, clusters,
    (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0))

1 个答案:

答案 0 :(得分:6)

我没有弄清楚你的来源有什么问题,但这是我的实现:

import cv
import sys

if len(sys.argv) < 3:
    print 'usage: %s image.png K' % __file__
    sys.exit(1)
im = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_COLOR)
K = int(sys.argv[2])

#
# Prepare the data for K-means.  Represent each pixel in the image as a 3D
# vector (each dimension corresponds to one of B,G,R color channel value).
# Create a column of such vectors -- it will be width*height tall, 1 wide
# and have a total 3 channels.
#
col = cv.Reshape(im, 3, im.width*im.height)
samples = cv.CreateMat(col.height, 1, cv.CV_32FC3)
cv.Scale(col, samples)
labels = cv.CreateMat(col.height, 1, cv.CV_32SC1)
#
# Run 10 iterations of the K-means algorithm.
#
crit = (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 10, 1.0)
cv.KMeans2(samples, K, labels, crit)
#
# Determine the center of each cluster.  The old OpenCV interface (C-style)
# doesn't seem to provide an easy way to get these directly, so we have to
# calculate them ourselves.
#
clusters = {}
for i in range(col.rows):
    b,g,r,_ = cv.Get1D(samples, i)
    lbl,_,_,_ = cv.Get1D(labels, i)
    try:
        clusters[lbl].append((b,g,r))
    except KeyError:
        clusters[lbl] = [ (b,g,r) ]
means = {}
for c in clusters:
    b,g,r = zip(*clusters[c])
    means[c] = (sum(b)/len(b), sum(g)/len(g), sum(r)/len(r), _)

#
# Reassign each pixel in the original image to the center of its corresponding
# cluster.
#
for i in range(col.rows):
    lbl,_,_,_ = cv.Get1D(labels, i)
    cv.Set1D(col, i, means[lbl])

interactive = False
if interactive:
    cv.ShowImage(__file__, im)
        cv.WaitKey(0)
else:
    cv.SaveImage('kmeans-%d.png' % K, im)

以下屏幕截图显示了正在运行的脚本。左侧的图像是原始的128x128像素图像。右边的图像是聚类的结果,K分别等于2,4,6和8。

original K=2 K=4 K=6 K=8