EnoxSoftware / OpenCVForUnity

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

How to Utils.matToTexture2D for Float types? #192

Open ivilkhovyiameria opened 4 days ago

ivilkhovyiameria commented 4 days ago

I need to convert the result of calcOpticalFlowFarneback CV_32FC2 to Unity texture, e. g. TextureFormat.RGFloat, but when I use Utils.matToTexture2D I see the error "The Mat object must have the types 'CV_8UC4' (RGBA) , 'CV_8UC3' (RGB) or 'CV_8UC1' (GRAY)." So it says that I am forced to only three formats, and only uint. What can I do to work this around? Later in the code I am passing the data into compute shader as a texture. Thank you.

EnoxSoftware commented 3 days ago

Utils.fastMatToTexture2D() can be used to copy to Texture2D of various formats. However, the byte size of the Mat must equal the byte size of the Texture.

            Texture2D uvmap = new Texture2D (512, 512, TextureFormat.RGFloat, false, true) {
                wrapMode = TextureWrapMode.Clamp,
                filterMode = FilterMode.Point,
            };
            Mat mat = new Mat (uvmap.width, uvmap.height, CvType.CV_32FC2);

            Utils.fastMatToTexture2D (mat, uvmap);