首先从相机或者Texture中获得存储图像块
Color32[] pixels = Texture2D.GetPixels32();
//或者WebCamTexture.GetPixels32 ();
再使用GCHandle获得块的指针
GCHandle pixelHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
IntPtr pixelPointer = pixelHandle.AddrOfPinnedObject();
然后将此指针传递给OpenCV使用即可。以dll方式进行数据传递。
unity3d中调用如下
[DllImport(@"Unity2OpenCVImage.dll")]
private static extern void Unity2OpenCVImage(IntPtr data,int width,int height,int stride);
opencv中的dll函数为
void Unity2OpenCVImage(char* imageData,int width,int height,int stride) { if(NULL==imageData) { return; } cv::Mat opencvImage(height,width,CV_8UC4); memcpy(opencvImage.data,imageData,stride*height); cv::cvtColor(opencvImage,opencvImage,CV_BGR2RGB); // cv::imshow("11",opencvImage); // cv::waitKey(1); } }