Commit 54d41251 authored by Muresan Mircea Paul's avatar Muresan Mircea Paul

aded singe kernel matchings

fixed warnings and error

replaced colls with strides

fixed warnings

replaced colls with strides everywhere

the number of images is estabished at run time

fixed braket warnings

replaced all enums with a single enum

removed whitespaces

fixed last issues

the matching class
this class contains the most important functions used in stereo correspondence
(StereoBM and StereoSGBM) call methods from these classes

fixed shadowing warning problems

included intrin.h header for popcnt instruction

replaced __popcnt with _mm_popcnt_u32() equivalent

fixed some warnings

fixed index warning
parent 7d13acdb
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -47,7 +47,6 @@ by Heiko Hirschmuller. ...@@ -47,7 +47,6 @@ by Heiko Hirschmuller.
We match blocks rather than individual pixels, thus the algorithm is called We match blocks rather than individual pixels, thus the algorithm is called
SGBM (Semi-global block matching) SGBM (Semi-global block matching)
*/ */
#include "precomp.hpp" #include "precomp.hpp"
#include <limits.h> #include <limits.h>
...@@ -58,7 +57,6 @@ namespace cv ...@@ -58,7 +57,6 @@ namespace cv
typedef uchar PixType; typedef uchar PixType;
typedef short CostType; typedef short CostType;
typedef short DispType; typedef short DispType;
enum { NR = 16, NR2 = NR/2 }; enum { NR = 16, NR2 = NR/2 };
struct StereoBinarySGBMParams struct StereoBinarySGBMParams
{ {
...@@ -74,7 +72,6 @@ namespace cv ...@@ -74,7 +72,6 @@ namespace cv
speckleRange = 0; speckleRange = 0;
mode = StereoBinarySGBM::MODE_SGBM; mode = StereoBinarySGBM::MODE_SGBM;
} }
StereoBinarySGBMParams( int _minDisparity, int _numDisparities, int _SADWindowSize, StereoBinarySGBMParams( int _minDisparity, int _numDisparities, int _SADWindowSize,
int _P1, int _P2, int _disp12MaxDiff, int _preFilterCap, int _P1, int _P2, int _disp12MaxDiff, int _preFilterCap,
int _uniquenessRatio, int _speckleWindowSize, int _speckleRange, int _uniquenessRatio, int _speckleWindowSize, int _speckleRange,
...@@ -161,10 +158,8 @@ namespace cv ...@@ -161,10 +158,8 @@ namespace cv
} }
} }
memset( cost, 0, width1*D*sizeof(cost[0]) ); memset( cost, 0, width1*D*sizeof(cost[0]) );
buffer -= minX2; buffer -= minX2;
cost -= minX1*D + minD; // simplify the cost indices inside the loop cost -= minX1*D + minD; // simplify the cost indices inside the loop
#if CV_SSE2 #if CV_SSE2
volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2); volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
#endif #endif
...@@ -298,7 +293,6 @@ namespace cv ...@@ -298,7 +293,6 @@ namespace cv
volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2); volatile bool useSIMD = checkHardwareSupport(CV_CPU_SSE2);
#endif #endif
const int ALIGN = 16; const int ALIGN = 16;
const int DISP_SHIFT = StereoMatcher::DISP_SHIFT; const int DISP_SHIFT = StereoMatcher::DISP_SHIFT;
const int DISP_SCALE = (1 << DISP_SHIFT); const int DISP_SCALE = (1 << DISP_SHIFT);
...@@ -410,17 +404,14 @@ namespace cv ...@@ -410,17 +404,14 @@ namespace cv
for( d = 0; d < D; d++ ) for( d = 0; d < D; d++ )
hsumAdd[d] = (CostType)(hsumAdd[d] + pixDiff[x + d]*scale); hsumAdd[d] = (CostType)(hsumAdd[d] + pixDiff[x + d]*scale);
} }
if( y > 0 ) if( y > 0 )
{ {
const CostType* hsumSub = hsumBuf + (std::max(y - SH2 - 1, 0) % hsumBufNRows)*costBufSize; const CostType* hsumSub = hsumBuf + (std::max(y - SH2 - 1, 0) % hsumBufNRows)*costBufSize;
const CostType* Cprev = !fullDP || y == 0 ? C : C - costBufSize; const CostType* Cprev = !fullDP || y == 0 ? C : C - costBufSize;
for( x = D; x < width1*D; x += D ) for( x = D; x < width1*D; x += D )
{ {
const CostType* pixAdd = pixDiff + std::min(x + SW2*D, (width1-1)*D); const CostType* pixAdd = pixDiff + std::min(x + SW2*D, (width1-1)*D);
const CostType* pixSub = pixDiff + std::max(x - (SW2+1)*D, 0); const CostType* pixSub = pixDiff + std::max(x - (SW2+1)*D, 0);
#if CV_SSE2 #if CV_SSE2
if( useSIMD ) if( useSIMD )
{ {
...@@ -563,28 +554,21 @@ namespace cv ...@@ -563,28 +554,21 @@ namespace cv
#endif #endif
{ {
int minL0 = MAX_COST, minL1 = MAX_COST, minL2 = MAX_COST, minL3 = MAX_COST; int minL0 = MAX_COST, minL1 = MAX_COST, minL2 = MAX_COST, minL3 = MAX_COST;
for( d = 0; d < D; d++ ) for( d = 0; d < D; d++ )
{ {
int Cpd = Cp[d], L0, L1, L2, L3; int Cpd = Cp[d], L0, L1, L2, L3;
L0 = Cpd + std::min((int)Lr_p0[d], std::min(Lr_p0[d-1] + P1, std::min(Lr_p0[d+1] + P1, delta0))) - delta0; L0 = Cpd + std::min((int)Lr_p0[d], std::min(Lr_p0[d-1] + P1, std::min(Lr_p0[d+1] + P1, delta0))) - delta0;
L1 = Cpd + std::min((int)Lr_p1[d], std::min(Lr_p1[d-1] + P1, std::min(Lr_p1[d+1] + P1, delta1))) - delta1; L1 = Cpd + std::min((int)Lr_p1[d], std::min(Lr_p1[d-1] + P1, std::min(Lr_p1[d+1] + P1, delta1))) - delta1;
L2 = Cpd + std::min((int)Lr_p2[d], std::min(Lr_p2[d-1] + P1, std::min(Lr_p2[d+1] + P1, delta2))) - delta2; L2 = Cpd + std::min((int)Lr_p2[d], std::min(Lr_p2[d-1] + P1, std::min(Lr_p2[d+1] + P1, delta2))) - delta2;
L3 = Cpd + std::min((int)Lr_p3[d], std::min(Lr_p3[d-1] + P1, std::min(Lr_p3[d+1] + P1, delta3))) - delta3; L3 = Cpd + std::min((int)Lr_p3[d], std::min(Lr_p3[d-1] + P1, std::min(Lr_p3[d+1] + P1, delta3))) - delta3;
Lr_p[d] = (CostType)L0; Lr_p[d] = (CostType)L0;
minL0 = std::min(minL0, L0); minL0 = std::min(minL0, L0);
Lr_p[d + D2] = (CostType)L1; Lr_p[d + D2] = (CostType)L1;
minL1 = std::min(minL1, L1); minL1 = std::min(minL1, L1);
Lr_p[d + D2*2] = (CostType)L2; Lr_p[d + D2*2] = (CostType)L2;
minL2 = std::min(minL2, L2); minL2 = std::min(minL2, L2);
Lr_p[d + D2*3] = (CostType)L3; Lr_p[d + D2*3] = (CostType)L3;
minL3 = std::min(minL3, L3); minL3 = std::min(minL3, L3);
Sp[d] = saturate_cast<CostType>(Sp[d] + L0 + L1 + L2 + L3); Sp[d] = saturate_cast<CostType>(Sp[d] + L0 + L1 + L2 + L3);
} }
minLr[0][xm] = (CostType)minL0; minLr[0][xm] = (CostType)minL0;
...@@ -593,7 +577,6 @@ namespace cv ...@@ -593,7 +577,6 @@ namespace cv
minLr[0][xm+3] = (CostType)minL3; minLr[0][xm+3] = (CostType)minL3;
} }
} }
if( pass == npasses ) if( pass == npasses )
{ {
for( x = 0; x < width; x++ ) for( x = 0; x < width; x++ )
...@@ -610,21 +593,17 @@ namespace cv ...@@ -610,21 +593,17 @@ namespace cv
if( npasses == 1 ) if( npasses == 1 )
{ {
int xm = x*NR2, xd = xm*D2; int xm = x*NR2, xd = xm*D2;
int minL0 = MAX_COST; int minL0 = MAX_COST;
int delta0 = minLr[0][xm + NR2] + P2; int delta0 = minLr[0][xm + NR2] + P2;
CostType* Lr_p0 = Lr[0] + xd + NRD2; CostType* Lr_p0 = Lr[0] + xd + NRD2;
Lr_p0[-1] = Lr_p0[D] = MAX_COST; Lr_p0[-1] = Lr_p0[D] = MAX_COST;
CostType* Lr_p = Lr[0] + xd; CostType* Lr_p = Lr[0] + xd;
const CostType* Cp = C + x*D; const CostType* Cp = C + x*D;
#if CV_SSE2 #if CV_SSE2
if( useSIMD ) if( useSIMD )
{ {
__m128i _P1 = _mm_set1_epi16((short)P1); __m128i _P1 = _mm_set1_epi16((short)P1);
__m128i _delta0 = _mm_set1_epi16((short)delta0); __m128i _delta0 = _mm_set1_epi16((short)delta0);
__m128i _minL0 = _mm_set1_epi16((short)minL0); __m128i _minL0 = _mm_set1_epi16((short)minL0);
__m128i _minS = _mm_set1_epi16(MAX_COST), _bestDisp = _mm_set1_epi16(-1); __m128i _minS = _mm_set1_epi16(MAX_COST), _bestDisp = _mm_set1_epi16(-1);
__m128i _d8 = _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7), _8 = _mm_set1_epi16(8); __m128i _d8 = _mm_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7), _8 = _mm_set1_epi16(8);
...@@ -645,7 +624,6 @@ namespace cv ...@@ -645,7 +624,6 @@ namespace cv
_bestDisp = _mm_xor_si128(_bestDisp, _mm_and_si128(_mm_xor_si128(_bestDisp,_d8), mask)); _bestDisp = _mm_xor_si128(_bestDisp, _mm_and_si128(_mm_xor_si128(_bestDisp,_d8), mask));
_d8 = _mm_adds_epi16(_d8, _8); _d8 = _mm_adds_epi16(_d8, _8);
} }
short CV_DECL_ALIGNED(16) bestDispBuf[8]; short CV_DECL_ALIGNED(16) bestDispBuf[8];
_mm_store_si128((__m128i*)bestDispBuf, _bestDisp); _mm_store_si128((__m128i*)bestDispBuf, _bestDisp);
_minL0 = _mm_min_epi16(_minL0, _mm_srli_si128(_minL0, 8)); _minL0 = _mm_min_epi16(_minL0, _mm_srli_si128(_minL0, 8));
...@@ -764,8 +742,7 @@ namespace cv ...@@ -764,8 +742,7 @@ namespace cv
disparr.create( left.size(), CV_16S ); disparr.create( left.size(), CV_16S );
Mat disp = disparr.getMat(); Mat disp = disparr.getMat();
computeDisparityBinarySGBM( left, right, disp, params, buffer ); computeDisparityBinarySGBM( left, right, disp, params, buffer );
medianBlur(disp, disp, 3); // medianBlur(disp, disp, 3);
if( params.speckleWindowSize > 0 ) if( params.speckleWindowSize > 0 )
filterSpeckles(disp, (params.minDisparity - 1)*StereoMatcher::DISP_SCALE, params.speckleWindowSize, filterSpeckles(disp, (params.minDisparity - 1)*StereoMatcher::DISP_SCALE, params.speckleWindowSize,
StereoMatcher::DISP_SCALE*params.speckleRange, buffer); StereoMatcher::DISP_SCALE*params.speckleRange, buffer);
...@@ -818,7 +795,6 @@ namespace cv ...@@ -818,7 +795,6 @@ namespace cv
<< "P2" << params.P2 << "P2" << params.P2
<< "mode" << params.mode; << "mode" << params.mode;
} }
void read(const FileNode& fn) void read(const FileNode& fn)
{ {
FileNode n = fn["name"]; FileNode n = fn["name"];
...@@ -835,14 +811,11 @@ namespace cv ...@@ -835,14 +811,11 @@ namespace cv
params.P2 = (int)fn["P2"]; params.P2 = (int)fn["P2"];
params.mode = (int)fn["mode"]; params.mode = (int)fn["mode"];
} }
StereoBinarySGBMParams params; StereoBinarySGBMParams params;
Mat buffer; Mat buffer;
static const char* name_; static const char* name_;
}; };
const char* StereoBinarySGBMImpl::name_ = "StereoMatcher.SGBM"; const char* StereoBinarySGBMImpl::name_ = "StereoMatcher.SGBM";
Ptr<StereoBinarySGBM> StereoBinarySGBM::create(int minDisparity, int numDisparities, int SADWindowSize, Ptr<StereoBinarySGBM> StereoBinarySGBM::create(int minDisparity, int numDisparities, int SADWindowSize,
int P1, int P2, int disp12MaxDiff, int P1, int P2, int disp12MaxDiff,
int preFilterCap, int uniquenessRatio, int preFilterCap, int uniquenessRatio,
...@@ -856,7 +829,6 @@ namespace cv ...@@ -856,7 +829,6 @@ namespace cv
speckleWindowSize, speckleRange, speckleWindowSize, speckleRange,
mode)); mode));
} }
typedef cv::Point_<short> Point2s; typedef cv::Point_<short> Point2s;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment