如何找到检测到的斑点/对象重叠以及如何将其分组? 例如,我有标记的blob坐标。
**检测到的BLOBS数量 - 5 ** 我的Blob坐标如下:
开始X,开始Y - Blob宽度 - blob高度
100 100 100 100
125 125 80 90
80 80 70 50
130 130 10 10
10 10 5 8
- 如何找到对象/矩形重叠? - 如何分组?
**我们的结果应该是**** **检测到2个Blob - 2 **
80 80 205 210
10 10 5 8
如何实现这个?
答案 0 :(得分:3)
如果我理解你的话,你的blob可以被视为cv::Rect(StartingX,StartingY, BlobWidth, BlobHeight)
在opencv c++
中,检查这些Rects重叠是否容易:rectA & rectB = rectOverlap
其中rectOverlap是由BOTH rects覆盖的矩形区域,因此如果rectOverlap有.width > 0
和c.height > 0
然后blob / rects重叠。
这里是示例代码,它创建一些矩形并计算它们是否重叠:
int main()
{
cv::Mat sample = cv::Mat(512,512,CV_8UC3, cv::Scalar(0,0,0));
// create sample rectangles: positionsX, positionY, width, height
cv::Rect rectA(100,50,50,200);
cv::Rect rectB(50,100,200,50);
cv::Rect rectC(400,50,100,100);
// draw in different colors:
cv::rectangle(sample, rectA, cv::Scalar(0,0,255));
cv::rectangle(sample, rectB, cv::Scalar(255,0,0));
cv::rectangle(sample, rectC, cv::Scalar(0,255,0));
// create output
cv::Mat result = cv::Mat(512,512,CV_8UC3, cv::Scalar(0,0,0));
// compute overlap with overloaded & operator for cv::Rect
cv::Rect overlapAB = rectA & rectB;
cv::Rect overlapAC = rectA & rectC;
// test for overlap and draw or terminal output
if(overlapAB.width && overlapAB.height) cv::rectangle(result, overlapAB, cv::Scalar(255,0,255), -1);
else std::cout << " no overlap between rectA and rectB" << std::endl;
if(overlapAC.width && overlapAC.height) cv::rectangle(result, overlapAC, cv::Scalar(0,255,255), -1);
else std::cout << " no overlap between rectA and rectC" << std::endl;
cv::imshow("rects", sample);
cv::imshow("overlap", result);
cv::imwrite("RectOverlapInput.png", sample);
cv::imwrite("RectOverlapOutput.png", result);
cv::waitKey(-1);
}
这里是输入和输出。你可以看到检测到的蓝色和红色矩形的重叠是粉红色矩形。
输入:
输出:no overlap between rectA and rectC
和此图片:
答案 1 :(得分:1)
与here类似,您可以检查两个rects是否相互重叠,如下所示:
bool valueInRange(int value, int min, int max)
{ return (value >= min) && (value <= max); }
bool RectOverlap(Rect A, Rect B)
{
bool xOverlap = valueInRange(A.x, B.x, B.x + B.width) ||
valueInRange(B.x, A.x, A.x + A.width);
bool yOverlap = valueInRange(A.y, B.y, B.y + B.height) ||
valueInRange(B.y, A.y, A.y + A.height);
return xOverlap && yOverlap;
}
在此之后,您应该将所有过度使用的矩形组合并为大的组(一组到一组)以获得最终结果。