Rect数据结构经常是在OpenCV中被用来表示为一个矩形尺寸,其成员包括x,y, width,height,其中x和y分别表示矩形框的左上角的起始点坐标,width和height分别表示宽和高。

Rect

OpenCV中预定义好的几种Rect,用来支持不同的数据类型:

Rect2i:整型int

Rect2f: float

Rect2d: double

源代码定义如下:

其中Rect与Rect2i相同都是整型,实现主要依靠的是Rect_类 和Size,Point类似

Rect_类

Rect_类主要如下:

总结如下:

Method Description
Rect_()

默认构造函数

cv::Rect r;

cv:Rect2f rf;

cv::Rect2d rd

Rect_(const Rect_& r) copy构造函数
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)

带参数构造函数

x,y为矩形的左上角坐标点

_width,_height分别为宽和高

Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz)

根据一个点和Size构造Rect

org:为矩形左上角

sz:为矩形宽和高

Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2)

利用两个对角点 构造Rect

x = std::min(pt1.x, pt2.x);
 y = std::min(pt1.y, pt2.y);
 width = std::max(pt1.x, pt2.x) - x;
  height = std::max(pt1.y, pt2.y) - y;

_Tp x; 
_Tp y; 
_Tp width; 
_Tp height;

x,y为矩形的左上角坐标点

width,height分别为宽和高

保存到类中的值

_Tp area() 矩形面积:(width*height)
Point_<_Tp> tl() 返回矩形的左上边角点坐标
Point_<_Tp> br()  返回右下边角点坐标
Size_<_Tp> size() size形式返回矩形的width, height
 bool empty()

true: width <= 0或 height <= 0

false:  width>0 且height>0

bool contains(const Point_<_Tp>& pt)

检查 pt是否在矩形内:

true:在矩形框内

false:不在矩形框内

使用用例如下:

#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"using namespace cv;
using namespace std;void main()
{Rect r;Rect2f rf;Rect2d rd;cout << "r.empty=:" << r.empty() << endl;cout << "f.empty=:" << rf.empty() << endl;cout << "rd.empty=:" << rd.empty() << endl;Rect r2(0,0,300,400);Rect2f rf2(0, 0, 300.1, 400.4);Rect2d rd2(2, 2, 2324, 2142424);cout << "r2=:" << r2 <<endl;cout << "r2.tl=:" << r2.tl() << endl;cout << "r2.br=:" << r2.br() << endl;cout << "r2.size=:" << r2.size() << endl;cout << "r2.area=:" << r2.area() << endl;cout << "r2.empty=:" << r2.empty() << endl;cout << "**********************" << endl;cout << "rf2=:" << rf2 << endl;cout << "rf2.tl=:" << rf2.tl() << endl;cout << "rf2.br=:" << rf2.br() << endl;cout << "rf2.size=:" << rf2.size() << endl;cout << "rf2.area=:" << rf2.area() << endl;cout << "rf2.empty=:" << rf2.empty() << endl;cout << "rd2.contains(Point(200,200))=:" << rd2.contains(Point(200, 200)) << endl;cout << "**********************" << endl;cout << "rd2=:" << rd2 << endl;cout << "rd2.tl=:" << rd2.tl() << endl;cout << "rd2.br=:" << rd2.br() << endl;cout << "rd2.size=:" << rd2.size() << endl;cout << "rd2.area=:" << rd2.area() << endl;cout << "rd2.empty=:" << rd2.empty() << endl;Rect2f rf3(rf2);rf3.x = 500;rf3.y = 600;cout << "rf3=:" << rf3 << endl;
}

运行结果:

Rect_类operator重构

Rect_类通过operator重构的操作:

operator Method
 +=:

template<typename _Tp> static inline
Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Point_<_Tp>& b )

左上角点按照b偏移相当于平移矩阵:

a.x += b.x;
    a.y += b.y;

template<typename _Tp> static inline
Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Size_<_Tp>& b )

width和heigh按照b扩大,扩展矩形区域:

a.width  += b.width;

a.height += b.height

-=:

template<typename _Tp> static inline
Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Point_<_Tp>& b )

相当于平移矩阵

template<typename _Tp> static inline
Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Size_<_Tp>& b )

缩小矩形区域

&=:

template<typename _Tp> static inline
Rect_<_Tp>& operator &= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )

