【C#】OpenCV使用汇总

一、环境配置

1、.Net框架的版本要至少要大于V4.6以上,否则安装可能会不成功;

2、下载DLL;

菜单栏->工具->NuGet包管理器->管理解决方案的NuGet程序包
网上教程都说是分别安装OpenCvSharp4、OpenCvSharp4.Extensions和OpenCvSharp4.runtime.win三个包;
OpenCvSharp4.windows,这个是将OpenCvSharp4和OpenCvSharp4.runtime.win两个包整合到了一起,但不知道为什么不下载的话,运行会报错(无法找到入口点),所以也要安装。

以下版本供参考,工程框架是.NET4.6.1,是可以跑起来的。

 

 

 

 

 如果NuGet管理包安装失败,可参考这一篇:https://www.cnblogs.com/Mars-0603/p/17119323.html

https://blog.csdn.net/weixin_56423149/article/details/129266399

二、例程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using Point = OpenCvSharp.Point;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            test();
        }

        private void test()
        {
            //例1
            Mat src = new Mat(@"C:\Users\Public\Pictures\Sample Pictures\郁金香.png", ImreadModes.Grayscale);
            Mat dst = new Mat();

            Cv2.Canny(src, dst, 50, 200);
            Cv2.ImShow("src image", src);
            Cv2.ImShow("dst image", dst);
            Cv2.WaitKey(0);


            //例2 创建一张大小为400*600颜色为白色背景的三通道彩色图像
            //int d = 100;
            //Mat img = new Mat(400, 600, MatType.CV_8UC3, new Scalar(255, 255, 255));
            ////
            //Cv2.Line(img, 250, 100, 50, 200, new Scalar(0, 255, 0), 2);
            //Cv2.Rectangle(img, new Rect(50, 50, d, d + 100), new Scalar(0, 0, 255), -1);
            //Cv2.Circle(img, new Point(50, 50), 25, new Scalar(255, 255, 0), -1);

            //Cv2.PutText(img, "OpenCV", new Point(220, 100), HersheyFonts.HersheyComplex, 3, Scalar.Blue, 15);
            //Cv2.PutText(img, "OpenCV", new Point(220, 100), HersheyFonts.HersheyComplex, 3, Scalar.Yellow, 5);

            ////显示图像
            //Cv2.ImShow("img", img);
            ////延时等待按键按下
            //Cv2.WaitKey(0);
        }

    }
}

 

三、Bitmap和Mat格式互转

Mat mat = BitmapConverter.ToMat(bitmap)

Bitmap map = BitmapConverter.ToBitmap(mat)

 

四、图像缩放、框选ROI区域、融合

https://blog.csdn.net/zanllp/article/details/79830571

https://blog.csdn.net/Iawfy_/article/details/123275118

https://blog.51cto.com/u_13267193/5370593

 

posted @ 2023-06-09 09:29  不溯流光  阅读(1475)  评论(0编辑  收藏  举报