一、Rect类

template<typename _Tp> class Rect_
{
public:typedef _Tp value_type;//! various constructorsRect_();Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);Rect_(const Rect_& r);Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);Rect_& operator = ( const Rect_& r );//! the top-left cornerPoint_<_Tp> tl() const;//! the bottom-right cornerPoint_<_Tp> br() const;//! size (width, height) of the rectangleSize_<_Tp> size() const;//! area (width*height) of the rectangle_Tp area() const;//! conversion to another data typetemplate<typename _Tp2> operator Rect_<_Tp2>() const;//! checks whether the rectangle contains the pointbool contains(const Point_<_Tp>& pt) const;_Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle
};

Rect_(),形参列表为空,即定义一个空窗口(默认值为:x=y=width=height=0);
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height),定义一个左上角点坐标为(_x, _y)的_width*_height矩形窗口;( 这个最常用)
Rect_(const Rect_& r),使用其他的Rect_对象初始化;
Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz),分别将位置坐标(_x, _y)和窗口大小(_width, _height)用Point_和Size_对象初始化;

Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2),分别将坐标位置(_x, _y)和窗口大小(_width, _height)用Point_和Point_对象初始化。

//如果创建一个Rect对象rect(100, 50, 50, 100),那么rect会有以下几个功能:
rect.area();     //返回rect的面积 5000
rect.size();     //返回rect的尺寸 [50 × 100]
rect.tl();       //返回rect的左上顶点的坐标 [100, 50]
rect.br();       //返回rect的右下顶点的坐标 [150, 150]
rect.width();    //返回rect的宽度 50
rect.height();   //返回rect的高度 100
rect.contains(Point(x, y));  //返回布尔变量,判断rect是否包含Point(x, y)点  //还可以求两个矩形的交集和并集
rect = rect1 & rect2;
rect = rect1 | rect2;  //还可以对矩形进行平移和缩放
rect = rect + Point(-100, 100); //平移,也就是左上顶点的x坐标-100,y坐标+100
rect = rect + Size(-100, 100);  //缩放,左上顶点不变,宽度-100,高度+100  //还可以对矩形进行对比,返回布尔变量
rect1 == rect2;
rect1 != rect2;  //OpenCV里貌似没有判断rect1是否在rect2里面的功能,所以自己写一个吧
bool isInside(Rect rect1, Rect rect2)
{  return (rect1 == (rect1&rect2));
}  //OpenCV貌似也没有获取矩形中心点的功能,还是自己写一个
Point getCenterPoint(Rect rect)
{  Point cpt;  cpt.x = rect.x + cvRound(rect.width/2.0);  cpt.y = rect.y + cvRound(rect.height/2.0);  return cpt;
}  //围绕矩形中心缩放
Rect rectCenterScale(Rect rect, Size size)
{  rect = rect + size;   Point pt;  pt.x = cvRound(size.width/2.0);  pt.y = cvRound(size.height/2.0);  return (rect-pt);
}  

二、rectangle函数

void rectangle(InputOutputArray img, Point pt1, Point pt2,const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);

简介:使用对角线的两点pt1,pt2画一个矩形轮廓或者填充矩形

@param img Image.
@param pt1 Vertex of the rectangle.
@param pt2 Vertex of the rectangle opposite to pt1 .
@param color Rectangle color or brightness (grayscale image).color 线条颜色 (RGB) 或亮度(灰度图像 )
@param thickness Thickness of lines that make up the rectangle. Negative values, like CV_FILLED ,
mean that the function has to draw a filled rectangle.thickness 组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形

@param lineType Type of the line.

//! type of line
enum LineTypes {FILLED  = -1,LINE_4  = 4, //!< 4-connected lineLINE_8  = 8, //!< 8-connected lineLINE_AA = 16 //!< antialiased line
};

@param shift Number of fractional bits in the point coordinates.坐标点的小数点的位数

void rectangle(CV_IN_OUT Mat& img, Rect rec,const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);

简介:使用矩阵类rec画一个矩形轮廓或者填充矩形

其他参数同上

三、实例示范

    rectangle(matImage,Point(20,200),Point(200,300),Scalar(255,0,0),1,1,0);  
   //Rect(int a,int b,int c,int d)a,b为矩形的左上角坐标,c,d为矩形的长和宽  
    rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0);  

参考链接:

https://blog.csdn.net/yhl_leo/article/details/50593825

https://blog.csdn.net/kh1445291129/article/details/51149849

