1

How to find the detected blob / object are overlapped and how to group it one? I have the labeled blob co-ordinates for example.

** Number of BLOBS Detected - 5 ** My Blob co-ordinates are as follows:

Starting X ,Starting Y - Blob Width - blob Height

100 100  100 100
125 125  80  90
80  80   70  50
130 130  10  10

10 10 5 8 

-- How to find the objects / rectangles are overlapped ? -- How to group it ?

** Our results should be **** **2 Blobs detected - 2 **

80 80 205 210
10 10 5 8 

How to achieve this ??

RoboMe
  • 113
  • 2
  • 13

2 Answers2

3

If I understand you right, your blobs can be regarded as cv::Rect(StartingX,StartingY, BlobWidth, BlobHeight)

in opencv c++, checking whether such Rects overlap is easy: rectA & rectB = rectOverlap where rectOverlap is the rect area covered by BOTH rects, so if rectOverlap has .width > 0 and c.height > 0 then the blobs/rects overlap.

here is sample code which creates some rectangles and computes whether they overlap or not:

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);


}

and here are input and output. you can see the detected overlap of the blue and red rectangles is the pink rect.

input:

enter image description here

output: no overlap between rectA and rectC and this image:

enter image description here

Micka
  • 19,585
  • 4
  • 56
  • 74
  • Please explain in more detail. `rectA & rectB = rectC ` ** what is happening here ** i didn't understand it. – RoboMe Apr 30 '14 at 08:30
1

Similar to here, you can check whether two rects overlap with each other or not as follows:

bool valueInRange(int value, int min, int max)
{ return (value &gt;= min) && (value &lt;= 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;
}

After this, you should merge all the overlopped rect groups into big rects (one group to one) to get the final result.

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174