FindStereoCorrespondence() public method

Computes disparity map for the input rectified stereo pair.
Invalid pixels (for which disparity can not be computed) are set to (state->minDisparity-1)*16
public FindStereoCorrespondence ( Byte>.Image left, Byte>.Image right, Int16>.Image disparity ) : void
left Byte>.Image The left single-channel, 8-bit image
right Byte>.Image The right image of the same size and the same type
disparity Int16>.Image The output single-channel 16-bit signed disparity map of the same size as input images. Its elements will be the computed disparities, multiplied by 16 and rounded to integer's
return void
Exemplo n.º 1
0
        public Image<Gray, short> GetDispMapCPU(Image<Gray, byte> leftImg, Image<Gray, byte> rightImg, DispMapFounderParameters parameters)
        {
            var ap = (StereoSGBMDispMapFounderParameters)parameters;

            using (StereoSGBM sgbm = new StereoSGBM(
                minDisparity: ap.MinDisparity,
                numDisparities: ap.NumDisparities,
                blockSize: ap.BlockSize,
                p1: ap.P1,
                p2: ap.P2,
                disp12MaxDiff: ap.Disp12MaxDiff,
                preFilterCap: ap.PreFilterCap,
                uniquenessRatio: ap.UniquenessRatio,
                speckleWindowSize: ap.SpeckleWindowSize,
                speckleRange: ap.SpeckleRange,
                mode: ap.Mode))
            //using (var leftProcessImg = leftImg.Copy())
            //using (var rightProcessImg = rightImg.Copy())
            {
                var leftProcessImg = leftImg;
                var rightProcessImg = rightImg;
                var dispMap = new Image<Gray, short>(leftProcessImg.Size);
                //TODO: dirty hack
                try
                {
                    sgbm.FindStereoCorrespondence(leftProcessImg, rightProcessImg, dispMap);
                }
                catch
                {

                }
                return dispMap;
            }
        }
Exemplo n.º 2
0
 private Image<Gray, short> FindDisparity1(Image<Bgr, Byte> image1, Image<Bgr, Byte> image2)
 {
     var disparity = new Image<Gray, short>(image1.Size);
     using (
         StereoSGBM stereoSolver = new StereoSGBM(-(int)minDispSlider_.Value, (int)numDispSlider_.Value, (int)sadWindowSizeSlider_.Value, (int)p1Slider_.Value, (int)p2Slider_.Value,
             (int)disp12MaxDiffSlider_.Value, (int)preFilterCapSlider_.Value, (int)uniquenessRatioSlider_.Value, (int)speckleWindowSizeSlider_.Value, (int)speckleRangeSlider_.Value, StereoSGBM.Mode.SGBM)
         )
     {
         stereoSolver.FindStereoCorrespondence(image1.Convert<Gray, Byte>(), image2.Convert<Gray, Byte>(),
             disparity);
     }
     return disparity;
 }
