【问题标题】:How to use clustering with opencv c++ to classify the connected component based on the area and height如何使用opencv c ++进行聚类根据面积和高度对连通分量进行分类
【发布时间】:2014-03-23 03:25:33
【问题描述】:

您好,使用opencv c++,我想做聚类,根据面积和高度对连通分量进行分类。 我确实了解集群的概念,但我很难在 opencv c++ 中实现它。

在opencv中

http://docs.opencv.org/modules/core/doc/clustering.html

有一个聚类方法kmeans

我搜索的大部分网站,他们只是解释opencv c++中kmeans函数的概念和参数,大部分是从opencv文档网站复制的。

double kmeans(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray() )

这里也有很好的例子,但它是用 Python 实现的

http://docs.opencv.org/trunk/doc/py_tutorials/py_ml/py_kmeans/py_kmeans_opencv/py_kmeans_opencv.html?highlight=kmeans

正如我上面提到的,我有所有的连接组件,我可以计算每个连接组件的面积和高度。

我想使用聚类来区分连接的组件。

例如,对于 k-means 方法,我将使用 k=2。

谢谢..

【问题讨论】:

标签: c++ opencv image-processing components hierarchical-clustering


【解决方案1】:

签出this

除了迭代 x、y 和 z 之外,您将迭代组件和属性(面积和高度)。

【讨论】:

    【解决方案2】:

    我正在发布sn-p,希望这会对你有所帮助.... 组件的高度和面积可以用作 kmean 的特征。现在,对于每个功能,kmean 都会为您提供中心。即区域的 1 个中心和组件高度的 1 个中心。

    Mat labels;
    int attempts = 11;
    Mat centers;
    int no_of_features = 2;//(i.e. height, area)
    Mat samples(no_of_connected_components, no_of_features, CV_32F);
    int no_of_sub_classes = 1; // vary for more sub classes
    
    for (int j = 0; j < no_of_connected_components; j++)
    {
        for (int x = 0; x < no_of_features; x++)
        {
            samples.at<float>(j, x) = connected_component_values[j,x];
            //fill the values(height, area) of connected component labelling
        }
    }
    
    cv::kmeans(samples, no_of_sub_classes, labels, TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10000, 0.001), attempts, KMEANS_PP_CENTERS, centers);
    
    for (size_t si_i = 0; si_i < no_of_sub_classes ; si_i++)
    {
        for (size_t si_j = 0; si_j < no_of_features; si_j++)
        {
            KmeanTable[si_i*no_of_sub_classes + si_i][si_j] = centers.at<float>(si_i, si_j);
        }
    
    }
    

    在这里,我将中心存储在 kmeanTable 2D 数组中,您可以使用您的。现在对于每个连接的组件,您可以计算到中心的欧几里得距离。 较低差异的特征有资格进行分类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-10
      • 2015-07-20
      • 2016-05-13
      • 2020-11-17
      • 2020-09-21
      • 2017-11-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多