OpenCVSharpSample04WinForms

1、创建一个winform项目

在界面添加一个flowLayoutPanel1,并请它改成窗口大小

2、添加opencvsharp4

3、在form1.cs中添加opencvsharp引用

using OpenCvSharp;
using OpenCvSharp.Extensions;

4、按照例子修改代码

编辑中发现,此行出错

pictureBoxIpl = new OpenCvSharp.UserInterface.PictureBoxIpl

研究发现,opencvSharp3包含了userinterface,但在4中并没有,删去此函数,编译,系统运行如下:

OpenCVSharpSample04WinForms_第1张图片

本程序演示了如何在界面上面显示mat图形

主要思路为

(1)使用函数BitmapConverter.ToBitmap()将图片转为bmp

(2)将bmp加载到PictureBox

(3)将PictureBox添加到flowLayoutPanel1里面

主要代码如下:

Bitmap bitmap;
            using (var iplImage = new Mat(@"..\..\Images\Penguin.png", ImreadModes.AnyDepth | ImreadModes.AnyColor))
            {
                bitmap = iplImage.ToBitmap(); // BitmapConverter.ToBitmap()
            }

            var pictureBox = new PictureBox
            {
                Image = bitmap,
                ClientSize = bitmap.Size
            };

            //How to redraw:
            //iplImage.ToBitmap(dst: (Bitmap)pictureBox.Image);

            flowLayoutPanel1.Controls.Add(pictureBox);

opencvsharp的文档

https://shimat.github.io/opencvsharp_docs/html/d69c29a1-7fb1-4f78-82e9-79be971c3d03.htm

opencvsharp Mat转bitmap
Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(image); //bitmap转 mat
Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat); // mat 转 bitmap

你可能感兴趣的:(vs2019,opencvSharp4)