C# combined with OpenCV (2) Draw straight lines, circles, rectangles, text, and straight lines with arrows

Select Image

Common default parameters when drawing

Such as rectangle

public static void Rectangle(Mat img, Rect rect,

Scalar color, the color of the drawing

int thickness = 1, the thickness of the drawn line, if it is -1, the area is filled

LineTypes lineType = LineTypes.Link8, the drawing type defaults to 8 connected domains

int shift = 0) The number of decimal places to draw the center point. The default is 0. If the center point is not an integer, it can be modified.

 public static void Drawimage()
        {
            var Src_Images = Cv2.ImRead("lenna.png");

          

            //绘制矩形***********************************
            //使用Point的方式
            OpenCvSharp.Point p1 = new OpenCvSharp.Point(10, 10);
            OpenCvSharp.Point p2 = new OpenCvSharp.Point(20, 20);
            Cv2.Rectangle(Src_Images, p1, p2, Scalar.Green);
            //使用Rect的方式
            Rect roi = new Rect(100, 100, 150, 150);
            Cv2.Rectangle(Src_Images, roi, Scalar.Green);
            //***********************************

            //绘制矩形圆
            //使用中心点+直径的方式
            OpenCvSharp.Point Centerp1 = new OpenCvSharp.Point(10, 10);
            Cv2.Circle(Src_Images, Centerp1, 10, Scalar.Green );
            //使用int 中心点+直径的方式
            Cv2.Circle(Src_Images, 50, 50, 10, Scalar.Green);

            //绘制椭圆
            OpenCvSharp.Point Centerp2 = new OpenCvSharp.Point(100, 100);
            Cv2.Ellipse(Src_Images, Centerp2, new OpenCvSharp.Size(30, 10), 0, 0, 360, Scalar.Green);
            OpenCvSharp.Point Centerp = new OpenCvSharp.Point(120, 120);
            RotatedRect elip = new RotatedRect(Centerp, new Size2f(10, 30), 0);
            Cv2.Ellipse(Src_Images, elip, Scalar.Green);
            //绘制直线
            Cv2.Line(Src_Images, 120, 120, 200, 200, Scalar.Red);
            Cv2.Line(Src_Images, new OpenCvSharp.Point(100, 300), new OpenCvSharp.Point(200, 200), Scalar.Red);
            //添加文本
            Cv2.PutText(Src_Images, "123456", new Point(100,100), HersheyFonts.HersheyComplex, 1, Scalar.AliceBlue);
            //绘制带箭头的直线
            Cv2.ArrowedLine(Src_Images, new OpenCvSharp.Point(300, 100), new OpenCvSharp.Point(200, 200), Scalar.Red);
            //  Cv2.ImShow("原图", Src_Images);
            Cv2.ImShow("绘制后的图像", Src_Images);
        }

 

Guess you like

Origin blog.csdn.net/weixin_43852823/article/details/127743853