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 ...
随机推荐
- -source 1.5 中不支持 diamond 运算符(中文版idea)
-source 1.5 中不支持 diamond 运算符(中文版idea) 将idea中的各个部分的jdk设为8即可,中文版的如下 1.文件-设置 2.项目上右击-打开模块设置 模块中每一个都要确认是 ...
- 力扣627(MySQL)-变更性别(简单)
题目: Salary 表: 请你编写一个 SQL 查询来交换所有的 'f' 和 'm' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表. 注意 ...
- 力扣182(MySQL)-查找重复的电子邮箱(简单)
题目: 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: 解题思路: 方法一: 使用group by 按Email来分组,然后使用having选择count(id)> ...
- Spring Cloud Stream 体系及原理介绍
简介: Spring Cloud Stream在 Spring Cloud 体系内用于构建高度可扩展的基于事件驱动的微服务,其目的是为了简化消息在 Spring Cloud 应用程序中的开发. 作者 ...
- Hologres揭秘:高性能原生加速MaxCompute核心原理
简介: Hologres技术揭秘系列持续更新中,本期我们将带来Hologres高性能原生加速查询MaxCompute的技术原理解析. Hologres(中文名交互式分析)是阿里云自研的一站式实时数仓, ...
- dotnet OpenXML 聊聊文本段落对齐方式
本文来和大家聊聊在 OpenXML 里面,文本段落对齐方式.在 Word 和 PPT 的文本段落对齐规则是相同的,对齐的规则比较多,本文将一一告诉大家 文本的段落对齐,需要设置给段落属性上,在 Ope ...
- Vue3 和 Vue2 的异同及开发中具体区别
目录 总体来说 性能提升 树摇(Tree shaking) 碎片化节点(Fragment) 传送门 (Teleport) Suspense 更好的TypeScript支持 Composition AP ...
- 前端之JavaScript基础学习
一.JS代码引入以及基本代码规范 # 1.js代码书写格式 <script> ....js的代码 </script> #2.script标签写在页面那个位置 1)页面的head ...
- 数据分析之重要模块pandas
1.简介 基于Numpy构建 pandas的出现,让Python语言成为使用最广泛而且强大的数据分析环境之一 pandas的主要功能 - 具备诸多功能的两大数据结构 Series.DataFrame( ...
- Python的国内安装源(也称为镜像源)
Python的国内安装源(也称为镜像源)数量会随着时间而增加或减少,因为新的镜像源可能会建立,而一些旧的镜像源可能会停止服务或不再更新.以下是一些常用的Python国内安装源(也称为PyPI镜像源): ...