看项目代码时,发现了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. RestfulApi 学习笔记——分页和排序(六)

    前言 分页和排序时一些非常常规的操作,同样也有一些我们注意的点. 正文 分页 先来谈及分页. 看下前端传递的参数. public class EmployeeDtoParameters { priva ...

  2. redis 一百二十篇(历史发展)之第二篇

    正文 简介: Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化, ...

  3. 当大火的文图生成模型遇见知识图谱,AI画像趋近于真实世界

    简介: 模型免费开放!零基础也能一键进行AI艺术创作.本⽂简要介绍ARTIST的技术解读,以及如何在EasyNLP框架中使⽤ARTIST模型. 导读 用户生成内容(User Generated Con ...

  4. Dataphin产品核心功能大图(六)发布中心:生产和开发隔离模式下的保护伞

    ​简介:Dataphin,用中台方法论打造企业级好数据.Dataphin是阿里巴巴集团OneData数据治理方法论内部实践的云化输出,一站式提供数据采.建.管.用全生命周期的大数据能力,以助力企业显著 ...

  5. OceanBase首次阐述战略:继续坚持自研开放之路 开源300万行核心代码

    简介: 在数据库OceanBase3.0峰会上,蚂蚁集团自主研发的分布式数据库OceanBase首次从技术.商业和生态三个维度对未来发展战略进行了系统性阐述.同时,OceanBase宣布正式开源,并成 ...

  6. Kubernetes 已经成为云原生时代的安卓,这就够了吗?

    ​简介:本文将介绍如何在 Kubernetes 上构建新的应用管理平台,提供一层抽象以封装底层逻辑,只呈现用户关心的接口,使用户可以只关注自己的业务逻辑,管理应用更快更安全. 作者:司徒放 导语:云原 ...

  7. 新闻网页Python爬虫(jieba分词+关键词搜索排序)

    前言 最近做了一个python3作业题目,涉及到: 网页爬虫 网页中文文字提取 建立文字索引 关键词搜索 涉及到的库有: 爬虫库:requests 解析库:xpath 正则:re 分词库:jieba ...

  8. 一、Doris演进史

    Apache Doris -- 为分析而生 Doris发展历程: Doris发展比较重要的关键节点与事件 #2008 - Doris1 :「筑巢引凤」的重要基石 早年,百度最主要的收入来源是广告.广告 ...

  9. CMS垃圾收集器小实验之CMSInitiatingOccupancyFraction参数

    CMS垃圾收集器小实验之CMSInitiatingOccupancyFraction参数 背景 测试CMSInitiatingOccupancyFraction参数,测试结果和我的预期不符,所以花了一 ...

  10. vue安装tinyMCE

    目录 [参考视频] [参考文章] 官网: https://www.tiny.cloud/auth/signup/ 资源下载 tinymce 官方为 vue 项目提供了一个组件tinymce-vue n ...