0

我有一个来自 opencv c++ 的 bgr 图像 uchar 格式。

该函数就像 int* texture(int* data, int width, int height); 该函数在 C++ 端处理图像并返回指向数据的指针。如何将 Unity 中的这些数据转换为纹理。基本上使这些数据可用作纹理。我不想将其写入文件。请帮忙。

代码片段(我正在使用 dll):::

public static WebCamTexture webCamTexture;
 private Color32[] data;
 private int[] imageData;
 private int[] imdat;

void Start () {
....

data = new Color32[webCamTexture.width * webCamTexture.height];
        imageData = new int[data.Length * 3];
}


void Update()
    {

        webCamTexture.GetPixels32(data);

        // Convert the Color32[] in int* and emit it in bgr format 
        for (int i = 0; i < data.Length; ++i)
        {
            imageData[i * 3] = (int)data[i].b;
            imageData[i * 3 + 1] = (int)data[i].g;
            imageData[i * 3 + 2] = (int)data[i].r;
        }

       //this is the function called from dll
       imdat =  texture(imageData, int width, int height); 
}

DLL 端看起来像 ::

char *tmp;

int* texture(int* imageData ,int width ,int height)
{
int n = w * h * 3;
    tmp = new char[n];

    //ImageData inverted here and then passed onto tmp 3 channels image
    for (int i = 0; i < (w*3); ++i)
            for (int j = 0; j < h; ++j)
                tmp[i + j * (w*3)] = (char)imageData[i + (h - j - 1) * (w*3)];

return (int)tmp;

}
4

2 回答 2

1

I'm not sure what format texture you have is, but if you can convert it into byte[] you can use Texture2D.LoadImage(byte[]) to turn in into working texture.

于 2013-08-18T11:54:20.173 回答
1

您应该能够使用BitConverter.GetBytes()Texture2D.LoadImage()实现您想要的。请务必特别注意 Unity 手册页中的图像格式限制。

不确定您的 C++land 和 C#land 代码之间的绑定方式,但您应该能够执行以下操作:

/* iImportedTexture = [Your c++ function here]; */

byte[] bImportedTexture = BitConverter.GetBytes(iImportedTexture);

Texture2D importedTexture = Texture2D.LoadImage(bImportedTexture);
于 2013-08-19T05:22:35.497 回答