c++ 計算兩個矩形重疊面積 (粗略版)(c++ calculate the overlap area of two rectangles, a rough version)


 

在圖像處理中,經常需要計算兩個矩形的重疊面積,在 python 中,可以使用 shapely 包中的 Polygon 函數,但是到了 c++ 沒有想象中的那么簡單。

查閱了很多資料,基本上都是判斷兩個矩形是否包含來計算,但是兩個矩形的相交情況太多了,每個方法我都擔心考慮不全,所以想了一個在畫布上畫出矩形框,然后通過計算白點數或者輪廓的方法來計算面積。

但是就算用了這個方法,求取真正的重疊面積還差一個像素點,是否要加數值為1這個偏移量需要根據矩形的重疊情況來確定,這里不寫的那么精細,不考慮1個像素點的偏移。

所以本方法適合於計算重疊率,而不是重疊面積,因為重疊面積會根據矩形重疊情況的不同差0個或1個像素值。

 

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 
 4 using namespace std;  5 using namespace cv;  6 
 7 int main()  8 {  9     // 1. 新建一個畫布,把矩形畫在畫布上, 注意,矩形一定要在畫布里面,不能在畫布外面或者邊上
10     Mat canvaCaluateRectangleOverlap(100, 100, CV_8UC1, Scalar(0, 0, 0)); 11     
12     // 2. 把兩個矩形都畫在畫布上
13     Rect rect1 = Rect(10, 10, 20, 20); 14     Rect rect2 = Rect(20, 20, 20, 30); 15     //為了使用fillPoly填充畫布需要生成Point
16     Point rect1Point[1][4]; 17     rect1Point[0][0] = Point(rect1.x, rect1.y); 18     rect1Point[0][1] = Point(rect1.x + rect1.width, rect1.y); 19     rect1Point[0][2] = Point(rect1.x + rect1.width, rect1.y + rect1.height); 20     rect1Point[0][3] = Point(rect1.x, rect1.y + rect1.height); 21 
22     // 以下是用輪廓法計算矩形面積的方法,可以看看,但是實際使用當然還是 width*height 23     //vector<Point> rect1Contours; 24     //rect1Contours.push_back(Point(10, 10)); 25     //rect1Contours.push_back(Point(30, 10)); 26     //rect1Contours.push_back(Point(30, 30)); 27     //rect1Contours.push_back(Point(10, 30)); 28     //int rect1ContourArea = contourArea(rect1Contours); 29     //cout << "rect1ContourArea : " << rect1ContourArea << endl;
30 
31     const Point* pointConst1[1] = { rect1Point[0] }; 32     int npt[] = { 4 }; 33     fillPoly(canvaCaluateRectangleOverlap, pointConst1, npt, 1, Scalar(255, 255, 255)); 34 
35     Point rect2Point[1][4]; 36     rect2Point[0][0] = Point(rect2.x, rect2.y); 37     rect2Point[0][1] = Point(rect2.x + rect2.width, rect2.y); 38     rect2Point[0][2] = Point(rect2.x + rect2.width, rect2.y + rect2.height); 39     rect2Point[0][3] = Point(rect2.x, rect2.y + rect2.height); 40     const Point* pointConst2[1] = { rect2Point[0] }; 41     fillPoly(canvaCaluateRectangleOverlap, pointConst2, npt, 1, Scalar(255, 255, 255)); 42 
43     // 3. 找出畫布的輪廓
44     vector<vector<Point> > canvaContours; 45     vector<Vec4i> hierarchy; 46     findContours(canvaCaluateRectangleOverlap, canvaContours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0)); 47     
48     // 修正結果的偏移量,會差1個或0個像素,這里不考慮這個,大家有時間可以列舉出每種情況計算出來
49     int offset = 0; 50     // 4. 對畫布輪廓進行判斷 (如果輪廓數等於1並且這兩個矩形不是相鄰就可以證明矩形是相交的)
51     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) 52  { 53         // 當矩形相交時,用計算輪廓面積的方法計算出相交多邊形的面積(注意,這邊會差0個或1個偏移量,所以本方式最適合計算重疊率,一個近似的數)
54         int canvaContourArea = contourArea(canvaContours[0]) + offset; 55         // 重疊的面積數 = 兩個矩形面積的交集 - 兩個矩形面積的並集 (要理解這個,好好去畫圖你就明白了)
56         int rectangleOveralpArea = rect1.width * rect1.height + rect2.width * rect2.height - canvaContourArea; 57         cout << "The two rectangles are overlapping. " << endl; 58         cout << "retangleOverlapArea = " << rectangleOveralpArea << endl; 59  } 60     else { 61         cout << "The two rectangles do not overlap. " << endl; 62  } 63     return 0; 64 }

 

 

矩形重疊的方式很多,如下所示一部分,有時間和精力的同學可以試下算出所有矩形相交情況的偏移量,姐姐我就先歇歇啦。

 

 

本方法或許還有一些本人考慮不周全的地方,有錯誤歡迎大家指正,謝謝。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM