Ejemplo n.º 1
0
        public StereoCorrespondence()
        {
            // cvFindStereoCorrespondenceBM + cvFindStereoCorrespondenceGC
            // ブロックマッチング, グラフカットの両アルゴリズムによるステレオマッチング

            // 入力画像の読み込み
            using (IplImage imgLeft = new IplImage(Const.ImageTsukubaLeft, LoadMode.GrayScale))
            using (IplImage imgRight = new IplImage(Const.ImageTsukubaRight, LoadMode.GrayScale))
            {
                // 視差画像, 出力画像の領域を確保
                using (IplImage dispBM = new IplImage(imgLeft.Size, BitDepth.S16, 1))
                using (IplImage dispLeft = new IplImage(imgLeft.Size, BitDepth.S16, 1))
                using (IplImage dispRight = new IplImage(imgLeft.Size, BitDepth.S16, 1))
                using (IplImage dstBM = new IplImage(imgLeft.Size, BitDepth.U8, 1))
                using (IplImage dstGC = new IplImage(imgLeft.Size, BitDepth.U8, 1))
                using (IplImage dstAux = new IplImage(imgLeft.Size, BitDepth.U8, 1))
                using (Mat dstSGBM = new Mat())
                {
                    // 距離計測とスケーリング  
                    int sad = 3;
                    using (CvStereoBMState stateBM = new CvStereoBMState(StereoBMPreset.Basic, 16))
                    using (CvStereoGCState stateGC = new CvStereoGCState(16, 2))
                    using (StereoSGBM sgbm = new StereoSGBM()
                        {
                            MinDisparity = 0,
                            NumberOfDisparities = 32,
                            PreFilterCap = 63,
                            SADWindowSize = sad,
                            P1 = 8 * imgLeft.NChannels * sad * sad,
                            P2 = 32 * imgLeft.NChannels * sad * sad,
                            UniquenessRatio = 10,
                            SpeckleWindowSize = 100,
                            SpeckleRange = 32,
                            Disp12MaxDiff = 1,
                            FullDP = false,
                        })
                    {
                        Cv.FindStereoCorrespondenceBM(imgLeft, imgRight, dispBM, stateBM);                      // stateBM.FindStereoCorrespondence(imgLeft, imgRight, dispBM);
                        Cv.FindStereoCorrespondenceGC(imgLeft, imgRight, dispLeft, dispRight, stateGC, false);  // stateGC.FindStereoCorrespondence(imgLeft, imgRight, dispLeft, dispRight, false);
                        Cv.FindStereoCorrespondence(imgLeft, imgRight, DisparityMode.Birchfield, dstAux, 50, 25, 5, 12, 15, 25);
                        sgbm.FindCorrespondence(new Mat(imgLeft), new Mat(imgRight), dstSGBM);

                        Cv.ConvertScale(dispBM, dstBM, 1);
                        Cv.ConvertScale(dispLeft, dstGC, -16);
                        Cv.ConvertScale(dstAux, dstAux, 16);
                        dstSGBM.ConvertTo(dstSGBM, dstSGBM.Type, 32, 0);                     

                        using (new CvWindow("Stereo Correspondence (BM)", dstBM))
                        using (new CvWindow("Stereo Correspondence (GC)", dstGC))
                        using (new CvWindow("Stereo Correspondence (cvaux)", dstAux))
                        using (new CvWindow("Stereo Correspondence (SGBM)", dstSGBM.ToIplImage()))
                        {
                            Cv.WaitKey();
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Run()
        {
            // Load left&right images
            using (var imgLeft = new IplImage(FilePath.Image.TsukubaLeft, LoadMode.GrayScale))
            using (var imgRight = new IplImage(FilePath.Image.TsukubaRight, LoadMode.GrayScale))
            {
                // output image buffers
                using (var dispBM = new IplImage(imgLeft.Size, BitDepth.S16, 1))
                using (var dispLeft = new IplImage(imgLeft.Size, BitDepth.S16, 1))
                using (var dispRight = new IplImage(imgLeft.Size, BitDepth.S16, 1))
                using (var dstBM = new IplImage(imgLeft.Size, BitDepth.U8, 1))
                using (var dstGC = new IplImage(imgLeft.Size, BitDepth.U8, 1))
                using (var dstAux = new IplImage(imgLeft.Size, BitDepth.U8, 1))
                using (var dstSGBM = new Mat())
                {
                    // measures distance and scales
                    const int sad = 3;
                    using (var stateBM = new CvStereoBMState(StereoBMPreset.Basic, 16))
                    using (var stateGC = new CvStereoGCState(16, 2))
                    using (var sgbm = new StereoSGBM() // C++
                        {
                            MinDisparity = 0,
                            NumberOfDisparities = 32,
                            PreFilterCap = 63,
                            SADWindowSize = sad,
                            P1 = 8 * imgLeft.NChannels * sad * sad,
                            P2 = 32 * imgLeft.NChannels * sad * sad,
                            UniquenessRatio = 10,
                            SpeckleWindowSize = 100,
                            SpeckleRange = 32,
                            Disp12MaxDiff = 1,
                            FullDP = false,
                        })
                    {
                        Cv.FindStereoCorrespondenceBM(imgLeft, imgRight, dispBM, stateBM);   
                        Cv.FindStereoCorrespondenceGC(imgLeft, imgRight, dispLeft, dispRight, stateGC, false); 
                        Cv.FindStereoCorrespondence(imgLeft, imgRight, DisparityMode.Birchfield, dstAux, 50, 25, 5, 12, 15, 25); // cvaux
                        sgbm.Compute(new Mat(imgLeft), new Mat(imgRight), dstSGBM);

                        Cv.ConvertScale(dispBM, dstBM, 1);
                        Cv.ConvertScale(dispLeft, dstGC, -16);
                        Cv.ConvertScale(dstAux, dstAux, 16);
                        dstSGBM.ConvertTo(dstSGBM, dstSGBM.Type(), 32, 0);                     

                        using (new CvWindow("Stereo Correspondence (BM)", dstBM))
                        using (new CvWindow("Stereo Correspondence (GC)", dstGC))
                        using (new CvWindow("Stereo Correspondence (cvaux)", dstAux))
                        using (new CvWindow("Stereo Correspondence (SGBM)", dstSGBM.ToIplImage()))
                        {
                            Cv.WaitKey();
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// グラフカットに基づくアルゴリズムにより視差画像を計算する
        /// </summary>
        /// <param name="left">左画像.シングルチャンネル,8ビット.</param>
        /// <param name="right">右画像.左画像と同じサイズ,同じ種類.</param>
        /// <param name="dispLeft">出力オプション:シングルチャンネル,16ビット,符号有り整数.入力画像と同じサイズの左視差画像. </param>
        /// <param name="dispRight">出力オプション:シングルチャンネル,16ビット,符号有り整数.入力画像と同じサイズの右視差画像. </param>
        /// <param name="state">ステレオマッチング構造体.</param>
        /// <param name="useDisparityGuess">このパラメータが 0 でない場合, あらかじめ定義された視差画像を用いて計算が開始される.つまり, dispLeft と dispRight が共に,妥当な視差画像である必要がある. そうでない場合は,(すべてのピクセルがオクルージョンとなっている)空の視差画像から開始される.</param>
#else
        /// <summary>
        /// Computes the disparity map using graph cut-based algorithm
        /// </summary>
        /// <param name="left">The left single-channel, 8-bit image. </param>
        /// <param name="right">The right image of the same size and the same type. </param>
        /// <param name="dispLeft">The optional output single-channel 16-bit signed left disparity map of the same size as input images. </param>
        /// <param name="dispRight">The optional output single-channel 16-bit signed right disparity map of the same size as input images. </param>
        /// <param name="state">Stereo correspondence structure. </param>
        /// <param name="useDisparityGuess">If the parameter is not zero, the algorithm will start with pre-defined disparity maps. Both dispLeft and dispRight should be valid disparity maps. Otherwise, the function starts with blank disparity maps (all pixels are marked as occlusions). </param>
#endif
        public static void FindStereoCorrespondenceGC(CvArr left, CvArr right, CvArr dispLeft, CvArr dispRight, CvStereoGCState state, bool useDisparityGuess)
        {
            if (left == null)
                throw new ArgumentNullException("left");
            if (right == null)
                throw new ArgumentNullException("right");
            if (dispLeft == null)
                throw new ArgumentNullException("dispLeft");
            if (dispRight == null)
                throw new ArgumentNullException("dispRight");
            if (state == null)
                throw new ArgumentNullException("state");
            NativeMethods.cvFindStereoCorrespondenceGC(left.CvPtr, right.CvPtr, dispLeft.CvPtr, dispRight.CvPtr, state.CvPtr, useDisparityGuess);
        }
Ejemplo n.º 4
0
        // ReSharper restore InconsistentNaming
        #endregion
        #region FindStereoCorrespondenceGC
        // ReSharper disable InconsistentNaming
#if LANG_JP
        /// <summary>
        /// グラフカットに基づくアルゴリズムにより視差画像を計算する
        /// </summary>
        /// <param name="left">左画像.シングルチャンネル,8ビット.</param>
        /// <param name="right">右画像.左画像と同じサイズ,同じ種類.</param>
        /// <param name="dispLeft">出力オプション:シングルチャンネル,16ビット,符号有り整数.入力画像と同じサイズの左視差画像. </param>
        /// <param name="dispRight">出力オプション:シングルチャンネル,16ビット,符号有り整数.入力画像と同じサイズの右視差画像. </param>
        /// <param name="state">ステレオマッチング構造体.</param>
#else
        /// <summary>
        /// Computes the disparity map using graph cut-based algorithm
        /// </summary>
        /// <param name="left">The left single-channel, 8-bit image. </param>
        /// <param name="right">The right image of the same size and the same type. </param>
        /// <param name="dispLeft">The optional output single-channel 16-bit signed left disparity map of the same size as input images. </param>
        /// <param name="dispRight">The optional output single-channel 16-bit signed right disparity map of the same size as input images. </param>
        /// <param name="state">Stereo correspondence structure. </param>
#endif
        public static void FindStereoCorrespondenceGC(CvArr left, CvArr right, CvArr dispLeft, CvArr dispRight, CvStereoGCState state)
        {
            FindStereoCorrespondenceGC(left, right, dispLeft, dispRight, state, false);
        }