服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 基于c++计算矩形重叠面积代码实例

基于c++计算矩形重叠面积代码实例

2021-09-17 10:50ttweixiao9999 C/C++

这篇文章主要介绍了基于c++计算矩形重叠面积代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在图像处理中,经常需要计算两个矩形的重叠面积,在 python 中,可以使用 shapely 包中的 Polygon 函数,但是到了 c++ 没有想象中的那么简单。

查阅了很多资料,基本上都是判断两个矩形是否包含来计算,但是两个矩形的相交情况太多了,每个方法我都担心考虑不全,所以想了一个在画布上画出矩形框,然后通过计算白点数或者轮廓的方法来计算面积。

但是就算用了这个方法,求取真正的重叠面积还差一个像素点,是否要加数值为1这个偏移量需要根据矩形的重叠情况来确定,这里不写的那么精细,不考虑1个像素点的偏移。

所以本方法适合于计算重叠率,而不是重叠面积,因为重叠面积会根据矩形重叠情况的不同差0个或1个像素值。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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. 对画布轮廓进行判断 (如果轮廓数等于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个偏移量,所以本方式最适合计算重叠率,一个近似的数)
    int canvaContourArea = contourArea(canvaContours[0]) + offset;
    // 重叠的面积数 = 两个矩形面积的交集 - 两个矩形面积的并集 (要理解这个,好好去画图你就明白了)
    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;
}

矩形重叠的方式很多,如下所示一部分

基于c++计算矩形重叠面积代码实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/ttweixiao-IT-program/p/12506226.html

延伸 · 阅读

精彩推荐