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); } }
相关文章
- 忙活了一年的开源社区,终于赶上了春节前的末班车!
- ChatGPT 会开源吗?
- 7 款殿堂级的开源 CMS(内容管理系统)
- 请收下这 10 个安全相关的开源项目
- MySQL 5.7 升级到 8.0
- 越折腾越好用的 3 款开源 APP
- 10 款更先进的开源命令行工具
- 对开源框架跃跃欲试,却在写的时候犯了难?
- 一大波开源小抄来袭
- 物联网?快来看 Arduino 上云啦
- 想做钢铁侠?听说很多大佬都是用它入门的
- 写给小白的开源编译器
- 支持中文!秒建 wiki 知识库的开源项目,构建私人知识网络
- 一款开源的文件搜索神器,终于不用记 find 命令了
- 用一个文件,实现迷你 Web 框架
- 一个文件的开源项目,开启你的开源之旅
- 3.6 万颗星!开源 Web 服务器后起之秀,自带免费 HTTPS 开箱即用
- 狠人!标星 3.4 万的项目说删就删,几行代码搞崩数万个开源项目
- 那些年的开源项目,你跑起来了吗?
- 重玩 40 年前的经典游戏小蜜蜂,这次通关了源码