OpenCVForUnity icon indicating copy to clipboard operation
OpenCVForUnity copied to clipboard

Errors in Texture2DtoMat !

Open sufeidechabei opened this issue 4 years ago • 1 comments

Here is the code, When Print out the leftimage after convert leftex to mat, it's a white image, and all the pixel values is 205 Texture2D lefttex = new Texture2D(UndistortedImageWidth, UndistortedImageHeight, TextureFormat.RGBA32, false); Texture2D righttex = new Texture2D(UndistortedImageWidth, UndistortedImageHeight, TextureFormat.RGBA32, false); Graphics.CopyTexture(TextureUndistortedDisplayRight, righttex); Graphics.CopyTexture(TextureUndistortedDisplayLeft, lefttex); Mat rightimage = new Mat(TextureUndistortedDisplayRight.height, TextureUndistortedDisplayRight.width, CvType.CV_8UC4); Mat leftimage = new Mat(TextureUndistortedDisplayLeft.height, TextureUndistortedDisplayLeft.width, CvType.CV_8UC4); OpenCVForUnity.ImgcodecsModule.Imgcodecs.imwrite("D:\\eeee.png", leftimage); Utils.texture2DToMat(lefttex, leftimage); Utils.matToTexture2D(leftimage, lefttex); Utils.texture2DToMat(righttex, rightimage); Utils.matToTexture2D(rightimage, righttex); Graphics.CopyTexture(lefttex, TextureUndistortedDisplayLeft); Graphics.CopyTexture(righttex, TextureUndistortedDisplayRight);

sufeidechabei avatar Aug 23 '20 23:08 sufeidechabei

If you use the Graphics.CopyTexture() method to copy Texture2D, the format of TextureUndistortedDisplayRight must be the same as the format of righttex. texture_format

        Texture2D TextureUndistortedDisplayRight = Resources.Load("face") as Texture2D;
        Texture2D TextureUndistortedDisplayLeft = Resources.Load("face") as Texture2D;

        int UndistortedImageWidth = 512;
        int UndistortedImageHeight = 512;


        Texture2D lefttex = new Texture2D(UndistortedImageWidth, UndistortedImageHeight, TextureFormat.RGBA32, false);
        Texture2D righttex = new Texture2D(UndistortedImageWidth, UndistortedImageHeight, TextureFormat.RGBA32, false);
        Graphics.CopyTexture(TextureUndistortedDisplayRight, righttex);
        Graphics.CopyTexture(TextureUndistortedDisplayLeft, lefttex);

        Mat rightimage = new Mat(TextureUndistortedDisplayRight.height, TextureUndistortedDisplayRight.width, CvType.CV_8UC4);
        Mat leftimage = new Mat(TextureUndistortedDisplayLeft.height, TextureUndistortedDisplayLeft.width, CvType.CV_8UC4);

        //OpenCVForUnity.ImgcodecsModule.Imgcodecs.imwrite("D:\\eeee.png", leftimage);

        Utils.texture2DToMat(lefttex, leftimage);

        OpenCVForUnity.ImgcodecsModule.Imgcodecs.imwrite("D:\\eeee.png", leftimage);


        Utils.matToTexture2D(leftimage, lefttex);
        Utils.texture2DToMat(righttex, rightimage);
        Utils.matToTexture2D(rightimage, righttex);
        Graphics.CopyTexture(lefttex, TextureUndistortedDisplayLeft);
        Graphics.CopyTexture(righttex, TextureUndistortedDisplayRight);

        //gameObject.GetComponent<Renderer>().material.mainTexture = lefttex;
        gameObject.GetComponent<Renderer>().material.mainTexture = TextureUndistortedDisplayLeft;

EnoxSoftware avatar Aug 24 '20 07:08 EnoxSoftware