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(); } } } } }
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(); } } } } }
// ReSharper disable InconsistentNaming #if LANG_JP /// <summary> /// ブロックマッチングアルゴリズムを用いて視差画像を計算する /// </summary> /// <param name="left">左画像.シングルチャンネル,8ビット.</param> /// <param name="right">右画像.左画像と同じサイズ,同じ種類.</param> /// <param name="disparity">出力の視差配列.シングルチャンネル,16ビット,符号有り整数,入力画像と同サイズ.各要素は,計算された視差であり,16倍されて整数値にまるめられる.</param> /// <param name="state">ステレオマッチング構造体.</param> #else /// <summary> /// Computes the disparity map using block matching 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="disparity">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. </param> /// <param name="state">Stereo correspondence structure. </param> #endif public static void FindStereoCorrespondenceBM(CvArr left, CvArr right, CvArr disparity, CvStereoBMState state) { if (left == null) throw new ArgumentNullException("left"); if (right == null) throw new ArgumentNullException("right"); if (disparity == null) throw new ArgumentNullException("disparity"); if (state == null) throw new ArgumentNullException("state"); NativeMethods.cvFindStereoCorrespondenceBM(left.CvPtr, right.CvPtr, disparity.CvPtr, state.CvPtr); }