1

我有一个包含图像像素坐标的 Qvector。主要目标是根据它们彼此的距离对这些像素进行分组,并从这些像素组中得到一个矩形。向量中的每个像素彼此并不靠近,这就是我想对它们进行分组的原因。

我正在使用 openCv 和 Qt。我想避免 OpenCV 中的 blobDetector 非常慢,如果可能的话,我自己来做。

有谁知道如何管理这个?

编辑:

假设白点是彼此靠近的像素。主要目标是检测这些像素是否彼此靠近并能够获得这些像素的矩形。这可能吗 ?

在此处输入图像描述

编辑2:

获得集群后,我尝试使用以下代码获取这些集群周围的边界矩形。我可能没有以正确的方式使用此功能。

cv::partition(cvCoordsTable, testVector, Dist(eqludianThreshold));
std::vector<cv::Rect> rectTable;

for(int in = 0; in < testVector.size(); in++)
{
    rectTable.push_back(cv::boundingRect(cvCoordsTable.at(in)));
}

感谢您的帮助

4

2 回答 2

3

这首先是聚类问题。由于您不知道集群(组)的数量,因此您必须使用一些不需要集群数量作为输入的算法。您可以执行简单的cv::partition 在 C++ 中具有以下签名:

int cv::partition(const vector<_Tp>& vec, vector<int>& labels, _EqPredicate predicate=_EqPredicate())

使用它的例子:

    std::vector<cv::Point> pixelCoordinatesTable,
    std::vector<int> labelsTable;
    double threshold= 5;//Max eqludian distance between one cluster points
    cv::partition(pixelCoordinatesTable, labelsTable, [&threshold](auto const& l, auto const& r){
        return cv::norm(l - r))<threshold;
    });

另一个更成熟的选择是使用真正的聚类算法,如DBSCAN。这是一种基于密度的聚类算法。你可以在这里找到一个 C++实现

在你得到集群之后(以任何方法),只需cv::boundingRect在每个集群周围应用cluster以获得rectangle你想要的。

编辑:

解决矩形问题:

auto cluster_count = cv::partition(cvCoordsTable, testVector, Dist(eqludianThreshold)); // gettting the number of clusters
std::vector<cv::Rect> rectTable;
rectTable.reserve(cluster_count);//Optimiaztion
for(int in = 0; in < cluster_count; in++){
    std::vector<cv::Point> temp;
    temp.reserve(testVector.size());
    for(size_t i=0;i<testVector.size();++i){
        if(testVector[i]==in){
             temp.emplace_back(rectTable[i]);
        }
    }
    rectTable.emplace_back(cv::boundingRect(temp));
}

我相信有更好更快的方法,我只是在解释这个想法,你可以尽可能优化它。

于 2016-02-09T09:29:29.247 回答
2

@Humam Helfawi 在这里击败了我,但无论如何,如果您有中心点列表,请使用 cv::partition 进行(无监督)聚类:

struct Dist
{
    double D;
    Dist(double d) : D(d) {}
    bool operator()(const Point &a, const Point &b)
    {
        return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) < D;
    }
};
vector<Point> centers; // e.g. from findContours -> boundingRect
vector<int> labels;
cv::partition(centers,labels,Dist(800));
cerr << Mat(labels).t() << endl;

[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ]

于 2016-02-09T09:46:11.750 回答