2012-04-20 121 views
5

这个问题特定于opencv: opencv文档中给出的kmeans示例具有2通道矩阵 - 每个维度的特征向量都有一个通道。但是,其他的一些例子似乎表明,它应该是一个单通道矩阵,沿着列具有一个特征,每个样本一行。哪些是正确的?opencv kmeans聚类的输入矩阵

,如果我有一个5维特征向量,应该是什么,我使用的输入矩阵: 这一个:

cv::Mat inputSamples(numSamples, 1, CV32FC(numFeatures)) 

或者这一个:

cv::Mat inputSamples(numSamples, numFeatures, CV_32F) 

回答

28

正确的答案是cv::Mat inputSamples(numSamples, numFeatures, CV_32F) 。 在OpenCV文档约kmeanssays

样本 - 输入样本的浮点矩阵,每采样1行

所以它不是如在n维浮一个浮点矢量另一种选择。哪些例子表明了这种行为?

这里也是一个小例子,显示了如何使用kmeans。它簇的图像的像素,并显示结果:

#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 

using namespace cv; 

int main(int argc, char** argv) 
{ 
    Mat src = imread(argv[1], 1); 
    Mat samples(src.rows * src.cols, 3, CV_32F); 
    for(int y = 0; y < src.rows; y++) 
    for(int x = 0; x < src.cols; x++) 
     for(int z = 0; z < 3; z++) 
     samples.at<float>(y + x*src.rows, z) = src.at<Vec3b>(y,x)[z]; 


    int clusterCount = 15; 
    Mat labels; 
    int attempts = 5; 
    Mat centers; 
    kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers); 


    Mat new_image(src.size(), src.type()); 
    for(int y = 0; y < src.rows; y++) 
    for(int x = 0; x < src.cols; x++) 
    { 
     int cluster_idx = labels.at<int>(y + x*src.rows,0); 
     new_image.at<Vec3b>(y,x)[0] = centers.at<float>(cluster_idx, 0); 
     new_image.at<Vec3b>(y,x)[1] = centers.at<float>(cluster_idx, 1); 
     new_image.at<Vec3b>(y,x)[2] = centers.at<float>(cluster_idx, 2); 
    } 
    imshow("clustered image", new_image); 
    waitKey(0); 
} 
+0

我想知道你在循环之前在clusterCount变量声明中所做的事情,以及你在kmeans之后的for循环结尾所做的事情。你认为用这些信息更新答案是可能的吗?谢谢! – 2013-02-11 19:17:27

+0

第一个循环将来自图像的数据从(行,列,3)矩阵重新排列到(rows * cols,3)矩阵(每个像素一行)。最后的循环将图像中的每个像素替换为相应的聚类中心以进行可视化。 – sietschie 2013-02-12 09:25:07

+0

是否可以使用'Mat :: reshape()'而不是嵌套for循环? – Jayesh 2016-10-26 13:41:16

1

作为替代手动重塑输入矩阵,可以使用的OpenCV reshape函数来实现类似的结果用更少的代码。下面是降低颜色与K-means法(在Java中)计数的我的工作实现:

private final static int MAX_ITER = 10; 
private final static int CLUSTERS = 16; 

public static Mat colorMapKMeans(Mat img, int K, int maxIterations) { 

    Mat m = img.reshape(1, img.rows() * img.cols()); 
    m.convertTo(m, CvType.CV_32F); 

    Mat bestLabels = new Mat(m.rows(), 1, CvType.CV_8U); 
    Mat centroids = new Mat(K, 1, CvType.CV_32F); 
    Core.kmeans(m, K, bestLabels, 
       new TermCriteria(TermCriteria.COUNT | TermCriteria.EPS, maxIterations, 1E-5), 
       1, Core.KMEANS_RANDOM_CENTERS, centroids); 
    List<Integer> idx = new ArrayList<>(m.rows()); 
    Converters.Mat_to_vector_int(bestLabels, idx); 

    Mat imgMapped = new Mat(m.size(), m.type()); 
    for(int i = 0; i < idx.size(); i++) { 
     Mat row = imgMapped.row(i); 
     centroids.row(idx.get(i)).copyTo(row); 
    } 

    return imgMapped.reshape(3, img.rows()); 
} 

public static void main(String[] args) { 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
    Highgui.imwrite("result.png", 
     colorMapKMeans(Highgui.imread(args[0], Highgui.CV_LOAD_IMAGE_COLOR), 
      CLUSTERS, MAX_ITER)); 
} 

OpenCV的读取图像到二维,3信道矩阵。首先拨打reshape - img.reshape(1, img.rows() * img.cols()); - 实质上将3个通道展开为列。在结果矩阵中,一行对应于输入图像的一个像素,而3列对应于RGB分量。

后K-means算法完成其工作,并且颜色映射得到了应用,我们再次呼吁reshape - imgMapped.reshape(3, img.rows()),但现在滚动体列回通道,减少行数为原始图像的行数,从而找回了原始矩阵格式,但只有减少的颜色。

+0

I认为在采取这种方法之前,你需要注意图像是连续的。如果你使用克隆ie,你需要注意http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-iscontinuous – ejectamenta 2017-01-24 09:34:16

+0

。使用克隆,如img.clone()。reshape(1,img。rows()* img.cols()),那么图像将是连续的(并且原始图像将保持不变) – ejectamenta 2017-01-24 10:07:49