天天看点

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

这里写自定义目录标题

  • Create an OpenCV DLL with VS2019 for Unity 2019
    • 1. VS 2019 创建 DLL
      • 创建 C++ 工程
      • C++ 实现代码
      • OpenCV 文件夹引用设置
    • 2. 在Unity中使用 DLL
      • 创建 Unity 工程
      • 创建使用 dll 的脚本
      • 创建物体
    • 参考资料

Create an OpenCV DLL with VS2019 for Unity 2019

使用VS 2019创建一个使用OpenCV 的插件,以在Unity 2019中使用。

1. VS 2019 创建 DLL

创建 C++ 工程

打开 VS 2019,依次选择 Create a new project,Windows Desktop Wizard,点击 Next 按钮。设定Project Name为OpenCVDll,如下所示。点击 Create 按钮。

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

在弹出的对话框中选择 Dynamic Link Library (.dll),如下所示。

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

修改 Solution Configuration 为 Release,Solution Platforms为 x64。

C++ 实现代码

头文件为OpenCVDll.h,代码如下所示:

#pragma once

#include <stdexcept>
#include <windows.h>
#include <iostream>

#ifdef USINGOPENCV_EXPORTS  
#define USINGOPENCV_EXPORTS_API __declspec(dllexport)   
#else  
#define USINGOPENCV_EXPORTS_API __declspec(dllimport)   
#endif  


namespace UsingOpenCV
{
	class Functions
	{
	public:
		// Initialize OpenCV.
		static USINGOPENCV_EXPORTS_API void Initialize();

		// return each frame.
		static USINGOPENCV_EXPORTS_API BYTE* ProcessFrame();
	};
}
           

源文件为 OpenCVDll.cpp,如下所示:

#include "framework.h"
#include "OpenCVDLL.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

namespace UsingOpenCV
{
	cv::VideoCapture* cap;

	void Functions::Initialize()
	{
		cap = new cv::VideoCapture(0);
	}

	BYTE* Functions::ProcessFrame() {
		cv::Mat image;
		cv::Mat betterImage;

		BYTE* result;

		(*cap) >> image;

		cv::flip(image, image, 0);

		cv::cvtColor(image, betterImage, CV_BGR2BGRA, 4);

		result = new BYTE[image.cols * image.rows * 4];

		memcpy(result, betterImage.data, betterImage.cols * image.rows * 4);
		return result;
	}
}

           

OpenCV 文件夹引用设置

右键点击工程名称,选择 Properties,弹出对话框。

在C/C++,General → Additional Include Directories,添加 OpenCV 的 include文件夹,如下所示:

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

在Linker,General → Additional Library Directories,添加 OpenCV 的 libraries 文件夹,如下所示:

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

在Linker,Input → Additional Dependencies,添加 opencv_world347.lib 文件(依下载的OpenCV版本而定) ,如下所示:

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

右键点击工程名称,选择 Build,即可生成 OpenCVDll.dll 文件。

2. 在Unity中使用 DLL

创建 Unity 工程

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

新建一个Plugins文件夹,并在该文件夹内创建一个x64文件夹,将上面生成的 OpenCVDll.dll 以及 opencv_world347.dll文件拖入该文件夹。

创建使用 dll 的脚本

文件名为,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class UsingDLL : MonoBehaviour
{
    //Lets make our calls from the Plugin
    [DllImport("OpenCVDll", EntryPoint ="[email protected]@BackgroundSubtraction@@SAXXZ")]
    private static extern void Initialize();

    [DllImport("OpenCVDll", EntryPoint = "[email protected]@BackgroundSubtraction@@SAPEAEXZ")]
    private static extern byte[] ProcessFrame();
    Material m;
    void Start()
    {
        Initialize();
        Debug.Log("done");
        m = new Material(Shader.Find("Diffuse"));
    }

    void Update()
    {
        byte[] imgData = ProcessFrame();
        Texture2D tex = new Texture2D(640, 480, TextureFormat.BGRA32, false);
        tex.LoadRawTextureData(imgData);
        tex.Apply();
        m.mainTexture = tex;
        this.GetComponent<Renderer>().material = m;
    }
}
           

其中 dll 引用时的 EntryPoint 是通过 Dependency Walker (http://www.dependencywalker.com/)得到的,如下所示:

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

创建物体

在场景中任意创建一个带有材质文件的物体,如Quad,并将UsingDLL.cs 脚本拖放到这个物体上,如下所示:

在Unity中使用OpenCV DLLCreate an OpenCV DLL with VS2019 for Unity 2019

点击运行,即可在Quad物体上看到摄像头拍摄的视频。

参考资料

  1. Creating an OpenCV DLL with Visual Studio 2017 for Unity 2017