OpenCvSharp study notes 9--Basic painting (Line, Ellipse, Rectangle, Circle, FillPoly)

Table of contents

Line draws a straight line

Ellipse draws or fills ellipse outlines and ellipses

Rectangle draws or fills a rectangle

CircleDraw or fill a circle

FillPoly fill polygon


Line draws a straight line

Function description: Draw a straight line between two given points. Anything outside the image is cropped. For non-antialiasing use 8-channel or 4-channel algorithm. Thick lines are drawn at both ends with rounded corners. Antialiased lines are drawn using Gaussian filtering.

//函数原型1
void Line(InputOutputArray img,
    int pt1X,
    int pt1Y,
    int pt2X,
    int pt2Y,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)

//函数原型2
void Line(InputOutputArray img,
    Point pt1,
    Point pt2,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)
parameter illustrate
InputOutputArray img The image of the straight line to be drawn

Point pt1

int pt1X,int pt1Y

Starting point coordinates

Point pt2

int pt2X,int pt2Y 

End point coordinates
Scalar color straight line color
int thickness The line width of the straight line (>0 && <=32767), cannot be -1
LineTypes lineType Line type
int shift Coordinate displacement to the right (multiple of reduction)

Drawing results when LineType is different values

AntiAlias: Anti-aliasing (with Gaussian blur)

Drawing effects of Link4 and Link8

 Actual drawing effect, red (AntiAlias), blue (Link4), green (Link8)

/// <summary>
/// 测试Line的LinkType参数
/// </summary>
private void Test_Line_LinkType()
{
    //Line LinkType参数测试
    using (Mat canvas = Mat.Zeros(w / 4, w / 4, MatType.CV_8UC3))
    {
        var winName = "Test LineTypes";
        Cv2.NamedWindow(winName, WindowFlags.Normal);
        Cv2.ResizeWindow(winName, new Size(w * 4, w * 4));

        //不同LineType画线
        In_Test_Line_LinkType(canvas, winName, LineTypes.AntiAlias, Scalar.Red);
        In_Test_Line_LinkType(canvas, winName, LineTypes.Link4, Scalar.Blue);
        In_Test_Line_LinkType(canvas, winName, LineTypes.Link8, Scalar.Green);

        Cv2.DestroyAllWindows();
    }
}

//偏移坐标用
private int index = 0;
/// <summary>
/// 绘制直线和LinkType
/// </summary>
/// <param name="canvas"></param>
/// <param name="winName"></param>
/// <param name="lineType"></param>
/// <param name="color"></param>
private void In_Test_Line_LinkType(Mat canvas, string winName, LineTypes lineType, Scalar color)
{
    int x1 = 11, y1 = 13, x2 = 78, y2 = 96;
    Cv2.Line(canvas, x1, y1, x2, y2, color, 1, lineType);
    Cv2.PutText(canvas, lineType.ToString(), new Point(15 + index * 10, 15 + index * 10), HersheyFonts.HersheySimplex, 0.4, color);
    Cv2.ImShow(winName, canvas);
    Cv2.WaitKey();
    index++;
}

The coordinates are the same, and the drawing results are when the shift parameters are 2, 1, and 0 respectively.

/// <summary>
/// 测试Line的shift参数
/// </summary>
private void Test_Line_Shift()
{
    //Line Shift参数测试
    using (Mat canvas = Mat.Zeros(w, w, MatType.CV_8UC3))
    {
        int x1 = 100, y1 = 150, x2 = 300, y2 = 350;

        LineTypes lineTypes = LineTypes.AntiAlias;
        Cv2.Line(canvas, x1, y1, x2, y2, Scalar.Red, 1, lineTypes, 0);

        //shift参数测试
        int shift = 1;
        Cv2.Line(canvas, x1, y1, x2, y2, Scalar.Red, 1, lineTypes, shift);
        //基本等价于下面
        Cv2.Line(canvas, x1 >> shift, y1 >> shift, x2 >> shift, y2 >> shift, Scalar.Green, 1, lineTypes, 0);

        shift = 2;
        Cv2.Line(canvas, x1, y1, x2, y2, Scalar.Red, 1, lineTypes, shift);
        //基本等价于下面
        Cv2.Line(canvas, x1 >> shift, y1 >> shift, x2 >> shift, y2 >> shift, Scalar.Green, 1, lineTypes, 0);
        var winName = "Test shift";
        Cv2.NamedWindow(winName, WindowFlags.Normal);                
        Cv2.ResizeWindow(winName, new Size(w * 2, w * 2));
        Utils.ShowWaitDestroy(canvas, winName, WindowFlags.AutoSize);
    }
}

