EnoxSoftware / OpenCVForUnity

OpenCV for Unity (Untiy Asset Plugin)
https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088
557 stars 175 forks source link

Issue with texture2DToMat #175

Closed mattycorbett closed 10 months ago

mattycorbett commented 10 months ago

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;

    Texture2D tempVideoTexturesRGBA;

    rawVideoTexturesRGBA = new Texture2D(1280, 720, TextureFormat.RGBA32, false);

    tempVideoTexturesRGBA = new Texture2D(1280, 720, TextureFormat.RGBA32, false);

    ImageConversion.LoadImage(tempVideoTexturesRGBA, tempByteArray);

        Mat imgMat = new Mat(tempVideoTexturesRGBA.height, tempVideoTexturesRGBA.width, CvType.CV_8UC4);

        OpenCVForUnity.UnityUtils.Utils.texture2DToMat(tempVideoTexturesRGBA, imgMat);

        OpenCVForUnity.UnityUtils.Utils.matToTexture2D(imgMat, rawVideoTexturesRGBA);

`

image

EnoxSoftware commented 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 pca_test1

mattycorbett commented 10 months ago

Thank you!