Exemplo n.º 3
0
        public static Func<Bitmap> Go(uint w,uint h, byte[] one, byte[] two)
        {
            GCHandle gchOne = default(GCHandle), gchTwo = default(GCHandle);
            try {
                gchOne = GCHandle.Alloc(one,GCHandleType.Pinned);
                var ptrOne = gchOne.AddrOfPinnedObject();
                gchTwo = GCHandle.Alloc(two,GCHandleType.Pinned);
                var ptrTwo = gchTwo.AddrOfPinnedObject();

                var l_image = new Image<Gray,short>((int)w,(int)h,1,ptrOne)
                    .Resize(0.25,Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                var r_image = new Image<Gray,short>((int)w,(int)h,1,ptrTwo)
                    .Resize(0.25,Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);

                var disparity = new Image<Gray, short>(l_image.Size);
                using (StereoSGBM stereoSolver = new StereoSGBM(
                     Config.MinDisparity
                    ,Config.NumDisparities
                    ,Config.SADWindowSize
                    ,Config.P1
                    ,Config.P2
                    ,Config.Disp12MaxDiff
                    ,Config.PreFilterCap
                    ,Config.UniquenessRatio
                    ,Config.SpeckleWindowSize
                    ,Config.SpeckleRange
                    ,StereoSGBM.Mode.SGBM
                )) {
                    stereoSolver.FindStereoCorrespondence(
                        l_image.Convert<Gray, byte>()
                        ,r_image.Convert<Gray, byte>()
                        ,disparity
                    );
                }
                //make it lazy so that it don't have to copy it twice
                return () => disparity.Bitmap;
            }
            finally {
                if (gchOne.IsAllocated) { gchOne.Free(); }
                if (gchTwo.IsAllocated) { gchTwo.Free(); }
            }
        }
Exemplo n.º 4
0
 public static Image<Gray, byte> Compute(StereoSgbmModel model)
 {
     var disparity = new Image<Gray, short>(model.Image1.Size);
     using (var stereoSolver = new StereoSGBM(
         model.MinDisparity,
         model.NumDisparity,
         model.SadWindowSize,
         model.P1,
         model.P2,
         model.Disparity12MaxDiff,
         model.PreFilterCap,
         model.UniquenessRatio,
         model.SpeckleWindowSize,
         model.SpeckleRange,
         model.Mode))
     {
         stereoSolver.FindStereoCorrespondence(model.Image1, model.Image2, disparity);
     }
     return disparity.Convert<Gray, byte>();
 }
Exemplo n.º 5
0
        public void TestStereoSGBMCorrespondence()
        {
            Image<Gray, Byte> left = new Image<Gray, byte>("left.jpg");
             Image<Gray, Byte> right = new Image<Gray, byte>("right.jpg");
             Size size = left.Size;

             Image<Gray, Int16> disparity = new Image<Gray, Int16>(size);

             StereoSGBM bm = new StereoSGBM(10, 64, 0, 0, 0, 0, 0, 0, 0, 0, false);
             Stopwatch watch = Stopwatch.StartNew();
             bm.FindStereoCorrespondence(left, right, disparity);
             watch.Stop();

             Trace.WriteLine(String.Format("Time used: {0} milliseconds", watch.ElapsedMilliseconds));

             Matrix<double> q = new Matrix<double>(4, 4);
             q.SetIdentity();
             MCvPoint3D32f[] points = PointCollection.ReprojectImageTo3D(disparity * (-16), q);

             float min = (float)1.0e10, max = 0;
             foreach (MCvPoint3D32f p in points)
             {
            if (p.z < min) min = p.z;
            else if (p.z > max) max = p.z;
             }
             Trace.WriteLine(String.Format("Min : {0}\r\nMax : {1}", min, max));
        }
        /// <summary>
        /// Given the left and right image, computer the disparity map and the 3D point cloud.
        /// </summary>
        /// <param name="left">The left image</param>
        /// <param name="right">The right image</param>
        /// <param name="disparityMap">The left disparity map</param>
        /// <param name="points">The 3D point cloud within a [-0.5, 0.5] cube</param>
        private void Computer3DPointsFromStereoPair(Image<Gray, Byte> left, Image<Gray, Byte> right, out Image<Gray, short> disparityMap, out MCvPoint3D32f[] points)
        {
            Size size = left.Size;

            disparityMap = new Image<Gray, short>(size);

            int P1 = 8 * 1 * Calibration.SAD * Calibration.SAD;//GetSliderValue(P1_Slider);
            int P2 = 32 * 1 * Calibration.SAD * Calibration.SAD;//GetSliderValue(P2_Slider);

            using (StereoSGBM stereoSolver = new StereoSGBM(
                Calibration.MinDisparities, 
                Calibration.NumDisparities, 
                Calibration.SAD, 
                P1, 
                P2, 
                Calibration.MaxDiff, 
                Calibration.PrefilterCap,
                Calibration.UniquenessRatio,
                Calibration.Speckle,
                Calibration.SpeckleRange,
                Calibration.DisparityMode))
            //using (StereoBM stereoSolver = new StereoBM(Emgu.CV.CvEnum.STEREO_BM_TYPE.BASIC, 0))
            {
                stereoSolver.FindStereoCorrespondence(left, right, disparityMap);//Computes the disparity map using: 
                /*GC: graph cut-based algorithm
                  BM: block matching algorithm
                  SGBM: modified H. Hirschmuller algorithm HH08*/
                points = PointCollection.ReprojectImageTo3D(disparityMap, Calibration.Q); //Reprojects disparity image to 3D space.
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Given the left and right image, computer the disparity map and the 3D point cloud.
        /// </summary>
        /// <param name="leftImage">Left image</param>
        /// <param name="rightImage">Right image</param>
        /// <param name="disparityMap">Left disparity map</param>
        /// <param name="points">The 3D point cloud within a [-0.5, 0.5] cube</param>
        private void Computer3DPointsFromStereoPair(Image<Gray, Byte> leftImage, Image<Gray, Byte> rightImage, out Image<Gray, short> disparityMap, out MCvPoint3D32f[] points)
        {
            // Create disparity map.
            disparityMap = new Image<Gray, short>(leftImage.Size);

            // This is maximum disparity minus minimum disparity.
            // Always greater than 0. In the current implementation this parameter must be divisible by 16.
            int numDisparities = this.GetSliderValue(this.trbDisparities);

            // The minimum possible disparity value. Normally it is 0,
            // but sometimes rectification algorithms can shift images,
            // so this parameter needs to be adjusted accordingly.
            int minDispatities = this.GetSliderValue(this.trbMinDisparities);

            // The matched block size. Must be an odd number >=1.
            // Normally, it should be somewhere in 3..11 range.
            int SAD = this.GetSliderValue(this.trbSADWindow);

            // П1, P2 – Parameters that control disparity smoothness. The larger the values, the smoother the disparity. 
            // P1 is the penalty on the disparity change by plus or minus 1 between neighbor pixels. 
            // P2 is the penalty on the disparity change by more than 1 between neighbor pixels. 
            // The algorithm requires P2 > P1 . 
            // See stereo_match.cpp sample where some reasonably good P1 and P2 values are shown 
            // (like 8 * number_of_image_channels * SADWindowSize * SADWindowSize and 32 * number_of_image_channels * SADWindowSize*SADWindowSize , respectively).
            int P1 = 8 * 1 * SAD * SAD;
            int P2 = 32 * 1 * SAD * SAD;

            // Maximum allowed difference (in integer pixel units) in the left-right disparity check.
            // Set it to non-positive value to disable the check.
            int disp12MaxDiff = GetSliderValue(trbDisp12MaxDiff);

            // Truncation value for the prefiltered image pixels. 
            // The algorithm first computes x-derivative at each pixel and clips its value by [-preFilterCap, preFilterCap] interval. 
            // The result values are passed to the Birchfield-Tomasi pixel cost function.
            int PreFilterCap = GetSliderValue(trbPreFilterCap);

            // The margin in percents by which the best (minimum) computed cost function value
            // should “win” the second best value to consider the found match correct. 
            // Normally, some value within 5-15 range is good enough*/
            int UniquenessRatio = GetSliderValue(trbUniquenessRatio);

            // Maximum disparity variation within each connected component. 
            // If you do speckle filtering, set it to some positive value, multiple of 16. 
            // Normally, 16 or 32 is good enough*/
            int Speckle = GetSliderValue(trbSpeckleWindow);

            // Maximum disparity variation within each connected component.
            // If you do speckle filtering, set it to some positive value,
            // multiple of 16. Normally, 16 or 32 is good enough.
            int SpeckleRange = GetSliderValue(trbSpeckleRange);

            // Set it to true to run full-scale 2-pass dynamic programming algorithm.
            // It will consume O(W*H*numDisparities) bytes, which is large for 640x480 stereo and huge for HD-size pictures.
            // By default this is usually false. Set globally for ease
            // bool fullDP = true;

            using (StereoSGBM stereoSolver = new StereoSGBM(minDispatities, numDisparities, SAD, P1, P2, disp12MaxDiff, PreFilterCap, UniquenessRatio, Speckle, SpeckleRange, this.stereoMode))
            {
                // Computes the disparity map using:
                stereoSolver.FindStereoCorrespondence(leftImage, rightImage, disparityMap);
                // GC: Graph Cut-based algorithm
                // BM: Block Matching algorithm
                // SGBM: modified H. Hirschmuller algorithm HH08.

                // Reprojects disparity image to 3D space.
                points = PointCollection.ReprojectImageTo3D(disparityMap, this.Q); 
            }
        }