Unity3D官方下载地址
opencv环境配置教程
Dependency Walker下载地址
OpenCV for Unity package提取码:a8h5
参考书籍:Windows核心编程第五版【第19、20章】提取码:u6ps
Unity圣典
vs生成dll
将下图中的两处改为dll编译就会生成dll,改回exe则为应用程序
process.cpp
#include <opencv2\opencv.hpp>
#define DLL_API extern "C" __declspec (dllexport)
DLL_API void transferImage(int rgbInt[], int width, int height, int channel);
void transferImage(int rgbInt[], int width, int height, int channel)
{
cv::Mat image(height, width, CV_MAKETYPE(CV_8U, channel));
uchar* ptr = image.data;
for (int i = 0; i < width*height*channel; i++) {
*ptr++ = (uchar)rgbInt[i];
}
cv::imshow("image", image);
cv::waitKey(10);
// start process image
image = processImage(image);
// end process image
ptr = image.data;
for (int i = 0; i < width*height*channel; i++) {
rgbInt[i] = int(*ptr++);
}
}
生成process.dll,拷贝到unity工程中【yourProjectPath/Assets/Plugins】
如果没有Plugins文件夹,则自己创建
ProcessImage.cs
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using OpenCVForUnity;
public class ProcessImage: MonoBehaviour {
[DllImport("process")]
public extern static void transferImage(int[] rgbInt, int width, int height, int channel);
public int width = 640;
public int height = 480;
public VideoCapture capture = new VideoCapture();
private int[] rgbInt;
private Mat frame;
private Mat gray;
public int cntFrame = 0;
// Use this for initialization
void Start () {
capture.open(0);
rgbInt = new int[width * height * 3];
frame = new Mat(480, 640, CvType.CV_8UC4);
gray = new Mat();
}
// Update is called once per frame
void Update () {
capture.read(frame);
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
frame.get(0, 0, rgbByte);
for (int i = 0; i < width * height*3; i++)
{
rgbInt[i] = (int)rgbByte[i];
}
transferImage(rgbInt, width, height, 3);
for (int i = 0; i < width * height * 3; i++)
{
rgbByte[i] = (byte)rgbInt[i];
}
byte[] bb = new byte[3];
int cnt = 0;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
bb[2] = rgbByte[cnt];
cnt += 1;
bb[1] = rgbByte[cnt];
cnt += 1;
bb[0] = rgbByte[cnt];
cnt += 1;
frame.put(i, j, bb);
}
}
Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false, false);
Utils.matToTexture2D(frame, texture);
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
cntFrame++;
}
}
如果unity运行时找不到process.dll,可能是process.dll调用了其他dll,可以用Dependency Walker查看,将缺少的dll的路径添加到系统环境变量中,unity就可以找到dll了