I have a function that compares vectors in a vector and merges them if they are similar. I have to remember the indices of array elements that were already merged, so I save them in a queue.
On some random elements the insert of the index into the queue produces a seg fault. I really don't get why. Here is my code:
using namespace cv;
using namespace std;
//...
//...
unordered_set<uint> mergedIdxSet = unordered_set<uint>();
mergedIdxSet.reserve(regions.size());
//...
//...
cout << "Test region " << r1 << " (" << regions[r1].size() << ") with...\n";
for(uint r2 = r1+1; r2 < regions.size(); r2++)
{
if((bBoxes[r1] & bBoxes[r2]).area() == 0)
{
continue;
}
cout << "\tRegion " << r2 << "\n";
Rect unionBound = bBoxes[r1] | bBoxes[r2];
Point upperLeftBound = Point(unionBound.x, unionBound.y);
Mat interMap = Mat::zeros(unionBound.size(), CV_8UC1);
for(uint p = 0; p < regions[r1].size(); p++)
{
interMap.at<uchar>(regions[r1][p]-upperLeftBound) = 128;
}
for(uint p = 0; p < regions[r2].size(); p++)
{
interMap.at<uchar>(regions[r2][p]-upperLeftBound) += 127;
}
int intersectionArea = countNonZero(interMap);
if(intersectionArea / regions[r1].size() >= ratio ||
intersectionArea / regions[r2].size() >= ratio)
{
cout << "\t\tMerge\n";
mergedIdxSet.insert(r2);
for(uint i = 0; i < regions[r2].size(); i++)
{
pointSet.insert(regions[r2][i]);
}
regions[r1] = vector<Point>(pointSet.begin(), pointSet.end());
cout << "New length: " << regions[r1].size() << "\n";
}
}
The algorithm begins with region 0 and merges regions 1, 2, 3, 4, 5, 6 and 8. Then it wants to merge region 685 and crashes upon adding the unsigned int 685 in variable r2 into mergedIdxSet. If I just skip the insert it crashes when trying to insert regions[r2] into pointSet. If I skip the region 685 it crashes on some other region much later. Why does that happen?
Thanks in advance.
The problem was that I tried to overwrite the vector in regions[r1]. Somehow that caused the whole program to crash at the queue insert. I replaced the merging process by deleting the very similar regions and it works now.
Thanks for the input.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.