gpt4 book ai didi

python - OpenCV 的 Python 接口(interface)中的 K-means

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:09 25 4
gpt4 key购买 nike

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

我有一个可用的 matlab 代码,但不知道如何在 opencv 中执行同样的操作。如何 reshape 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))

最佳答案

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

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

关于python - OpenCV 的 Python 接口(interface)中的 K-means,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3923906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com