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, PRECT, NPRECT, LPRECT;它们之间有什么区别?

http://hi.baidu.com/gis_passion/item/784719d792a6eec51a72b438 typedef struct tagRECT { LO...

用Fortran来实现DLL的编程和应用(2)

Fortran程序中,很多用到了COMMON来进行数据的交换,如何在DLL中处理也必须面对,在这里根据Fortran手册的程序,实现在Fortran程序间的调用。但是在Fortran与C之间,好像不存...
  • PolyUp
  • PolyUp
  • 2006-08-18 14:00
  • 2372

【OpenCV入门教程之四】 ROI区域图像叠加&初级图像混合 全剖析

在这篇文章里,我们一起学习了在OpenCV中如何定义感兴趣区域ROI,如何使用addWeighted函数进行图像混合操作,以及将ROI和addWeighted函数结合起来使用,对指定区域进行图像混合操...

RECT类

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

opencv识别正方形(矩形)代码

#include "cv.h" #include "highgui.h" #include #include #include #include int thresh = 50; IplIma...

Android调用OpenCV,不用OpenCV manager

Android调用OpenCV,不用OpenCV manager 代码如下: package com.example.grayprocess;    import org.opencv.a...

java版利用opencv根据RotateRect裁剪图像区域算法

public Mat guiyihuaMatByRoi(Mat cpsrcMat,RotatedRect rotatedRect){ /*Point[] rectPoints=new ...

Java调用OpenCV进行Hough变换直线检测

private BitmapHoughTransFormLine(Bitmap bmp)    {     Mat rgbMat = new Mat();  //存储原图像的矩阵     ...

【OpenCV入门教程之五】 分离颜色通道&多通道图像混合

上篇文章中我们讲到了使用addWeighted函数进行图像混合操作,以及将ROI和addWeighted函数结合起来使用,对指定区域进行图像混合操作。 而为了更好的观察一些图像材料的特征,有时需要对R...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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