使用 EncodeToJPG(在 OpenVR 中)将 Texture2D 转换为 Byte[]
Convert Texture2D to Byte[] using EncodeToJPG (in OpenVR)
我有一个问题!
我只想,只是...使用 EncodeToJPG()[= 将 Texture2D 转换为 Byte[] 42=]函数!
我的工作区是 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()));
而且...有问题。变量texture是异常的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[=42= 的 Intptr 类型参数].
不知道怎么办?
+++编辑!错误显示+++
由于 Unity 可能无法直接访问外部纹理,我建议先执行 Graphics.Blit() 以通过 GPU 将该纹理传输到 RenderTexture。
在 RenderTexture 中拥有纹理后,您需要再跳一个圈,并将像素从 RenderTexture.active 读取到另一个标准、基于 RAM 的 Texture2D,然后您应该能够对其进行编码。
记得在执行 ReadPixels() 后应用() 纹理
声明了这个函数:
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);
这完全有效!
我有一个问题!
我只想,只是...使用 EncodeToJPG()[= 将 Texture2D 转换为 Byte[] 42=]函数!
我的工作区是 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()));
而且...有问题。变量texture是异常的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[=42= 的 Intptr 类型参数].
不知道怎么办?
+++编辑!错误显示+++
由于 Unity 可能无法直接访问外部纹理,我建议先执行 Graphics.Blit() 以通过 GPU 将该纹理传输到 RenderTexture。
在 RenderTexture 中拥有纹理后,您需要再跳一个圈,并将像素从 RenderTexture.active 读取到另一个标准、基于 RAM 的 Texture2D,然后您应该能够对其进行编码。
记得在执行 ReadPixels() 后应用() 纹理
声明了这个函数:
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);
这完全有效!