OpenCV的Rect矩形类用法

转载 2016年06月01日 13:14:37

转载来自:http://www.voidcn.com/blog/kh1445291129/article/p-5775101.html

大笑

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//如果创建一个Rect对象rect(100, 50, 50, 100),那么rect会有以下几个功能:</span>
rect.area();     <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回rect的面积 5000</span>
rect.size();     <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回rect的尺寸 [50 × 100]</span>
rect.tl();       <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回rect的左上顶点的坐标 [100, 50]</span>
rect.br();       <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回rect的右下顶点的坐标 [150, 150]</span>
rect.width();    <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回rect的宽度 50</span>
rect.height();   <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回rect的高度 100</span>
rect.contains(Point(x, y));  <span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//返回布尔变量,判断rect是否包含Point(x, y)点</span>

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//还可以求两个矩形的交集和并集</span>
rect = rect1 & rect2;
rect = rect1 | rect2;

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//还可以对矩形进行平移和缩放  </span>
rect = rect + Point(-<span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">100</span>, <span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">100</span>);	<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//平移,也就是左上顶点的x坐标-100,y坐标+100</span>
rect = rect + Size(-<span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">100</span>, <span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">100</span>);	<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//缩放,左上顶点不变,宽度-100,高度+100</span>

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//还可以对矩形进行对比,返回布尔变量</span>
rect1 == rect2;
rect1 != rect2;

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//OpenCV里貌似没有判断rect1是否在rect2里面的功能,所以自己写一个吧</span>
<span class="hljs-function" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; background: transparent;"><span class="hljs-keyword" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(149, 65, 33); background: transparent;">bool</span> <span class="hljs-title" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(25, 70, 157); background: transparent;">isInside</span><span class="hljs-params" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 255); background: transparent;">(Rect rect1, Rect rect2)</span>
</span>{
	<span class="hljs-keyword" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(149, 65, 33); background: transparent;">return</span> (rect1 == (rect1&rect2));
}

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//OpenCV貌似也没有获取矩形中心点的功能,还是自己写一个</span>
<span class="hljs-function" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; background: transparent;">Point <span class="hljs-title" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(25, 70, 157); background: transparent;">getCenterPoint</span><span class="hljs-params" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 255); background: transparent;">(Rect rect)</span>
</span>{
	Point cpt;
	cpt.x = rect.x + cvRound(rect.width/<span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">2.0</span>);
	cpt.y = rect.y + cvRound(rect.height/<span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">2.0</span>);
	<span class="hljs-keyword" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(149, 65, 33); background: transparent;">return</span> cpt;
}

<span class="hljs-comment" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: italic; font-family: inherit; vertical-align: baseline; color: rgb(64, 128, 128); background: transparent;">//围绕矩形中心缩放</span>
<span class="hljs-function" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; background: transparent;">Rect <span class="hljs-title" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(25, 70, 157); background: transparent;">rectCenterScale</span><span class="hljs-params" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 255); background: transparent;">(Rect rect, Size size)</span>
</span>{
	rect = rect + size;	
	Point pt;
	pt.x = cvRound(size.width/<span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">2.0</span>);
	pt.y = cvRound(size.height/<span class="hljs-number" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(64, 160, 112); background: transparent;">2.0</span>);
	<span class="hljs-keyword" style="border: 0px; margin: 0px; padding: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(149, 65, 33); background: transparent;">return</span> (rect-pt);
}


RECT类

RECT类   rect对象用来存储一个矩形框的左上角坐标、宽度和高度。 描述矩形的宽度、高度和原点。 命名空间:  System.Windows 程序集:  Sy...
  • holandstone
  • holandstone
  • 2012年05月10日 10:07
  • 13538

openCV 画矩形框或填充矩形

#include "stdafx.h" #include #include #include "cv.h" #include "highgui.h" using namespace cv...
  • yanglong890124
  • yanglong890124
  • 2014年06月06日 23:53
  • 21359

Python 内建函数

Chapter9 Python几个内建函数 /*! * * Twitter Bootstrap * */ /*! * Bootstrap v3.3.7 (http://getboots...
  • linian71
  • linian71
  • 2018年01月13日 16:38
  • 0

OpenCV的Rect矩形类用法

//如果创建一个Rect对象rect(100, 50, 50, 100),那么rect会有以下几个功能: rect.area() //返回rect的面积 5000 rect.size() //返回...
  • kh1445291129
  • kh1445291129
  • 2016年04月14日 11:44
  • 23347

opencv在图片指定区域内填充矩形,并在图片中显示矩形的轮廓

用opencv在图片的指定区域画轮廓
  • fuyou25
  • fuyou25
  • 2016年12月08日 21:18
  • 1366

c/c++成长之捷径

c/c++成长之捷径           c++成长之路不再迷茫!           不知不觉中进入计算机行业已有多个年头了,回首往事依然历历在目。今天我把我这段时间收集的书...
  • zhaodan19861107
  • zhaodan19861107
  • 2014年05月20日 11:20
  • 5067

我的OpenCV学习笔记(24):详细讨论OpenCV中的数据结构

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

Opencv图像识别从零到精通(24)------漫水填充,种子填充,区域生长、孔洞填充

可以说从这篇文章开始,就结束了图像识别的入门基础,来到了第二阶段的学习。在平时处理二值图像的时候,除了要进行形态学的一些操作,还有有上一节讲到的轮廓连通区域的面积周长标记等,还有一个最常见的就是孔洞的...
  • qq_20823641
  • qq_20823641
  • 2016年08月11日 21:36
  • 4952

opencv3寻找最小包围矩形-minAreaRect函数

#include #include #include using namespace cv; using namespace std; int main() { Mat srcImage(Siz...
  • qq_23880193
  • qq_23880193
  • 2015年10月20日 00:14
  • 9059
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OpenCV的Rect矩形类用法
举报原因:
原因补充:

(最多只允许输入30个字)