Ellipse draws or fills ellipse outlines and ellipses

Function description: It can draw elliptical outline, filled circle, elliptical arc or filled elliptical sector.

//函数原型1
void Ellipse(InputOutputArray img,
    Point center,
    Size axes,
    double angle,
    double startAngle,
    double endAngle,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)

//函数原型2
void Ellipse(InputOutputArray img,
    RotatedRect box,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8)
parameter illustrate
InputOutputArray img Image to be drawn
Point center ellipse center point
Size axes Half of the major and minor axes of the ellipse
RotaedRect box The size of the bounding rectangle of the ellipse
double angle Ellipse deflection angle, >0 clockwise, <0 counterclockwise
double startAngle Starting angle of the elliptical profile (calculated from the deflection angle)
double endAngle The ending angle of the elliptical outline (calculated from the deflection angle) (if you draw an ellipse, startAngle=0, endAngle=360)
Scalar color Ellipse line color
int thickness Ellipse line width. Negative numbers indicate padding
LineTypes lineType elliptical line style
int shift Rightward displacement of center and axes values ​​(reduction factor)

The meaning of each parameter when drawing an elliptical arc (picture from official website)

 

/// <summary>
/// 测试Ellipse
/// </summary>
private void Test_Ellipse()
{
    using (Mat canvas = Mat.Zeros(w, w, MatType.CV_8UC3))
    {
        var center = new Point(w / 2, w / 2);
        var axes = new Size(150, 100);
        var angle = 45;

        //画椭圆的辅助线
        DrawEllipseAuxiliaryLine(canvas, center, axes, angle);

        //画椭圆弧
        Cv2.Ellipse(canvas, center, axes, angle, 90, 360, Scalar.Red);

        //画椭圆,角度不同
        Cv2.Ellipse(canvas, new RotatedRect(new Point2f(center.X, center.Y), new Size2f(axes.Width * 2, axes.Height * 2), -angle), Scalar.Blue

        var winName = "Test Ellipse";
        Cv2.NamedWindow(winName, WindowFlags.Normal);                
        Cv2.ResizeWindow(winName, new Size(w * 2, w * 2));
        Utils.ShowWaitDestroy(canvas, winName, WindowFlags.AutoSize);
    }
}

/// <summary>
/// 绘制椭圆的外接矩形
/// </summary>
/// <param name="canvas"></param>
/// <param name="center">中心点</param>
/// <param name="axes">椭圆长短轴大小的一半</param>
/// <param name="angle">椭圆偏转角度</param>
private void DrawEllipseAuxiliaryLine(Mat canvas, Point center,Size axes,double angle)
{
    Scalar color = Scalar.Green;
    //X轴
    Cv2.Line(canvas, new Point(center.X - axes.Width, center.Y), new Point(center.X + axes.Width, center.Y), color);
    //Y轴
    Cv2.Line(canvas, new Point(center.X, center.Y - axes.Height), new Point(center.X, center.Y + axes.Height), color);

    //AXES BOX
    Cv2.Rectangle(canvas,
                  new Point(center.X - axes.Width, center.Y - axes.Height),
                  new Point(center.X + axes.Width, center.Y + axes.Height),
                  color);
    var rotat = Cv2.GetRotationMatrix2D(center, -angle, 1);//注意,这里的角度与椭圆角度相反

    //将画布旋转            
    Cv2.WarpAffine(canvas, canvas, rotat, canvas.Size());
}

Rectangle draws or fills a rectangle

Function description: Draw a hollow or solid rectangle based on the given diagonal point coordinates.

//函数原型1
void Rectangle(InputOutputArray img,
    Point pt1,
    Point pt2,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)

//函数原型2
void Rectangle(InputOutputArray img,
    Rect rect,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)

//函数原型3
void Rectangle(Mat img,
    Point pt1,
    Point pt2,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)

//函数原型4
void Rectangle(Mat img,
    Rect rect,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)
parameter illustrate

InputOutputArray img

Mat img

image to draw
Point pt1 a vertex of a rectangle
Point pt2 Diagonal point of pt1
Rect rect rectangle
Scalar color Rectangle color
int thickness The line width of the rectangle. If it is less than 0, it means filling.
LineTypes lineType linear
int shift The rightward displacement of the coordinate value (reduction factor)
private void Test_Rectangle()
{
    using (Mat canvas = Mat.Zeros(w, w, MatType.CV_8UC3))
    {

        int bias = 50;
        //左上,右下,两个点,shift=0
        Point p1 = new Point(bias, bias);
        Point p2 = new Point(w - bias, w - bias);

        DrawRectangle(canvas, p1, p2, 0);

        //左下,右上,两个点,shift=1
        p1 = new Point(bias, w - bias);
        p2 = new Point(w - bias, bias);
        DrawRectangle(canvas, p1, p2, 1);

        Utils.ShowWaitDestroy(canvas, "Test Rectangle", WindowFlags.AutoSize);
    }
}
/// <summary>
/// 绘制矩形及对角点
/// </summary>
/// <param name="canvas"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="shift"></param>
private void DrawRectangle(Mat canvas,Point p1,Point p2,int shift)
{
    Scalar colorRect = Scalar.Red;            
    Cv2.Rectangle(canvas, p1, p2, colorRect, shift: shift);
    Scalar colorPoint = Scalar.Green;
    Cv2.Circle(canvas, p1.X >> shift, p1.Y >> shift, 5, colorPoint, -1);
    Cv2.Circle(canvas, p2.X >> shift, p2.Y >> shift, 5, colorPoint, -1);
}

CircleDraw or fill a circle

Function description: Draw a hollow or solid circle based on the center and radius.

//函数原型1
void Circle(InputOutputArray img,
    int centerX,
    int centerY,
    int radius,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)

//函数原型2
void Circle(InputOutputArray img,
    Point center,
    int radius,
    Scalar color,
    int thickness = 1,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0)
parameter illustrate
InputOutputArray img image to draw

int centerX

int centerY

Point center

Circle center coordinates
int radius radius of circle
Scalar color circle color
int thickness The line width of the circle. Negative numbers indicate padding
LineTypes lineType round line style
int shift Rightward displacement of center and radius (reduction factor)
/// <summary>
/// 测试Circle
/// </summary>
private void Test_Circle()
{
    using (Mat canvas = Mat.Zeros(w, w, MatType.CV_8UC3))
    {
        var center = new Point(w / 2, w / 2);
        //画圆
        Cv2.Circle(canvas, center, w / 4, Scalar.Red);

        //填充,shift=1
        Cv2.Circle(canvas, center, w / 4, Scalar.Green, -1, shift: 1);

        Utils.ShowWaitDestroy(canvas, "Test Circle", WindowFlags.AutoSize);
    }
}

FillPoly fill polygon

Function description: Fill one or more polygons according to the given boundaries. Can fill complex areas, such as holes and self-intersections

of.

//函数原型1
void FillPoly(InputOutputArray img,
    InputArray pts,
    Scalar color,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0,
    Point? offset = null)

//函数原型2
void FillPoly(Mat img,
    IEnumerable<IEnumerable<Point>> pts,
    Scalar color,
    LineTypes lineType = LineTypes.Link8,
    int shift = 0,
    Point? offset = null)
parameter illustrate

InputOutputArray img

Mat img

image to draw

InputArray pts

IEnumerable<IEnumerable<Point>> pts

The set of polygon outline coordinates to be filled

Scalar color Color to be filled
LineTypes lineType fill line style
int shift Rightward displacement of point set coordinates (multiple of reduction)
Point? offset The offset of the global coordinate set. This is useful when the outline is based on the coordinates of a partial area of ​​a large image.
/// <summary>
/// 测试FillPoly
/// </summary>
private void Test_FillPoly()
{
    using (Mat canvas = Mat.Zeros(w, w, MatType.CV_8UC3))
    {
        LineTypes lineType = LineTypes.Link8;
        Point[][] points = new Point[1][];
        points[0] = new Point[4];
        points[0][0] = new Point(w / 8, w / 8);
        points[0][1] = new Point(w / 8 * 3, w / 8 * 3);
        points[0][2] = new Point(w / 8 * 3, w / 8);
        points[0][3] = new Point(w / 8, w / 8 * 3);

        Cv2.FillPoly(canvas, points, new Scalar(255, 0, 0), lineType);

        //shift =2 
        Cv2.FillPoly(canvas, points, new Scalar(0, 0, 255), lineType, shift: 2);

        points[0][2] = new Point(w / 8, w / 8 * 3);
        points[0][3] = new Point(w / 8 * 3, w / 8);
        //坐标偏移w/4
        Cv2.FillPoly(canvas, points, new Scalar(0, 255, 0), lineType, offset: new Point(w / 4, w / 4));

        Utils.ShowWaitDestroy(canvas, "Test FillPoly", WindowFlags.AutoSize);
    }
}

OpenCvSharp function example (catalog )

reference

OpenCV: Basic Drawing

Guess you like

Origin blog.csdn.net/TyroneKing/article/details/129813379