看项目代码时,发现了Rect的神奇用法,rect = rect + point。于是了解了一下Rect类。

1. 构造函数

public Rect(Point location, Size size);
public Rect(int x, int y, int width, int height);

示例:在黑色掩膜上画两个白色矩形

Mat mask = new Mat(1000, 1000, MatType.CV_8UC1, new Scalar(0));
Rect rect1 = new Rect(100, 200, 200, 200);
Rect rect2 = new Rect(new Point(300,600),new Size(100,100));
Cv2.Rectangle(mask, rect1, new Scalar(255));
Cv2.Rectangle(mask, rect2, new Scalar(255));
Cv2.ImShow("mask", mask);
Cv2.WaitKey(0);

2. 属性

Bottom,Right,Top,Left有点特别

示例:

Console.WriteLine("Bottom:" + rect1.Bottom.ToString());
Console.WriteLine("Right:" + rect1.Right.ToString());
Console.WriteLine("BottomRight:" + rect1.BottomRight.ToString());
Console.WriteLine("Top:" + rect1.Top.ToString());
Console.WriteLine("Left:" + rect1.Left.ToString());
Console.WriteLine("TopLeft:" + rect1.TopLeft.ToString());

输出为:

Bottom:399
Right:299
BottomRight:(x:299 y:399)
Top:200
Left:100
TopLeft:(x:100 y:200)

3. Rect.Inflate,Intersect,Union

public static Rect Inflate(Rect rect, int x, int y);//沿轴放大是沿两个方向(正方向和负方向)进行的。
public void Inflate(int width, int height);//也会放大2*width
public void Inflate(Size size);

示例:

Mat mask = new Mat(1000, 1000, MatType.CV_8UC1, new Scalar(0));
Rect rect1 = new Rect(100, 200, 200, 200);
Rect rect2 = Rect.Inflate(rect1, -12, 12);
//rect2.Inflate(100, 100);
Console.WriteLine("before inflate,size:" + rect1.Size);
Console.WriteLine("after inflate,size:" + rect2.Size);
Cv2.Rectangle(mask, rect1, new Scalar(255));
Cv2.Rectangle(mask, rect2, new Scalar(255));
Cv2.ImShow("mask", mask);

输出:

before inflate,size:(width:200 height:200)
after inflate,size:(width:176 height:224)

 

1.Inflate                                                        2. Intersect                                                3.Union

4. 重载运算符

rect = rect ± point         (shifting a rectangle by a certain offset)
rect = rect ± size         (expanding or shrinking a rectangle by a certain amount)
rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
rect = rect1 & rect2         (rectangle intersection)
rect = rect1 | rect2         (minimum area rectangle containing rect1 and rect2 )
rect &= rect1, rect |= rect1     (and the corresponding augmenting operations)
rect == rect1, rect != rect1     (rectangle comparison)

超级重要的用法!!!防止rect区域越界

rect &= new Rect(0, 0, img.Cols, img.Rows);

示例:

Mat mask = new Mat(1000, 1000, MatType.CV_8UC1, new Scalar(255));
Rect rect1 = new Rect(100, 100, 100, 100);
Point p = new Point(300, 300);
Rect rect2 = rect1 + p;
Console.WriteLine(rect2.Location);
Rect rect3 = rect2 + new Size(200, 200);
Console.WriteLine(rect2.Location);

以下为源码:

using System;

namespace OpenCvSharp.CPlusPlus
{
public struct Rect : IEquatable<Rect>
{
public const int SizeOf = 16;
public static readonly Rect Empty;
public int X;
public int Y;
public int Width;
public int Height; public Rect(Point location, Size size);
public Rect(int x, int y, int width, int height); public Size Size { get; set; }
public Point Location { get; set; }
public int Right { get; }
public int Left { get; set; }
public int Bottom { get; }
public int Top { get; set; }
public Point TopLeft { get; }
public Point BottomRight { get; } public static Rect FromLTRB(int left, int top, int right, int bottom);
public static Rect Inflate(Rect rect, int x, int y);
public static Rect Intersect(Rect a, Rect b);
public static Rect Union(Rect a, Rect b);
public bool Contains(int x, int y);
public bool Contains(Point pt);
public bool Contains(Rect rect);
public bool Equals(Rect obj);
public override bool Equals(object obj);
public override int GetHashCode();
public void Inflate(int width, int height);
public void Inflate(Size size);
public Rect Intersect(Rect rect);
public bool IntersectsWith(Rect rect);
public override string ToString();
public Rect Union(Rect rect); public static Rect operator +(Rect rect, Point pt);
public static Rect operator +(Rect rect, Size size);
public static Rect operator -(Rect rect, Point pt);
public static Rect operator -(Rect rect, Size size);
public static Rect operator &(Rect a, Rect b);
public static Rect operator |(Rect a, Rect b);
public static bool operator ==(Rect lhs, Rect rhs);
public static bool operator !=(Rect lhs, Rect rhs); public static implicit operator Rect(CvRect rect);
public static implicit operator CvRect(Rect self);
}
}

