OpenCV笔记(5) Rect类
看项目代码时,发现了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类的更多相关文章
- 【opencv基础】Rect类的神奇用法
前言 最近看github上源码发现对两个cv::Rect使用相与(&)操作,猛地感觉自己蒙啦,Rect类还有这种神奇用法?!翻看opencv官网Rect类,果然如此! opencv中Rect类 ...
- opencv笔记6:角点检测
time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...
- opencv笔记5:频域和空域的一点理解
time:2015年10月06日 星期二 12时14分51秒 # opencv笔记5:频域和空域的一点理解 空间域和频率域 傅立叶变换是f(t)乘以正弦项的展开,正弦项的频率由u(其实是miu)的值决 ...
- opencv笔记2:图像ROI
time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- OpenCV笔记大集锦(转载)
整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...
- TJI读书笔记10-复用类
TJI读书笔记10-复用类 组合语法 继承语法 代理 final关键字 final的数据 final的参数 final的方法 final的类 初始化和类的加载 乱七八糟不知道怎么归类的知识点 代码复用 ...
- opencv笔记4:模板运算和常见滤波操作
time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...
- opencv笔记3:trackbar简单使用
time:2015年 10月 03日 星期六 13:54:17 CST # opencv笔记3:trackbar简单使用 当需要测试某变量的一系列取值取值会产生什么结果时,适合用trackbar.看起 ...
- opencv笔记1:opencv的基本模块,以及环境搭建
opencv笔记1:opencv的基本模块,以及环境搭建 安装系统 使用fedora22-workstation-x86_64 安装opencv sudo dnf install opencv-dev ...
随机推荐
- 正则表达针对html(九)
前言 关于处理一些html的东西. 正文 假设有一段html文本: <?xml version="1.0" encoding="UTF-8"?> & ...
- react native 使用typescript
前言 TypeScript作为JavaScript的一个富类型扩展语言,深受代码风格严谨的前端开发者欢迎.但在react-native下,因为packager的配置困难,使用TypeScript一直是 ...
- c# 后端与前端时间戳的转换
C# DateTime与时间戳转换 C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的 ...
- 字节面试:如何解决MQ消息积压问题?
MQ(Message Queue)消息积压问题指的是在消息队列中累积了大量未处理的消息,导致消息队列中的消息积压严重,超出系统处理能力,影响系统性能和稳定性的现象. 1.消息积压是哪个环节的问题? M ...
- 力扣387(java)-字符串中的第一个唯一字符(简单)
题目: 给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 .如果不存在,则返回 -1 . 示例 1: 输入: s = "leetcode"输出: 0示例 2: 输 ...
- 安全同学讲Maven间接依赖场景的仲裁机制
简介: 去年的Log4j-core的安全问题,再次把供应链安全推向了高潮.在供应链安全的场景,蚂蚁集团在静态代码扫描平台-STC和资产威胁透视平台-哈勃这2款产品在联合合作下,优势互补,很好的解决了直 ...
- Flutter+FaaS一体化任务编排的思考与设计
作者:闲鱼技术-古风 Flutter+Serverless三端一体研发架构,客户端不仅仅是编写双端的代码,而是扩展了客户端的工作边界,形成完整的业务闭环.在新的研发模式落地与实践的过程中,一直在思考如 ...
- 飞天大数据产品价值解读— SaaS模式云数据仓库MaxCompute
飞天大数据产品价值解读 - SaaS模式云数据仓库 MaxCompute摘要:企业在数字化转型过程中面临数据技术平台建设和运营的诸多挑战,随着现代化数据仓库向多功能.服务化方向发展演进,技术侧的变革为 ...
- MaxCompute Spark 使用和常见问题
简介: 本文将就MaxCompute Spark开发环境搭建.常用配置.作业迁移注意事项以及常见问题进行深入介绍. 一. MaxCompute Spark 介绍 MaxCompute Spark是Ma ...
- 如何专业化监控一个Kubernetes集群?
简介: 本文会介绍 Kubernetes 可观测性系统的构建,以及基于阿里云云产品实现 Kubernetes 可观测系统构建的最佳实践. 作者:佳旭 阿里云容器服务技术专家 引言 Kubernetes ...