取两个矩形框交集部分

 |= 

template<typename _Tp> static inline
Rect_<_Tp>& operator |= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )

尽量取两个矩形框并集即所覆盖的全部面积

==

template<typename _Tp> static inline
bool operator == (const Rect_<_Tp>& a, const Rect_<_Tp>& b)

两个矩阵完全相等(包括x,y,width,height)返回true

!=

template<typename _Tp> static inline
bool operator != (const Rect_<_Tp>& a, const Rect_<_Tp>& b)

两个矩阵不相等返回true

+

template<typename _Tp> static inline
Rect_<_Tp> operator + (const Rect_<_Tp>& a, const Point_<_Tp>& b)

左上角x和y的位置右偏移b大小,相当于平移矩阵

template<typename _Tp> static inline
Rect_<_Tp> operator + (const Rect_<_Tp>& a, const Size_<_Tp>& b)

长和宽扩大b大小,扩展矩形区域

-

template<typename _Tp> static inline
Rect_<_Tp> operator - (const Rect_<_Tp>& a, const Point_<_Tp>& b)

左上角x和y的位置左偏移b大小,相当于平移矩阵

template<typename _Tp> static inline
Rect_<_Tp> operator - (const Rect_<_Tp>& a, const Size_<_Tp>& b)

长和宽缩短b大小,缩小矩形区域

&

template<typename _Tp> static inline
Rect_<_Tp> operator & (const Rect_<_Tp>& a, const Rect_<_Tp>& b)

取两个矩形框交集部分

|

template<typename _Tp> static inline
Rect_<_Tp> operator | (const Rect_<_Tp>& a, const Rect_<_Tp>& b)

取两个矩形框并集即所覆盖的全部面积

+、- Point平移:

+、-按照Point平移如下(宽度和高度不变):

假设

Rect a

Point b

Rect c= a+b

+、-Size

+、- Size只修改宽度和高度,x和y不变:

假设

Rect a

Size b

Rect c= a+b

如下图所示:

Point和Size偏移用例

分别按照Point和Size进行右偏移用例:

#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"using namespace cv;
using namespace std;void main()
{Rect r(0,0,300,400);cout << "r=: " << r << endl;cout << "r+Point(2,3)=: " << r + Point(2, 3)<< endl;cout << "r+Size(200,300)=: " << r + Size(200, 300) << endl; }

运行结果:

r=: [300 x 400 from (0, 0)]
       r+Point(2,3)=: [300 x 400 from (2, 3)]
       r+Size(200,300)=: [500 x 700 from (0, 0)]

&和&=操作

&和&= 操作为取两个矩阵交集:

假设

Rect a

Rect b

Rect c= a&b

C为a和b两个矩阵的交集,如下图:

图中红框的部分为c

,两个矩阵的交集部分。

|和|=操作

|和|=操作为取两个矩阵最大并集:

假设

Rect a

Rect b

Rect c= a|b

则c矩阵的大小为:

&和| 用例

两个矩形框 &和| 用例:

#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"using namespace cv;
using namespace std;void main()
{Rect r1(0,0,300,400);Rect r2(5, 5, 500, 600);cout << "r1&r2=: " << (r1&r2) << endl;cout << "r1|r2" << (r1|r2) << endl;Rect r3(0, 0, 100, 200);Rect r4(300, 400, 100, 200);cout << "r3&r4=: " << (r3&r4) << endl;cout << "r3|r4" << (r3 | r4) << endl;
}

运行结果:

Rect结合Mat截取图片

可以利用Rect来截取Mat中感兴趣中ROI部分,下面用例用用来截取图片中感兴趣中的一部分:

#include <stdio.h>
#include "opencv2/opencv.hpp"using namespace cv;void main()
{Mat srcImage = imread("len_top.jpg");printf("Image height:%d, width:%d\n", srcImage.rows, srcImage.cols);imshow("Image", srcImage);waitKey(0);Mat dstImage(srcImage, Rect(50, 50, 250, 150));imshow("Rect Image", dstImage);waitKey(0);}

运行结果:

原始加载的图片为:

截取的感兴趣中的一部分结果为:

