0

我有一个问题!

我只是想要,只是...使用EncodeToJPG()函数将Texture2D转换为Byte[] !

我的工作区是 Unity 和 C# Script,+ OpenCvSharp。

也许你认为这很容易,但它有一些问题。

此脚本使用 OpenVR(HTC VIVE)。

无论如何,这是我的来源。

var source = SteamVR_TrackedCamera.Source(undistorted);

//Receive texture data from VR.
texture = source.texture;

//Debug.Log(source.texture.width + "/" + source.texture.height);    
//size : 612 /460
if (texture == null)
{
    return;
}

//Input texture data into material, and it's in unity (quad GameObject).
//this G.O print display like camera
material.mainTexture = texture;


//here is my src, I want to save Image but texture.EncodeToJPG has some error.
Cv2.ImShow("_Texture ...", Mat.FromImageData(texture.EncodeToJPG()));
Cv2.ImWrite(SavePath+ "Image.jpg", Mat.FromImageData(texture.EncodeToJPG()));

而且……有问题。变量纹理是异常Texture2D类型。

if (_texture == null)
{
    _texture = Texture2D.CreateExternalTexture((int)header.nWidth, (int)header.nHeight, TextureFormat.RGBA32, false, false, nativeTex);
    //_texture = new Texture2D(612, 460, TextureFormat.RGBA32, false);

    uint width = 0, height = 0;
    var frameBounds = new VRTextureBounds_t();
    if (trackedCamera.GetVideoStreamTextureSize(deviceIndex, frameType, ref frameBounds, ref width, ref height) == EVRTrackedCameraError.None)
    {
        // Account for textures being upside-down in Unity.
        frameBounds.vMin = 1.0f - frameBounds.vMin;
        frameBounds.vMax = 1.0f - frameBounds.vMax;
        this.frameBounds = frameBounds;
    }
}
else
{
    _texture.UpdateExternalTexture(nativeTex);
    //_texture.Apply();
}

_texture是由函数CreateExternalTexture创建的,它具有名为 nativeTex 的 Intptr 类型参数

我不知道我该怎么办?

+++编辑!错误显示 +++

在此处输入图像描述

4

2 回答 2

0

声明了这个函数:

private Texture2D ReadExternalTexture(Texture externalTexture)
{
    Texture2D myTexture2D = new Texture2D(texture.width, texture.height);
    //myTexture2D => empty Texture2D type variable
    if (myTexture2D == null)
    {
        Debug.Log("var: myTexture2D is null state.");
        myTexture2D = new Texture2D(texture.width, texture.height);
    }

    //Make RenderTexture type variable
    RenderTexture tmp = RenderTexture.GetTemporary(
    externalTexture.width,
    externalTexture.height,
    0,
    RenderTextureFormat.ARGB32,
    RenderTextureReadWrite.sRGB);

    Graphics.Blit(externalTexture, tmp);
    RenderTexture previous = RenderTexture.active;
    RenderTexture.active = tmp;

    myTexture2D.ReadPixels(new UnityEngine.Rect(0, 0, tmp.width, tmp.height), 0,     0);
    myTexture2D.Apply();

    RenderTexture.active = previous;
    RenderTexture.ReleaseTemporary(tmp);

    return myTexture2D;
}

并在 void Update() 中使用 ReadExternalTexture 函数:

_texture = ReadExternalTexture(material.mainTexture);

Mat mat = Mat.FromImageData(_texture.EncodeToPNG());

Cv2.Flip(mat, mat, 0);
//Flip(src, dest, 0->updown flip / 1->leftright flip)

Cv2.Resize(mat, mat, new Size(224, 224));

Cv2.ImShow("Image", mat);
//Cv2.ImWrite(SavePath + "Image.jpg", mat);

这完全有效!

于 2018-07-30T01:56:50.893 回答
0

由于 Unity 可能无法直接访问外部纹理,我建议先执行 Graphics.Blit() 以将该纹理通过 GPU 传输到 RenderTexture。

在 RenderTexture 中获得纹理后,您需要再跳一个圈,并将 RenderTexture.active 中的像素读取到另一个标准的基于 RAM 的 Texture2D 中,然后您应该能够对其进行编码。

请记住在执行 ReadPixels() 后应用()纹理

于 2018-07-25T11:03:27.203 回答