【问题标题】:K-means in OpenCV's Python interfaceOpenCV Python 界面中的 K-means
【发布时间】:2014-04-25 13:24:03
【问题描述】:

我正在使用带有内置 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))

【问题讨论】:

  • 这怎么行?如果我可以问。我还没有遇到你的问题,但是我即将开始一个有一些类似分析的项目,所以我很好奇你可能有什么问题。
  • 我让它在 C 中工作,但在 Reshape 和/或 Kmeans2 的 python 实现中有一个错误。我仅在需要显示时将所有内容转换为矩阵并返回图像。 clusters 应该是一个带有 cv.CV_8UC1 的矩阵。图像矩阵的深度应该是 CV_32FC2。如果您有问题,请询问更多。

标签: python opencv


【解决方案1】:

我不必弄清楚你的源代码有什么问题,但这是我的实现:

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 的聚类结果。

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 2010-12-11
    • 2011-07-24
    • 2013-07-03
    • 2011-07-27
    • 2015-02-15
    • 2018-10-11
    • 2016-02-01
    • 2010-12-05
    相关资源
    最近更新 更多