OpenCV 的 Python 接口中的 K-means [英] K-means in OpenCV's Python interface

查看:61
本文介绍了OpenCV 的 Python 接口中的 K-means的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using v2.1 with the built-in python interface. I'm trying to load an image from a file, transform it to lab and get the clusters from the ab plane.

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

I have a working matlab code but don't know how to do the same in opencv. How do I reshape a jpeg or png images and feed it to 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 代码:

Working matlab code:

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(不工作)

python-opencv (not working)

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))

推荐答案

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

I didn't have to work out what the problem with your source was, but here's my implementation:

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

The following screenshots show the script in action. The image on the left is the original 128x128 pixel image. Images to its right are the result of clustering with K equal to 2, 4, 6 and 8, respectively.

这篇关于OpenCV 的 Python 接口中的 K-means的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