天天看點

在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