编程语言
首页 > 编程语言> > Unity C# Texture图像高校传输到C++ dll的方法

Unity C# Texture图像高校传输到C++ dll的方法

作者:互联网

最近在写dll,用opencv处理图像,但是图像信息要从unity那边传过来,我们本来使用的方法是用byte数组传递:

void transferImage(byte* pImg, int width, int height)
{
	image = cv::Mat(height, width, CV_8UC3, pImg).clone();
	flip(p_FaceParam->input_frame, p_FaceParam->input_frame, 0);
}

但是后来发现unity那边获取Texture转成byte还需要一定的时间,所以最近发现了一个更好的办法

这个方法转载自链接:
在Unity3D和OpenCV之间传递图片(Texture2D/WebCamTexture转Mat).

C#:

using System;
using System.Runtime.InteropServices;

Texture2D TextureToTexture2D(Texture texture)
{
    Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
    RenderTexture currentRT = RenderTexture.active;
    RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
    Graphics.Blit(texture, renderTexture);
    RenderTexture.active = renderTexture;
    texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    texture2D.Apply();
    RenderTexture.active = currentRT;
    RenderTexture.ReleaseTemporary(renderTexture);
    return texture2D;
}

pixels = texture2D.GetPixels32();
GCHandle pixelHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
IntPtr pixelPointer = pixelHandle.AddrOfPinnedObject();

C++:

void transferImage(char* pImg, int width, int height)
{
	Mat image(height, width, CV_8UC4);
	memcpy(image.data, pImg, width*height * 4);
	flip(image, image, -1);
	cv::cvtColor(image, image, cv::COLOR_RGBA2BGR);
}

特别注意的是,因为传过来的数据保存到cv::Mat里相当于是整个数据顺序颠倒,所以不光 图像需要做xy轴的镜像,还要转换一次RGBA2BGR。
如果你想直接保存CV_8UC3的话,C#那边需要传TextureFormat.RGB24的。
再次感谢@佐晓佐大神的文章!

标签:RenderTexture,C#,image,Texture,height,width,texture,renderTexture,dll
来源: https://blog.csdn.net/fiendwbc/article/details/112692154