如何找到给定的矩形坐标是重叠的,如何将它们组合成一个如果? [英] How to find the given rectangle co-ordinqtes are overlapped and how to group it together as one if ?

查看:134
本文介绍了如何找到给定的矩形坐标是重叠的,如何将它们组合成一个如果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到给定的矩形坐标重叠?

如果矩形重叠,如何将它组合在一起?



这里给出了示例Exmaple:



格式 - Rect.x,Rect.y Rect.Width,Rect.Height



 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;







以上是我们需要的坐标查找哪个矩形重叠并将其分组。





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

使用 命名空间 cv;
使用 命名空间标准;

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 ::零(mImage.rows + 2,mImage.cols + 2,CV_8U);
// Mat mIMask = imread(,1);
// floodFill( mImage,CV ::点(55,70),CV ::标量(0,0,255),0,CV ::标量(nA_Val,nA_Val,nA_Val),CV ::标量(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: :标量(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 ::标量( 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 ;
}

解决方案

查找两个矩形是否重叠并不是那么困难,即

< pre lang =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)))
{
// 重叠
}



您可以使用> = < = 包含边缘情况。



您可以对矩形进行分组维护重叠的图表(例如,您可以使用邻接矩阵 [ ^ ]。


你需要编写一个函数进行比较s,并返回true或false结果。如果您使用的是Windows,则可以使用 IntersectRect 功能 [ ^ ]


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

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.


#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;
}

解决方案

Finding if two rectangles overlap is not that dramatically difficult, namely

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[^]).


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[^]


这篇关于如何找到给定的矩形坐标是重叠的,如何将它们组合成一个如果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