From 0e9c8e2dd5f20ffbedbe0af1b9aaa6b104f7ffca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Br=C3=83=C2=A1s?= Date: Fri, 15 Jul 2016 19:55:19 -0300 Subject: [PATCH 1/4] 'resolves' 6898 I have seen that you can input a Mat_ on StereoBM, but the value seems the same as CV_16S. I changed it so, only if you input a Mat_ it makes use of a previously truncated 4 bits, giving more resolution to Disparity Matrix. (The algorithm stays the same, it's not more precise). If any other input Mat is given, it changes nothing. --- modules/calib3d/src/stereobm.cpp | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/calib3d/src/stereobm.cpp b/modules/calib3d/src/stereobm.cpp index 83ed9ae0c..9df510080 100644 --- a/modules/calib3d/src/stereobm.cpp +++ b/modules/calib3d/src/stereobm.cpp @@ -317,7 +317,9 @@ prefilterXSobel( const Mat& src, Mat& dst, int ftzero ) } -static const int DISPARITY_SHIFT = 4; +static int DISPARITY_SHIFT; +static const int DISPARITY_SHIFT_16S = 4; +static const int DISPARITY_SHIFT_32S = 8; #if CV_SSE2 static void findStereoCorrespondenceBM_SSE2( const Mat& left, const Mat& right, @@ -568,8 +570,9 @@ static void findStereoCorrespondenceBM_SSE2( const Mat& left, const Mat& right, } #endif +template static void -findStereoCorrespondenceBM( const Mat& left, const Mat& right, +findStereoCorrespondenceBM_( const Mat& left, const Mat& right, Mat& disp, Mat& cost, const StereoBMParams& state, uchar* buf, int _dy0, int _dy1 ) { @@ -587,7 +590,7 @@ findStereoCorrespondenceBM( const Mat& left, const Mat& right, int ftzero = state.preFilterCap; int textureThreshold = state.textureThreshold; int uniquenessRatio = state.uniquenessRatio; - short FILTERED = (short)((mindisp - 1) << DISPARITY_SHIFT); + mType FILTERED = (mType)((mindisp - 1) << DISPARITY_SHIFT); #if CV_NEON CV_Assert (ndisp % 8 == 0); @@ -603,7 +606,7 @@ findStereoCorrespondenceBM( const Mat& left, const Mat& right, const uchar* lptr0 = left.ptr() + lofs; const uchar* rptr0 = right.ptr() + rofs; const uchar *lptr, *lptr_sub, *rptr; - short* dptr = disp.ptr(); + mType* dptr = disp.ptr(); int sstep = (int)left.step; int dstep = (int)(disp.step/sizeof(dptr[0])); int cstep = (height+dy0+dy1)*ndisp; @@ -846,13 +849,30 @@ findStereoCorrespondenceBM( const Mat& left, const Mat& right, sad[ndisp] = sad[ndisp-2]; int p = sad[mind+1], n = sad[mind-1]; d = p + n - 2*sad[mind] + std::abs(p - n); - dptr[y*dstep] = (short)(((ndisp - mind - 1 + mindisp)*256 + (d != 0 ? (p-n)*256/d : 0) + 15) >> 4); + dptr[y*dstep] = (mType)(((ndisp - mind - 1 + mindisp)*256 + (d != 0 ? (p-n)*256/d : 0) + 15) + >> (DISPARITY_SHIFT_32S - DISPARITY_SHIFT)); costptr[y*coststep] = sad[mind]; } } } } +static void +findStereoCorrespondenceBM( const Mat& left, const Mat& right, + Mat& disp, Mat& cost, const StereoBMParams& state, + uchar* buf, int _dy0, int _dy1 ) +{ + if(disp.type() == CV_16S){ + DISPARITY_SHIFT = DISPARITY_SHIFT_16S; + findStereoCorrespondenceBM_(left, right, disp, cost, state, + buf, _dy0, _dy1 ); + } else { + DISPARITY_SHIFT = DISPARITY_SHIFT_32S; + findStereoCorrespondenceBM_(left, right, disp, cost, state, + buf, _dy0, _dy1 ); + } +} + #ifdef HAVE_OPENCL static bool ocl_prefiltering(InputArray left0, InputArray right0, OutputArray left, OutputArray right, StereoBMParams* state) { @@ -1129,7 +1149,7 @@ public: Mat disp = disp0; if( dtype == CV_32F ) { - dispbuf.create(disp0.size(), CV_16S); + dispbuf.create(disp0.size(), CV_32S); disp = dispbuf; } From 440ba2e4a7c9b1d0773abf3f955c6ecd9f84fe7a Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 20 Jul 2016 22:57:55 -0300 Subject: [PATCH 2/4] Passing Disparity Shift as a parameter. --- modules/calib3d/src/stereobm.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/modules/calib3d/src/stereobm.cpp b/modules/calib3d/src/stereobm.cpp index 9df510080..98a4d6939 100644 --- a/modules/calib3d/src/stereobm.cpp +++ b/modules/calib3d/src/stereobm.cpp @@ -317,7 +317,6 @@ prefilterXSobel( const Mat& src, Mat& dst, int ftzero ) } -static int DISPARITY_SHIFT; static const int DISPARITY_SHIFT_16S = 4; static const int DISPARITY_SHIFT_32S = 8; @@ -574,7 +573,7 @@ template static void findStereoCorrespondenceBM_( const Mat& left, const Mat& right, Mat& disp, Mat& cost, const StereoBMParams& state, - uchar* buf, int _dy0, int _dy1 ) + uchar* buf, int _dy0, int _dy1, const int disp_shift ) { const int ALIGN = 16; @@ -590,7 +589,7 @@ findStereoCorrespondenceBM_( const Mat& left, const Mat& right, int ftzero = state.preFilterCap; int textureThreshold = state.textureThreshold; int uniquenessRatio = state.uniquenessRatio; - mType FILTERED = (mType)((mindisp - 1) << DISPARITY_SHIFT); + mType FILTERED = (mType)((mindisp - 1) << disp_shift); #if CV_NEON CV_Assert (ndisp % 8 == 0); @@ -850,7 +849,7 @@ findStereoCorrespondenceBM_( const Mat& left, const Mat& right, int p = sad[mind+1], n = sad[mind-1]; d = p + n - 2*sad[mind] + std::abs(p - n); dptr[y*dstep] = (mType)(((ndisp - mind - 1 + mindisp)*256 + (d != 0 ? (p-n)*256/d : 0) + 15) - >> (DISPARITY_SHIFT_32S - DISPARITY_SHIFT)); + >> (DISPARITY_SHIFT_32S - disp_shift)); costptr[y*coststep] = sad[mind]; } } @@ -862,15 +861,12 @@ findStereoCorrespondenceBM( const Mat& left, const Mat& right, Mat& disp, Mat& cost, const StereoBMParams& state, uchar* buf, int _dy0, int _dy1 ) { - if(disp.type() == CV_16S){ - DISPARITY_SHIFT = DISPARITY_SHIFT_16S; + if(disp.type() == CV_16S) findStereoCorrespondenceBM_(left, right, disp, cost, state, - buf, _dy0, _dy1 ); - } else { - DISPARITY_SHIFT = DISPARITY_SHIFT_32S; + buf, _dy0, _dy1, DISPARITY_SHIFT_16S ); + else findStereoCorrespondenceBM_(left, right, disp, cost, state, - buf, _dy0, _dy1 ); - } + buf, _dy0, _dy1, DISPARITY_SHIFT_32S ); } #ifdef HAVE_OPENCL From c6f790ce12966850b0a40c8ba73f547afeae7bfe Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 26 Jul 2016 23:29:05 -0300 Subject: [PATCH 3/4] Fixed using local variable instead of global var. - Still need to change SSE_2 --- modules/calib3d/src/stereobm.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/calib3d/src/stereobm.cpp b/modules/calib3d/src/stereobm.cpp index 98a4d6939..58de58772 100644 --- a/modules/calib3d/src/stereobm.cpp +++ b/modules/calib3d/src/stereobm.cpp @@ -338,7 +338,7 @@ static void findStereoCorrespondenceBM_SSE2( const Mat& left, const Mat& right, int ftzero = state.preFilterCap; int textureThreshold = state.textureThreshold; int uniquenessRatio = state.uniquenessRatio; - short FILTERED = (short)((mindisp - 1) << DISPARITY_SHIFT); + short FILTERED = (short)((mindisp - 1) << DISPARITY_SHIFT_16S); ushort *sad, *hsad0, *hsad, *hsad_sub; int *htext; @@ -1096,7 +1096,14 @@ public: if( params.uniquenessRatio < 0 ) CV_Error( Error::StsOutOfRange, "uniqueness ratio must be non-negative" ); - int FILTERED = (params.minDisparity - 1) << DISPARITY_SHIFT; + int disp_shift; + if (dtype == CV_16SC1) + disp_shift = DISPARITY_SHIFT_16S; + else + disp_shift = DISPARITY_SHIFT_32S; + + + int FILTERED = (params.minDisparity - 1) << disp_shift; #ifdef HAVE_OPENCL if(ocl::useOpenCL() && disparr.isUMat() && params.textureThreshold == 0) @@ -1109,7 +1116,7 @@ public: if( params.speckleRange >= 0 && params.speckleWindowSize > 0 ) filterSpeckles(disparr.getMat(), FILTERED, params.speckleWindowSize, params.speckleRange, slidingSumBuf); if (dtype == CV_32F) - disparr.getUMat().convertTo(disparr, CV_32FC1, 1./(1 << DISPARITY_SHIFT), 0); + disparr.getUMat().convertTo(disparr, CV_32FC1, 1./(1 << disp_shift), 0); CV_IMPL_ADD(CV_IMPL_OCL); return; } @@ -1138,7 +1145,7 @@ public: if( lofs >= width || rofs >= width || width1 < 1 ) { - disp0 = Scalar::all( FILTERED * ( disp0.type() < CV_32F ? 1 : 1./(1 << DISPARITY_SHIFT) ) ); + disp0 = Scalar::all( FILTERED * ( disp0.type() < CV_32F ? 1 : 1./(1 << disp_shift) ) ); return; } @@ -1194,7 +1201,7 @@ public: filterSpeckles(disp, FILTERED, params.speckleWindowSize, params.speckleRange, slidingSumBuf); if (disp0.data != disp.data) - disp.convertTo(disp0, disp0.type(), 1./(1 << DISPARITY_SHIFT), 0); + disp.convertTo(disp0, disp0.type(), 1./(1 << disp_shift), 0); } int getMinDisparity() const { return params.minDisparity; } From 718891d9377dd09bc7e16e4d3430c8b934ca0602 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 26 Jul 2016 23:46:58 -0300 Subject: [PATCH 4/4] Tabs -> Whitespaces. --- modules/calib3d/src/stereobm.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/calib3d/src/stereobm.cpp b/modules/calib3d/src/stereobm.cpp index 58de58772..c4278b361 100644 --- a/modules/calib3d/src/stereobm.cpp +++ b/modules/calib3d/src/stereobm.cpp @@ -1096,14 +1096,14 @@ public: if( params.uniquenessRatio < 0 ) CV_Error( Error::StsOutOfRange, "uniqueness ratio must be non-negative" ); - int disp_shift; - if (dtype == CV_16SC1) - disp_shift = DISPARITY_SHIFT_16S; - else - disp_shift = DISPARITY_SHIFT_32S; + int disp_shift; + if (dtype == CV_16SC1) + disp_shift = DISPARITY_SHIFT_16S; + else + disp_shift = DISPARITY_SHIFT_32S; - int FILTERED = (params.minDisparity - 1) << disp_shift; + int FILTERED = (params.minDisparity - 1) << disp_shift; #ifdef HAVE_OPENCL if(ocl::useOpenCL() && disparr.isUMat() && params.textureThreshold == 0) @@ -1116,7 +1116,7 @@ public: if( params.speckleRange >= 0 && params.speckleWindowSize > 0 ) filterSpeckles(disparr.getMat(), FILTERED, params.speckleWindowSize, params.speckleRange, slidingSumBuf); if (dtype == CV_32F) - disparr.getUMat().convertTo(disparr, CV_32FC1, 1./(1 << disp_shift), 0); + disparr.getUMat().convertTo(disparr, CV_32FC1, 1./(1 << disp_shift), 0); CV_IMPL_ADD(CV_IMPL_OCL); return; } @@ -1145,7 +1145,7 @@ public: if( lofs >= width || rofs >= width || width1 < 1 ) { - disp0 = Scalar::all( FILTERED * ( disp0.type() < CV_32F ? 1 : 1./(1 << disp_shift) ) ); + disp0 = Scalar::all( FILTERED * ( disp0.type() < CV_32F ? 1 : 1./(1 << disp_shift) ) ); return; } @@ -1201,7 +1201,7 @@ public: filterSpeckles(disp, FILTERED, params.speckleWindowSize, params.speckleRange, slidingSumBuf); if (disp0.data != disp.data) - disp.convertTo(disp0, disp0.type(), 1./(1 << disp_shift), 0); + disp.convertTo(disp0, disp0.type(), 1./(1 << disp_shift), 0); } int getMinDisparity() const { return params.minDisparity; }