zl程序教程

您现在的位置是:首页 >  .Net

当前栏目

OpenCV笔记(5) Rect类

2023-02-18 16:27:04 时间

看项目代码时,发现了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);
    }
}