Closed mattycorbett closed 10 months ago
JPG files are loaded into RGB24 format, PNG files are loaded into ARGB32 format.
https://docs.unity3d.com/ScriptReference/ImageConversion.LoadImage.html
The jpg files are read in RGB24 format, so I changed the code. It worked fine in my environment. OpenCVForUnity 2.5.7 Unity 2019.4.31f
byte[] tempByteArray = ReadJpgFile(Utils.getFilePath("OpenCVForUnity/core/pca_test1.jpg"));
int jpgWidth = 800;
int jpgHeihgt = 600;
Texture2D rawVideoTexturesRGBA;
Texture2D tempVideoTexturesRGB;
rawVideoTexturesRGBA = new Texture2D(jpgWidth, jpgHeihgt, TextureFormat.RGBA32, false);
tempVideoTexturesRGB = new Texture2D(jpgWidth, jpgHeihgt, TextureFormat.RGB24, false);
ImageConversion.LoadImage(tempVideoTexturesRGB, tempByteArray);
Mat imgMat = new Mat(tempVideoTexturesRGB.height, tempVideoTexturesRGB.width, CvType.CV_8UC4);
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(tempVideoTexturesRGB, imgMat);
OpenCVForUnity.UnityUtils.Utils.matToTexture2D(imgMat, rawVideoTexturesRGBA);
static private byte[] ReadJpgFile(string path)
{
var fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
var br = new System.IO.BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
return bytes;
}
pca_test1.jpg
Thank you!
I am trying to convert a Texture2D into a Mat using OpenCVForUnity.UnityUtils.Utils.texture2DToMat. The Texture is created by using the Unity ImageConversion process. I can confirm that the ImageConversion results in an image that is as I expect (right color, right size, right orientation), but when I try to convert it, it shows as greyscale, and distorted (shown below). My code is also below. rawVideoTexturesRGBA is the renderer on the Quad I use to show the image, and tempByteArray is the raw byte[] for the encoded JPG I get from a socket connection.
` public Texture2D rawVideoTexturesRGBA;
`