OpenCVSharp - 基本绘图功能

OpenCVSharp 跑一遍OpenCV官方教程(全为手敲代码,如有雷同都是我的错)

基本绘图功能

OpenCV教程链接:https://docs.opencv.org/4.5.0/d3/d96/tutorial_basic_geometric_drawing.html

核心语句

画直线:Line()
画椭圆:Ellipse()
画矩形:Rectangle()
画圆:Circle()
画填充多边形:FillPoly()

namespace ConsoleApp1
{
    class Program
    {       
        static void Main(string[] args)
        {
            tutorial1.Run();
        }
    }
}

 

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class tutorial1
    {
        private static int w = 400;
        private static LineTypes lineType = LineTypes.Link8;

        public static void Run()
        {
            string atom_window = "OpenCVSharp Drawing 1: Atom";
            string rook_window = "OpenCVSharp Drawing 2: Rook";
            Mat atom_image = Mat.Zeros(w, w, MatType.CV_8UC3);
            Mat rook_image = Mat.Zeros(w, w, MatType.CV_8UC3);
            MyEllipse(atom_image, 90);
            MyEllipse(atom_image, 0);
            MyEllipse(atom_image, 45);
            MyEllipse(atom_image, -45);
            MyFilledCircle(atom_image, new Point(w / 2, w / 2));
            MyPolygon(rook_image);

            Cv2.Rectangle(rook_image,
                   new Point(0, 7 * w / 8),
                   new Point(w, w),
                   new Scalar(0, 255, 255),
                   -1,
                   lineType);
            MyLine(rook_image, new Point(0, 15 * w / 16), new Point(w, 15 * w / 16));
            MyLine(rook_image, new Point(w / 4, 7 * w / 8), new Point(w / 4, w));
            MyLine(rook_image, new Point(w / 2, 7 * w / 8), new Point(w / 2, w));
            MyLine(rook_image, new Point(3 * w / 4, 7 * w / 8), new Point(3 * w / 4, w));

            Cv2.ImShow(atom_window, atom_image);
            Cv2.MoveWindow(atom_window, 0, 200);
            Cv2.ImShow(rook_window, rook_image);
            Cv2.MoveWindow(rook_window, w, 200);
            Cv2.WaitKey(0);

        }

        private static void MyFilledCircle(Mat img, Point center)
        {
            Cv2.Circle(img, center, w / 32, new Scalar(0, 0, 255));
        }

        private static void MyEllipse(Mat img, double angle)
        {
            int thickness = 2;
            LineTypes lineType = LineTypes.Link8;
            Cv2.Ellipse(
                img, 
                new Point(w / 2, w / 2), 
                new Size(w / 4, w / 16), 
                angle, 
                0, 
                360, 
                new Scalar(255, 0, 0), 
                thickness, 
                lineType);
        }

        private static void MyPolygon(Mat img)
        {

            Point[] rook_points = new Point[20];
            rook_points[0] = new Point(w / 4, 7 * w / 8);
            rook_points[1] = new Point(3 * w / 4, 7 * w / 8);
            rook_points[2] = new Point(3 * w / 4, 13 * w / 16);
            rook_points[3] = new Point(11 * w / 16, 13 * w / 16);
            rook_points[4] = new Point(19 * w / 32, 3 * w / 8);
            rook_points[5] = new Point(3 * w / 4, 3 * w / 8);
            rook_points[6] = new Point(3 * w / 4, w / 8);
            rook_points[7] = new Point(26 * w / 40, w / 8);
            rook_points[8] = new Point(26 * w / 40, w / 4);
            rook_points[9] = new Point(22 * w / 40, w / 4);
            rook_points[10] = new Point(22 * w / 40, w / 8);
            rook_points[11] = new Point(18 * w / 40, w / 8);
            rook_points[12] = new Point(18 * w / 40, w / 4);
            rook_points[13] = new Point(14 * w / 40, w / 4);
            rook_points[14] = new Point(14 * w / 40, w / 8);
            rook_points[15] = new Point(w / 4, w / 8);
            rook_points[16] = new Point(w / 4, 3 * w / 8);
            rook_points[17] = new Point(13 * w / 32, 3 * w / 8);
            rook_points[18] = new Point(5 * w / 16, 13 * w / 16);
            rook_points[19] = new Point(w / 4, 13 * w / 16);

            Cv2.FillPoly(img,
                  new Point[][] { rook_points },
                  new Scalar(255, 255, 255),
                  lineType);
        }

        private static void MyLine(Mat img, Point start, Point end)
        {
            int thickness = 2;

            Cv2.Line(img,
              start,
              end,
              new Scalar(0, 0, 0),
              thickness,
              lineType);
        }
    }
}

运行结果

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值