Ejemplo n.º 1
0
        // Use this for initialization
        void Start()
        {
            //Read the left and right images
            Texture2D texLeft  = Resources.Load("tsukuba_l") as Texture2D;
            Texture2D texRight = Resources.Load("tsukuba_r") as Texture2D;
            Mat       imgLeft  = new Mat(texLeft.height, texLeft.width, CvType.CV_8UC1);
            Mat       imgRight = new Mat(texRight.height, texRight.width, CvType.CV_8UC1);

            Utils.texture2DToMat(texLeft, imgLeft);
            Utils.texture2DToMat(texRight, imgRight);
            //or
            //Mat imgLeft = Imgcodecs.imread (Utils.getFilePath ("tsukuba_l.png"), Imgcodecs.IMREAD_GRAYSCALE);
            //Mat imgRight = Imgcodecs.imread (Utils.getFilePath ("tsukuba_r.png"), Imgcodecs.IMREAD_GRAYSCALE);


            Mat imgDisparity16S = new Mat(imgLeft.rows(), imgLeft.cols(), CvType.CV_16S);
            Mat imgDisparity8U  = new Mat(imgLeft.rows(), imgLeft.cols(), CvType.CV_8UC1);

            if (imgLeft.empty() || imgRight.empty())
            {
                Debug.Log("Error reading images ");
            }

            StereoBM sbm = StereoBM.create(16, 15);

            sbm.compute(imgLeft, imgRight, imgDisparity16S);

            //normalize to CvType.CV_8U
            Core.normalize(imgDisparity16S, imgDisparity8U, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);


            Texture2D texture = new Texture2D(imgDisparity8U.cols(), imgDisparity8U.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(imgDisparity8U, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;
        }