在unity3d中使用openCVSharp转换IPLimage texture2d。

时间:2022-10-10 23:32:49

Like the subject says. i am trying to implement openCVSharp surf in unity3d and kinda stuck in the converting part from iplimage to texture2d. Also considering that this converting proces should run at least at 25 fps. So any tips or suggestions are very helpfull!

像这个话题说。我正在尝试在unity3d中实现openCVSharp的surf,并将其从iplimage转换到texture2d。同时考虑到这种转换过程应该至少在25 fps中运行。所以任何提示或建议都是非常有用的!

3 个解决方案

#1


1  

Might be a bit late, I am working on the same thing now and here is my solution:

可能有点晚了,我现在正在做同样的事情,这是我的解决方案:

void IplImageToTexture2D (IplImage displayImg)
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float b = (float)displayImg[i, j].Val0;
            float g = (float)displayImg[i, j].Val1;
            float r = (float)displayImg[i, j].Val2;
            Color color = new Color(r / 255.0f, g / 255.0f, b / 255.0f);
            videoTexture.SetPixel(j, height - i - 1, color);
        }
    }
    videoTexture.Apply();
}

But it is a bit slow. Still trying to improve the performance.

但它有点慢。还在努力提高性能。

#2


1  

Texture2D tex = new Texture2D(640, 480);
CvMat img = new CvMat(640, 480, MatrixType.U8C3);

byte[] data = new byte[640 * 480 * 3];
Marshal.Copy(img.Data, data, 0, 640 * 480 * 3);
tex.LoadImage(data);

#3


0  

To improve performance use Unity3d's undocumented function LoadRawTextureData :

为了提高性能,使用Unity3d的无记录功能LoadRawTextureData:

Texture2D IplImageToTexture2D(IplImage img)
{
    Texture2D videoTexture = new Texture2D(imWidth, imHeight, TextureFormat.RGB24, false);
    byte[] data = new byte[imWidth * imHeight * 3];
    Marshal.Copy(img.ImageData, data, 0, imWidth * imHeight * 3);
    videoTexture.LoadRawTextureData(data);
    videoTexture.Apply();
    return videoTexture;
}

#1


1  

Might be a bit late, I am working on the same thing now and here is my solution:

可能有点晚了,我现在正在做同样的事情,这是我的解决方案:

void IplImageToTexture2D (IplImage displayImg)
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float b = (float)displayImg[i, j].Val0;
            float g = (float)displayImg[i, j].Val1;
            float r = (float)displayImg[i, j].Val2;
            Color color = new Color(r / 255.0f, g / 255.0f, b / 255.0f);
            videoTexture.SetPixel(j, height - i - 1, color);
        }
    }
    videoTexture.Apply();
}

But it is a bit slow. Still trying to improve the performance.

但它有点慢。还在努力提高性能。

#2


1  

Texture2D tex = new Texture2D(640, 480);
CvMat img = new CvMat(640, 480, MatrixType.U8C3);

byte[] data = new byte[640 * 480 * 3];
Marshal.Copy(img.Data, data, 0, 640 * 480 * 3);
tex.LoadImage(data);

#3


0  

To improve performance use Unity3d's undocumented function LoadRawTextureData :

为了提高性能,使用Unity3d的无记录功能LoadRawTextureData:

Texture2D IplImageToTexture2D(IplImage img)
{
    Texture2D videoTexture = new Texture2D(imWidth, imHeight, TextureFormat.RGB24, false);
    byte[] data = new byte[imWidth * imHeight * 3];
    Marshal.Copy(img.ImageData, data, 0, imWidth * imHeight * 3);
    videoTexture.LoadRawTextureData(data);
    videoTexture.Apply();
    return videoTexture;
}