Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to find the given rectangle co-ordinates are overlapped ?
How to group it together one if rectangles are overlapped?

Here given example Exmaple:

Format - Rect.x , Rect.y Rect.Width , Rect.Height

XML
vector<Rect>boundRect(8);

boundRect[0].x = 260;   boundRect[0].y = 77;    boundRect[0].width = 90;    boundRect[0].height  = 150;
boundRect[1].x = 307;   boundRect[1].y = 227;   boundRect[1].width = 51;    boundRect[1].height  = 15;
boundRect[2].x = 358;   boundRect[2].y = 214;   boundRect[2].width = 41;    boundRect[2].height  = 51;
boundRect[3].x = 329;   boundRect[3].y = 261;   boundRect[3].width = 37;    boundRect[3].height  = 11;

boundRect[4].x = 204;   boundRect[4].y = 272;   boundRect[4].width = 241;   boundRect[4].height  = 146;
boundRect[5].x = 210;   boundRect[5].y = 263;   boundRect[5].width = 9;     boundRect[5].height  = 9;
boundRect[6].x = 184;   boundRect[6].y = 362;   boundRect[6].width = 32;    boundRect[6].height  = 64;
boundRect[7].x = 551;   boundRect[7].y = 345;   boundRect[7].width = 27;    boundRect[7].height  = 25;



The above are the co-ordinates , we need to find which rectangle are overlapped and group it.


C#
#include "opencv2/opencv.hpp"
#include <string>

using namespace cv;
using namespace std;

int main()
{
    int nA_Val = 3;
    int nMax_Val = 150;
    Mat mImage = imread("TButterfly.jpg",1);
    //cv::floodFill(mImage, cv::Point(150,150), cv::Scalar(255.0, 255.0, 255.0));
    //Mat mask = imread("Mask2.jpg",0);
    //cv::Mat mask = cv::Mat::zeros(mImage.rows + 2, mImage.cols + 2, CV_8U);
    //  Mat mIMask = imread("",1);
    //  floodFill(mImage,cv::Point(55,70),cv::Scalar(0,0,255),0,cv::Scalar(nA_Val,nA_Val,nA_Val),cv::Scalar(nMax_Val,nMax_Val,nMax_Val),4);
//  cv::floodFill(mImage, mask, cv::Point(210,148), cv::Scalar(0,0,255), 0, cv::Scalar(nA_Val,nA_Val,nA_Val),
//      cv::Scalar(nMax_Val,nMax_Val,nMax_Val),  4 + (255 << 8) + cv::FLOODFILL_FIXED_RANGE     );

//  cv::floodFill(mImage, mask, cv::Point(404,231), cv::Scalar(0,255,255), 0, cv::Scalar(nA_Val,nA_Val,nA_Val),
//      cv::Scalar(nMax_Val,nMax_Val,nMax_Val),  4 + (255 << 8) + cv::FLOODFILL_FIXED_RANGE    );
        floodFill(mImage,cv::Point(71,62),cv::Scalar(0,0,255),0,cv::Scalar(nA_Val,nA_Val,nA_Val),cv::Scalar(nMax_Val,nMax_Val,nMax_Val),4);
    //  floodFill(mImage,cv::Point(202,350),cv::Scalar(0,255,255),0,cv::Scalar(nA_Val,nA_Val,nA_Val),cv::Scalar(nMax_Val,nMax_Val,nMax_Val),4);
    erode(mImage,mImage,Mat());
    imshow("mImage",mImage);
    imwrite("mImage.jpg",mImage);
    waitKey(0);
    return 0;
}
Posted
Updated 13-Jun-14 2:33am
v2

Finding if two rectangles overlap is not that dramatically difficult, namely
C++
if (((r0.x0 < r1.x0 && r0.x1 > r1.x0) || ( r1.x0 < r0.x0 && r1.x1 > r0.x0 )) && (r0.y0 < r1.y0 && r0.y1 > r1.y0) || ( r1.y0 < r0.y0 && r1.y1 > r0.y0 )))
{
  // overlap
}

You may use >= and <= to include edge cases.

You may group the rectagles maintaining a graph of the overlapping ones (you may use, for instance, an adjacency matrix[^]).
 
Share this answer
 
You would need to write a function that does the comparisons, and returns a true or false result. If you are using Windows then you can make use of the IntersectRect function[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900