Skip to content

Convert Unity Texture2D to OpenCVSharp Mat

■ Texture2D to OpenCVSharp Mat

    Mat Texture2DtoMat(Texture2D texture2d){

        int txWidth= texture2d.width;
        int txHeight = texture2d.height;

        Vec3d[] matData = new Vec3d[txWidth * txWidth];
        Color32[] pixels = texture2d.GetPixels32();

        Parallel.For(0, txHeight, i =>{
            for(int j=0; j < txWidth; j++){
                Color32 col = pixels[j + i * txWidth];
                Vec3d vec3 = new Vec3d{
                    Item0 = col.b,
                    Item1 = col.g,
                    Item2 = col.r
                };
                matData[j + i*txWidth] = vec3;
            }
        }
        );

        Mat mat0 = new Mat(txWidth, txHeight, MatType.CV_64FC3, matData);
        mat0 = mat0.Flip(FlipMode.X);
        return mat0;
    }

When you want to use cvtColor() or something method, you should convert CV_64F to 8UC or other suitable Mattype.

mat.ConvertTo(mat, MatType.CV_8UC3);

Note

This method doesn’t convert some images as shown in this image.
Probably, the MatType property or GetPixel Method is not appropriate in this code.

■ OpenCVSharp Mat to Texture2D

Mat openCVMat = Cv2.ImRead("hogehoge");
texture2d.LoadImage(openCVMat.ImEncode());

Reference

https://forum.unity.com/threads/convert-texture2d-to-opencvsharp-to-mat-and-vice-versa.524872/

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です