OpenCV笔记(5) Rect类的更多相关文章

  1. 【opencv基础】Rect类的神奇用法

    前言 最近看github上源码发现对两个cv::Rect使用相与(&)操作,猛地感觉自己蒙啦,Rect类还有这种神奇用法?!翻看opencv官网Rect类,果然如此! opencv中Rect类 ...

  2. opencv笔记6:角点检测

    time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...

  3. opencv笔记5:频域和空域的一点理解

    time:2015年10月06日 星期二 12时14分51秒 # opencv笔记5:频域和空域的一点理解 空间域和频率域 傅立叶变换是f(t)乘以正弦项的展开,正弦项的频率由u(其实是miu)的值决 ...

  4. opencv笔记2:图像ROI

    time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...

  5. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  6. OpenCV笔记大集锦(转载)

    整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...

  7. TJI读书笔记10-复用类

    TJI读书笔记10-复用类 组合语法 继承语法 代理 final关键字 final的数据 final的参数 final的方法 final的类 初始化和类的加载 乱七八糟不知道怎么归类的知识点 代码复用 ...

  8. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  9. opencv笔记3:trackbar简单使用

    time:2015年 10月 03日 星期六 13:54:17 CST # opencv笔记3:trackbar简单使用 当需要测试某变量的一系列取值取值会产生什么结果时,适合用trackbar.看起 ...

  10. opencv笔记1:opencv的基本模块,以及环境搭建

    opencv笔记1:opencv的基本模块,以及环境搭建 安装系统 使用fedora22-workstation-x86_64 安装opencv sudo dnf install opencv-dev ...

随机推荐

  1. 重新整理数据结构与算法(c#)——算法套佛洛伊德算法[三十二]

    前言 佛洛伊德算法和迪杰斯特拉算法非常像,但是它求的是任何一个点到其他点之间的距离. 假设有一张图: 转换为矩阵为: 他们的前驱为: 可能上面表述前驱不清楚,举个例子. 看下图: 这第二种图表示,从A ...

  2. 存储过程编写·记(“xxx“在需要下列之一:if)

    存储过程编写·记("xxx"在需要下列之一:if) 使用的数据库为Oracle数据库,数据库客户端为DBeaver 简单来说,就是使用SQL语句进行一些函数编写,进而进行一些过滤啊 ...

  3. 转载 | 基于阿里云Serverless函数计算开发的疫情数据统计推送机器人

    简介: 本文选自函数计算征集令优秀征文! 一.Serverless函数计算 什么是Serverless? 在<Serverless Architectures>中对 Serverless ...

  4. Dubbo-go 服务代理模型

    ​简介:HSF 是阿里集团 RPC/服务治理 领域的标杆,Go 语言又因为其高并发,云原生的特性,拥有广阔的发展前景和实践场景,服务代理模型只是一种落地场景,除此之外,还有更多的应用场景值得我们在研发 ...

  5. 云原生消息、事件、流超融合平台——RocketMQ 5.0 初探

    简介: 今天分享的主题是云原生消息事件流超融合平台 RocketMQ 5.0 初探,内容主要分为三个部分: 首先,带大家回顾业务消息领域首选 RocketMQ 4 发展历史以及 4.x 版本的演进与发 ...

  6. netcore3.1 程序在cento8下运行selenium

    我需要在linux下运行selenium抓取数据,本人不熟悉Python,所以只能用netcore.在带linux界面上运行爬取程序,驱动chromedriver比较简单.界面化安装好chrome,下 ...

  7. 实验8 #第8章 Verilog有限状态机设计-3 #Verilog #Quartus #modelsim

    3. 状态机A/D采样控制电路 3.1 目标:用状态机控制ADC0809实现数据采集. 3.2 ADC0809简介 (1)ADC0809是8位A/D转换器,片内有8路模拟开关,可控制8个 模拟量中 的 ...

  8. 【Oracle故障处理】ORA-00845: MEMORY_TARGET not supported on this system

    场景:由于需要用RMAN恢复数据库,提取以前的数据表中的数据.虚拟机为节省资源调小了内存,启动数据库报了 如下错误: ORA-00845: MEMORY_TARGET not supported on ...

  9. docker-compose部署Elasticsearch7.14.1+kabana7.14.1+elasticsearch-head并设置账号密码

    学习资料 https://www.bilibili.com/video/BV1eY411w7Lx/ https://www.bilibili.com/video/BV1SQ4y1m7Ds?p=13 仓 ...

  10. docker 搭建LNMP环境

    php7 仓库地址 https://gitee.com/haima1004/docker-lnmp