基于c++計算矩形重疊面積代碼實例
在圖像處理中,經(jīng)常需要計算兩個矩形的重疊面積,在 python 中,可以使用 shapely 包中的 Polygon 函數(shù),但是到了 c++ 沒有想象中的那么簡單。
查閱了很多資料,基本上都是判斷兩個矩形是否包含來計算,但是兩個矩形的相交情況太多了,每個方法我都擔心考慮不全,所以想了一個在畫布上畫出矩形框,然后通過計算白點數(shù)或者輪廓的方法來計算面積。
但是就算用了這個方法,求取真正的重疊面積還差一個像素點,是否要加數(shù)值為1這個偏移量需要根據(jù)矩形的重疊情況來確定,這里不寫的那么精細,不考慮1個像素點的偏移。
所以本方法適合于計算重疊率,而不是重疊面積,因為重疊面積會根據(jù)矩形重疊情況的不同差0個或1個像素值。
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// 1. 新建一個畫布,把矩形畫在畫布上, 注意,矩形一定要在畫布里面,不能在畫布外面或者邊上
Mat canvaCaluateRectangleOverlap(100, 100, CV_8UC1, Scalar(0, 0, 0));
// 2. 把兩個矩形都畫在畫布上
Rect rect1 = Rect(10, 10, 20, 20);
Rect rect2 = Rect(20, 20, 20, 30);
//為了使用fillPoly填充畫布需要生成Point
Point rect1Point[1][4];
rect1Point[0][0] = Point(rect1.x, rect1.y);
rect1Point[0][1] = Point(rect1.x + rect1.width, rect1.y);
rect1Point[0][2] = Point(rect1.x + rect1.width, rect1.y + rect1.height);
rect1Point[0][3] = Point(rect1.x, rect1.y + rect1.height);
// 以下是用輪廓法計算矩形面積的方法,可以看看,但是實際使用當然還是 width*height
//vector<Point> rect1Contours;
//rect1Contours.push_back(Point(10, 10));
//rect1Contours.push_back(Point(30, 10));
//rect1Contours.push_back(Point(30, 30));
//rect1Contours.push_back(Point(10, 30));
//int rect1ContourArea = contourArea(rect1Contours);
//cout << "rect1ContourArea : " << rect1ContourArea << endl;
const Point* pointConst1[1] = { rect1Point[0] };
int npt[] = { 4 };
fillPoly(canvaCaluateRectangleOverlap, pointConst1, npt, 1, Scalar(255, 255, 255));
Point rect2Point[1][4];
rect2Point[0][0] = Point(rect2.x, rect2.y);
rect2Point[0][1] = Point(rect2.x + rect2.width, rect2.y);
rect2Point[0][2] = Point(rect2.x + rect2.width, rect2.y + rect2.height);
rect2Point[0][3] = Point(rect2.x, rect2.y + rect2.height);
const Point* pointConst2[1] = { rect2Point[0] };
fillPoly(canvaCaluateRectangleOverlap, pointConst2, npt, 1, Scalar(255, 255, 255));
// 3. 找出畫布的輪廓
vector<vector<Point> > canvaContours;
vector<Vec4i> hierarchy;
findContours(canvaCaluateRectangleOverlap, canvaContours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
// 修正結果的偏移量,會差1個或0個像素,這里不考慮這個,大家有時間可以列舉出每種情況計算出來
int offset = 0;
// 4. 對畫布輪廓進行判斷 (如果輪廓數(shù)等于1并且這兩個矩形不是相鄰就可以證明矩形是相交的)
if (canvaContours.size() == 1 && rect1.x+rect1.width != rect2.x && rect2.x+rect2.width != rect1.x && rect1.y+rect1.height != rect2.y && rect2.y+rect2.height != rect1.y)
{
// 當矩形相交時,用計算輪廓面積的方法計算出相交多邊形的面積(注意,這邊會差0個或1個偏移量,所以本方式最適合計算重疊率,一個近似的數(shù))
int canvaContourArea = contourArea(canvaContours[0]) + offset;
// 重疊的面積數(shù) = 兩個矩形面積的交集 - 兩個矩形面積的并集 (要理解這個,好好去畫圖你就明白了)
int rectangleOveralpArea = rect1.width * rect1.height + rect2.width * rect2.height - canvaContourArea;
cout << "The two rectangles are overlapping. " << endl;
cout << "retangleOverlapArea = " << rectangleOveralpArea << endl;
}
else {
cout << "The two rectangles do not overlap. " << endl;
}
return 0;
}
矩形重疊的方式很多,如下所示一部分

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++將音頻PCM數(shù)據(jù)封裝成wav文件的方法
這篇文章主要為大家詳細介紹了C++將音頻PCM數(shù)據(jù)封裝成wav文件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
select函數(shù)實現(xiàn)高性能IO多路訪問的關鍵示例深入解析
這篇文章主要為大家介紹了select函數(shù)實現(xiàn)高性能IO多路訪問的關鍵示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
c++ 構造函數(shù)中調(diào)用虛函數(shù)的實現(xiàn)方法
下面小編就為大家?guī)硪黄猚++ 構造函數(shù)中調(diào)用虛函數(shù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