OpenCV中基本数据结构(4)_Rect相关推荐

  1. OPENCV中图像数据结构及其转化

    OPENCV中图像数据结构及其转化 1. IplImage 它是openCV库中表示图像的结构体. 初始化: cvLoadImage(),cvCreateImage() 访问元素:[行指针] b = ...

  2. OPENCV中的数据结构总结

    最近在写自己的算法,其实就是对一些传统算法的改进.传统算法可以参考opecv的源代码.在阅读源代码的过程中,我慢慢领会到了opencv的强大之处,并不是因为它实现了各种算法,而是在于它对于基本数据结构 ...

  3. OpenCV中的数据结构

    首先介绍2维点对Point_,它的是一个模板类.我们可以直接访问数据成员x,y.它不仅定了+.-.==.!=这4个基本的操作,还定义了点乘.叉乘等操作.特别的这个类还提供了inside函数来判断一个点 ...

  4. OpenCV中基本数据结构(1)_Point

    为了便于对一些常见的数据进行操作,OpenCV定义了一些常见的数据结构(Point ,Scalar等),以方便后续对数据算法的实现,主要分为basic data type.helper objects ...

  5. OpenCV中基本数据结构(8)_Complex

    Complex为OpenCV中的复数表示类,与STL 中的complex类似但是不一样,两者可以互换,与STL的complex最大的不同是,STL中获取到实部和虚部的值 分别使用real()和imag ...

  6. OpenCV中基本数据结构(6)_Matx

    Matx为OpenCV轻量级的矩阵,被称为fixed matrix classes,意思是每个矩阵的大小都是固定的,主要是应对矩阵数据比较小的场景,最新的版本4.0不超过6*6大小的矩阵,旧版本一般不 ...

  7. OpenCV中基本数据结构(7)_Vec

    Vec系列数据结构是Matx的一个派生类,其矩阵的行的大小永远固定为一行,列大小从1到6不等,可以认为类似与C++ vector,但与C++ 的vector又有很大不同,,以下不同来自于一段技术博客, ...

  8. OpenCV中基本数据结构(5)_RotatedRect

    RotatedRect也是表示一个矩形框,但是与Rect不同的是,RotatedRect可以带倾斜角的矩形,如下图所示: RotatedRect结构中包括三个变量: Point2f center:为矩 ...

  9. OpenCV中Mat数据结构使用举例

    #include "stdafx.h" #include <string> #include <iostream> #include <opencv2 ...

最新文章

  1. SD-WAN — 云专线(企业入云)
  2. hdu3535 (分组背包,最少选一 + 最多选一 + 随意)
  3. ConsumerCoordinator分析
  4. Python day13文件的读写
  5. Linux下添加eigen头文件,移植Eigen库到SylixOS下及使用方法
  6. 解决百度 ueditor v1.4.3 编辑器上传图片失真的bug?
  7. MyBatis---动态SQL
  8. 全网首发:github已经设置了令牌token,为什么还要验证用户密码
  9. linux系统怎么关闭445端口,如何关闭window 系统的445端口等方法 预防勒索病毒
  10. 南京大学俞扬博士AIDL演讲全文:强化学习前沿(上)
  11. java 中文词频统计_Java实现中文词频统计
  12. 鸿蒙系统学习系列 | 上手HarmonyOS十大必看指南!
  13. python numpy 版本问题:error module compiled against API version 0xc but this version of numpy is 0xb
  14. wps excel 多数字文本格式转换为数字(不会以科学计数法显示)
  15. JavaScript的佛祖保佑
  16. CV——基于Stitcher类实现图片拼接
  17. 西安交大计算机在线作业答案,西安交大电路在线作业及答案.docx
  18. Ubuntu下安装mer-gx系列相机驱动
  19. 数字化时代App们将何去何从?
  20. 从携程瘫痪事件看运维的85条军规

热门文章

  1. PDM 物理模型codename转大写
  2. vue2.0项目结构和打包发布
  3. Tensorflow+gpu安装
  4. 同一master,两个slave的server_id相同问题处理
  5. SQL Server下载安装
  6. Linux DNS视图脑裂的实例操作(四)
  7. (转)一台服务器安装两个tomcat6 服务的解决方案
  8. cisco LAP upgrade to Fat AP
  9. NBear.Mapping使用教程(5):实体对象与NameValueCollection,Dicitonary以及NBear.Mapping性能
  10. Juniper大中国区于肇烈