在C#中使用OpenCV的.net包装器EmguCV

发布于:2024-11-29 ⋅ 阅读:(30) ⋅ 点赞:(0)

Emgu.CV     OpenCvSharp 两个库都是OpenCV的C#封装。这里不讨论优劣,两个都有相应的用途。

下载安装4.6.0.5131,执行文件exe
https://sourceforge.net/projects/emgucv/files/emgucv/4.6.0/

安装到一个目录下,这里安装到H:\Emgu\ 目录下。

使用示例说明:我们要在C# 里使用OpenCV的一个函数 CvtColor做颜色空间转换;用到了工业相机的原始数据输出,大多数工业相机的输出格式都是 Bayer格式,很多时候都需要转为RGB格式才可以使用,所以这里用OpenCV的CvtColor函数转换。

新建C# 工程项目

工程项目名称

添加库引用

运行64位的

程序完整源码

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 MVSDK;//使用SDK接口
using CameraHandle = System.Int32;
using MvApi = MVSDK.MvApi;
using System.Runtime.InteropServices;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;

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

        private void btnGrab_Click(object sender, EventArgs e)
        {
            CameraSdkStatus status;
            CameraHandle m_hCamera = 0;
            tSdkCameraDevInfo[] tCameraDevInfoList;
            IntPtr ptr;
            IntPtr m_ImageBuffer;
            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            if (status != CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                return;
            }
            if (tCameraDevInfoList == null)
            {
                return;
            }
            status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera);
            if (status != CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                return;
            }
            tSdkCameraCapbility tCameraCapability;
            MvApi.CameraGetCapability(m_hCamera, out tCameraCapability);
            m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 4);
            MvApi.CameraPlay(m_hCamera);

            tSdkFrameHead FrameHead;
            IntPtr uRawBuffer;
            status = MvApi.CameraGetImageBuffer(m_hCamera, out FrameHead, out uRawBuffer, 20000);
            if (status != CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                return;
            }
            MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBuffer, ref FrameHead);
            
            MvApi.CameraSaveImage(m_hCamera, ".\\test.bmp", m_ImageBuffer, ref FrameHead, emSdkFileType.FILE_BMP, 100);

            //--------------------直接转换相机原始数据,假设输出是8位深度
            Mat matTmp0 = new Mat(FrameHead.iHeight, FrameHead.iWidth, DepthType.Cv8U, 1, uRawBuffer, FrameHead.iWidth * 1);
            Mat matRGB = new Mat();

            CvInvoke.CvtColor(matTmp0, matRGB, ColorConversion.BayerGb2Rgb);
            CvInvoke.NamedWindow("OpenCVRaw2RGB", WindowFlags.Normal);
            //CvInvoke.Imshow("OpenCVRaw2RGB", matRGB);
            //CvInvoke.WaitKey(5);
            CvInvoke.Imwrite(".\\testCV.bmp", matRGB);

            MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer);

            MvApi.CameraUnInit(m_hCamera);
            Marshal.FreeHGlobal(m_ImageBuffer);
        }
    }
}

最后采图转换成功的结果

(结束)


网站公告

今日签到

点亮在社区的每一天
去签到