我在条形码图像中检测到了轮廓,并为每个条形图计算了最小值。图像中还有其他对象,因此我希望通过找到类似角度矩形的簇来隔离条形码。
我是C ++ opencv的新手,所以我不确定我应该采用什么方法。有什么建议吗?
答案 0 :(得分:0)
vector<vector<int>> RAPC(vector<RotatedRect> minRect, float radius) {
vector<vector<int>> clusters; // vector that will contain identify who belongs to which cluster
vector<bool> touched(minRect.size(), false); // initializing vector that will identify whether or not a rectangle has been assigned to a cluster
// for every rect
for (int i = 0; i < minRect.size()-1; i++)
{
// compare to every other rect
for (int n = (i + 1); n < minRect.size(); n++)
{
// distance from rect i to rect n
float distance = sqrt(pow((minRect[i].center.x - minRect[n].center.x), 2)
+ pow((minRect[i].center.y - minRect[n].center.y), 2));
//criteria to determine whether the two rectangles belong in the same cluster
if ( distance < radius)
{
// if neither rectangle has been assigned to a cluster yet
if (touched[i] == false && touched[n] == false) {
// create new cluster in clusters with i and n as members
clusters.emplace_back(initializer_list<int>{i, n});
touched[i] = true; // make note of i being placed
touched[n] = true; // make note of n being placed
}
// if i has already been assigned to a cluster
else if (touched[i] == true && touched[n] == false) {
int loc = findLoc(i, clusters); // find i in clusters
clusters[loc].emplace_back(n); // place n in that cluster
touched[n] = true; // make note of n being placed
}
// if n has already been assigned to a cluster
else if (touched[n] == true && touched[i] == false) {
int loc = findLoc(n, clusters); // find n in clusters
clusters[loc].emplace_back(i); // place n in that cluster
touched[i] = true; // make note of i being placed
}
// if both rectangles have already been assigned
else (touched[i] == true && touched[n] == true); {
int locI = findLoc(i, clusters); // search for location of i
int locN = findLoc(n, clusters); // search for location of n
// if both rectangles are already part of the same cluster, ignore
if (locI == locN) {
break;
}
// concatenate vector n with vector i
clusters[locI].insert(clusters[locI].end(), clusters[locN].begin(), clusters[locN].end());
// erase original location of vector n
clusters[locN].erase(clusters[locN].begin(), clusters[locN].end());
}
}
}
}
return clusters;
}
// function used by RAPC mainly just to make code more concise
int findLoc(int numSearchedFor, vector<vector<int>> clusters) {
bool searchComplete = false;
int ans; // initializing function output ie the cluster ID where the value is found
for (int i = 0; i < clusters.size(); i++) {
for (int n = 0; n < clusters[i].size(); n++) {
if (clusters[i][n] == numSearchedFor) {
ans = i;
searchComplete = true;
break;
}
}
if (searchComplete == true) {
break;
}
}
return ans;
}