https://blog.csdn.net/ubunfans/article/details/24421981

Opencv3中Rect和rectangle函数相关推荐

  1. 关于Opencv中Rect和Rectangle函数

    1)构造函数 Rect(x,y,width,height),x, y 为左上角坐标, width, height 则为长和宽. 2)方法 contain(Point) 可返回改点是否在矩形内. 3)方 ...

  2. matlab 圆角,rectangle函数MATLAB matlab中rectangle画圆角矩形

    MATLAB如何画长方形,怎么用rectangle函数 rectangle函数用于绘制矩形图形.例如>>rectangle('Position',[0 0 2 4],'Curvature' ...

  3. 怎样在matlab中写技术,rect矩形函数 matlab中怎样编写矩形函数

    矩形函数的定义: 矩形函数 rect(t); 如果绝对值 |t| > 0.5 rect(t) = 0; 如果绝对值 |t| = 0.5 rect(t) = 0.5; 如果绝对值 |t| < ...

  4. OpenCV的Rect()函数、Rectangle()函数、matchTemplate()参数详解

    OpenCV的函数 Rect()函数 cvRectangle与cv::rectangle matchTemplate() 归一化函数normalize() Rect()函数 基本概念: Rect(in ...

  5. matlab rectangle用法,rectangle函数MATLAB matlab中rectangle画圆角矩形

    MATLAB如何画长方形,怎么用rectangle函数 rectangle函数用于绘制矩形图形.例如>>rectangle('Position',[0 0 2 4],'Curvature' ...

  6. php中rectangle函数怎么用,Rectangle()函数

    函数功能:该函数画一个矩形,用当前的画笔画矩形轮廓,用当前画刷进行填充. Rectangle函数原型 C 1 BOOLRectangle(HDChdc,intnLeftRect,intnTopRect ...

  7. Matlab中rectangle函数使用

    目录 语法 说明 示例 绘制矩形 用曲线边绘制矩形 绘制圆形 指定矩形轮廓和填充颜色 创建并修改矩形 rectangle函数是创建带有尖角或圆角的矩形. 语法 rectangle('Position' ...

  8. OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法...

    函数中的代码是部分代码,详细代码在最后 1 cv2.boundingRect 作用:矩形边框(boundingRect),用于计算图像一系列点的外部矩形边界. cv2.boundingRect(arr ...

  9. openCV实战(一):rectangle函数使用

    rectangle函数使用 rect类 Rect对象的定义: rectangle函数 连续绘制多个矩形 rect类 Rect对象的定义: typedef Rect_<int> Rect; ...

最新文章

  1. Linux常见命令(二)
  2. AngularJS 1.x系列:Node.js安装及npm常用命令(1)
  3. Python 为啥不建议使用 thread模块?
  4. python 类的功能,字符串字节,嵌套等相关学习总结
  5. COMMIT WORK关键字在CRM content management应用里的使用场景
  6. 《产品设计与开发(原书第5版)》——3.8 步骤5:选出最佳机会方案
  7. (转)linux sort 命令详解
  8. 【转】斐讯K2刷华硕固件教程
  9. ERP : 服装行业解决方案
  10. 用python计算工资工资_python税后工资计算器
  11. 【CSP201609-3】炉石传说【模拟】
  12. 软件项目规模估算的3种方法---Loc估算法、FP估算法、PERT估算法
  13. LOJ3124 CTS2019 氪金手游 概率、容斥、树形DP
  14. (46)改变占空比与相移
  15. 地理距离测算(方法免费共享,经纬度、省份、地级市、港口间距离)
  16. Windows11下安装jdk
  17. 梆梆企业版加固技术之防篡改剖析
  18. Spring Cloud中Hystrix仪表盘与Turbine集群监控 1
  19. IBM磁盘阵列配置教程
  20. 解决Mac安装Adobe软件的时候,总是提示安装包(软件)“可能损坏了”的问题

热门文章

  1. Python海龟绘图螺旋线
  2. RadioButton 的使用
  3. “外卖小哥”悄然改变城市青年就业观
  4. 生活的苦 VS 学习的苦
  5. tomcat日志分析工具awstats配置
  6. py几局代码能写出九九乘法表_Python 九九乘法表
  7. 99.99%的人没看懂《蜗居》画龙点睛的一笔:马克(转载)
  8. ppt中查看所有元件,比如文本框
  9. 彩色图像、灰度图像、二值图像和索引图像区别?
  10. 图片相似度识别算法公式,图片相似度检测算法