看项目代码时,发现了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. 重新点亮linux 命令树————进程的控制[二十二]

    前言 简单整理一下进程的控制 正文 进程优先级 nice 从-20-19,值越小,优先级越高 renice 从新设置优先级 进程的作业控制: job 那么先来创建一个进程. 那么使用top -p 26 ...

  2. RestfulApi 学习笔记——分页和排序(六)

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

  3. Pytorch-tensor的激活函数

    1.激活函数 激活函数的作用是能够给神经网络加入一些非线性因素,使得神经网络可以更好地解决较为复杂的问题.因为很多问题都不是线性的,你只有给它加入一些非线性因素,就能够让问题更好的解决. 函数1:RE ...

  4. git fork 项目的更新

    fork:github网站的操作,将开源项目复制一份到自己的仓库中 那fork的项目在原仓库更新后,如何同步呢? 1.查看远程仓库 $ git remote -v origin https://cod ...

  5. ASP.NET Core Web API下基于Keycloak的多租户用户授权的实现

    在上文<Keycloak中授权的实现>中,以一个实际案例介绍了Keycloak中用户授权的设置方法.现在回顾一下这个案例: 服务供应商(Service Provider)发布/Weathe ...

  6. HarmonyOS NEXT应用开发之Tab组件实现增删Tab标签

    介绍 本示例介绍使用了Tab组件实现自定义增删Tab页签的功能.该场景多用于浏览器等场景. 效果图预览 使用说明: 点击新增按钮,新增Tab页面. 点击删除按钮,删除Tab页面. 实现思路 设置Tab ...

  7. DataX在数据迁移中的应用

    简介: DataX在数据迁移中的应用 1. DataX定义 首先简单介绍下datax是什么.DataX是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlSe ...

  8. 慢sql治理经典案例分享

    ​简介:菜鸟供应链金融慢sql治理已经有一段时间,自己负责的应用持续很长时间没有慢sql告警,现阶段在推进组内其他成员治理应用慢sql.这里把治理过程中的一些实践拿出来分享下. ​ 作者 | 如期 来 ...

  9. TDA-04D8变送器数据上报阿里云

    ​简介:本文将以TDA-04D8变送器作为采集对象,使用海创微联采集控制系统对TDA-04D8变送器进行采集,然后将设备上的毛重.净重.皮重数据采集上传到阿里云物联网平台,阿里云物联网平台将数据实时可 ...

  10. io_uring vs epoll ,谁在网络编程领域更胜一筹?

    ​简介:从定量分析的角度,通过量化 io_uring 和 epoll 两种编程框架下的相关操作的耗时,来分析二者的性能差异. ​ 本文作者:王小光,「高性能存储技术SIG」核心成员. 背景 io_ur ...