Skip to content

Commit 106413a

Browse files
author
Pyotr Chekmaryov
committed
Simplest test added and code debuged.
1 parent 2918c3d commit 106413a

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

modules/calib3d/include/opencv2/calib3d.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ class CV_EXPORTS_W StereoSGBM : public StereoMatcher
18101810
MODE_SGBM = 0,
18111811
MODE_HH = 1,
18121812
MODE_SGBM_3WAY = 2,
1813-
MODE_HH4 = 1
1813+
MODE_HH4 = 3
18141814
};
18151815

18161816
CV_WRAP virtual int getPreFilterCap() const = 0;

modules/calib3d/src/stereosgbm.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ static void computeDisparitySGBM( const Mat& img1, const Mat& img2,
513513
6: r=(1, -dy*2)
514514
7: r=(2, -dy)
515515
*/
516+
516517
for( x = x1; x != x2; x += dx )
517518
{
518519
int xm = x*NR2, xd = xm*D2;
@@ -853,6 +854,7 @@ TODO: Don't forget to rewrire this commentaries after
853854
disp2cost also has the same size as img1 (or img2).
854855
It contains the minimum current cost, used to find the best disparity, corresponding to the minimal cost.
855856
*/
857+
#include<stdio.h> //TODO: DUBUG!!!
856858
static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
857859
Mat& disp1, const StereoSGBMParams& params,
858860
Mat& buffer )
@@ -1005,7 +1007,7 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
10051007
if( y > 0 ) //4e: We calculate horizontal sums and forming full block sums for y coord by adding this horsums to previous line's sums and subtracting stored lowest
10061008
{ //4e: horsum in hsumBuf. Exception is case y=0, where we need many iterations per lines to create full blocking sum.
10071009
const CostType* hsumSub = hsumBuf + (std::max(y - SH2 - 1, 0) % hsumBufNRows)*costBufSize;
1008-
const CostType* Cprev = !fullDP || y == 0 ? C : C - costBufSize; //4e: Well, actually y>0, so we don't need this check: y==0
1010+
const CostType* Cprev = C - costBufSize; //4e: Well, actually y>0, so we don't need this check: y==0
10091011

10101012
for( x = D; x < width1*D; x += D )
10111013
{
@@ -1089,11 +1091,11 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
10891091
6: r=(1, -dy*2)
10901092
7: r=(2, -dy)
10911093
*/
1092-
for( x = 0; x != width; x++ )
1094+
for( x = 0; x != width1; x++ )
10931095
{
10941096
int xd = x*D2;
10951097

1096-
int delta = minLr[1][x];
1098+
int delta = minLr[1][x] + P2;
10971099

10981100
CostType* Lr_ppr = Lr[1] + xd;
10991101

@@ -1189,9 +1191,9 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
11891191
L = Cpd + std::min((int)Lr_ppr[d], std::min(Lr_ppr[d-1] + P1, std::min(Lr_ppr[d+1] + P1, delta))) - delta;
11901192

11911193
Lr_p[d] = (CostType)L;
1192-
minL0 = std::min(minL, L);
1194+
minL = std::min(minL, L);
11931195

1194-
Sp[d] = saturate_cast<CostType>(Sp[d] + L0);
1196+
Sp[d] = saturate_cast<CostType>(Sp[d] + L);
11951197
}
11961198
minLr[0][x] = (CostType)minL;
11971199
}
@@ -1224,16 +1226,16 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
12241226
{
12251227
int x, d;
12261228
DispType* disp1ptr = disp1.ptr<DispType>(y);
1227-
CostType* C = Cbuf + costBufSize;
1228-
CostType* S = Sbuf + costBufSize;
1229+
CostType* C = Cbuf + y*costBufSize;
1230+
CostType* S = Sbuf + y*costBufSize;
12291231

12301232
for( x = 0; x < width; x++ )
12311233
{
12321234
disp1ptr[x] = disp2ptr[x] = (DispType)INVALID_DISP_SCALED;
12331235
disp2cost[x] = MAX_COST;
12341236
}
12351237

1236-
// clear the left and the right borders //TODO: Well, two of that memsets we could delete, but rest of them is direction-dependent
1238+
// clear the left and the right borders
12371239
memset( Lr - D2*LrBorder - 8, 0, D2*LrBorder*sizeof(CostType) ); //4e: To understand this "8" shifts and how they could work it's simpler to imagine pixel dislocation in memory
12381240
memset( Lr + width1*D2 - 8, 0, D2*LrBorder*sizeof(CostType) ); //4e: ...00000000|D2-16 of real costs value(and some of them are zeroes too)|00000000...
12391241
memset( minLr - LrBorder, 0, LrBorder*sizeof(CostType) );
@@ -1261,7 +1263,7 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
12611263
{
12621264
int xd = x*D2;
12631265

1264-
int delta = minLr[x - 1];
1266+
int delta = minLr[x - 1] + P2;
12651267

12661268
CostType* Lr_ppr = Lr + xd - D2;
12671269

@@ -1354,22 +1356,22 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
13541356
{
13551357
int Cpd = Cp[d], L; //4e: Remember, that every Cp is increased on P2 in line number 369. That's why next 4 lines are paper-like actually
13561358

1357-
L0 = Cpd + std::min((int)Lr_ppr[d], std::min(Lr_ppr[d-1] + P1, std::min(Lr_ppr[d+1] + P1, delta))) - delta;
1359+
L = Cpd + std::min((int)Lr_ppr[d], std::min(Lr_ppr[d-1] + P1, std::min(Lr_ppr[d+1] + P1, delta))) - delta;
13581360

13591361
Lr_p[d] = (CostType)L;
1360-
minL0 = std::min(minL, L);
1362+
minL = std::min(minL, L);
13611363

13621364
Sp[d] = saturate_cast<CostType>(Sp[d] + L);
13631365
}
1364-
minLr[x] = (CostType)minL0;
1366+
minLr[x] = (CostType)minL;
13651367
}
13661368
}
13671369

13681370
for( x = width1-1; x != -1; x--)
13691371
{
13701372
int xd = x*D2;
13711373

1372-
int delta = minLr[x + 1];
1374+
int delta = minLr[x + 1] + P2;
13731375

13741376
CostType* Lr_ppr = Lr + xd + D2;
13751377

@@ -1489,10 +1491,10 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
14891491
{
14901492
int Cpd = Cp[d], L; //4e: Remember, that every Cp is increased on P2 in line number 369. That's why next 4 lines are paper-like actually
14911493

1492-
L0 = Cpd + std::min((int)Lr_ppr[d], std::min(Lr_ppr[d-1] + P1, std::min(Lr_ppr[d+1] + P1, delta))) - delta;
1494+
L = Cpd + std::min((int)Lr_ppr[d], std::min(Lr_ppr[d-1] + P1, std::min(Lr_ppr[d+1] + P1, delta))) - delta;
14931495

14941496
Lr_p[d] = (CostType)L;
1495-
minL0 = std::min(minL, L);
1497+
minL = std::min(minL, L);
14961498

14971499
Sp[d] = saturate_cast<CostType>(Sp[d] + L);
14981500
if( Sp[d] < minS )
@@ -1501,7 +1503,7 @@ static void computeDisparitySGBMParallel( const Mat& img1, const Mat& img2,
15011503
bestDisp = d;
15021504
}
15031505
}
1504-
minLr[x] = (CostType)minL0;
1506+
minLr[x] = (CostType)minL;
15051507
}
15061508
//Some postprocessing procedures and saving
15071509
for( d = 0; d < D; d++ )
@@ -2206,6 +2208,8 @@ class StereoSGBMImpl : public StereoSGBM
22062208

22072209
if(params.mode==MODE_SGBM_3WAY)
22082210
computeDisparity3WaySGBM( left, right, disp, params, buffers, num_stripes );
2211+
else if(params.mode==MODE_HH4)
2212+
computeDisparitySGBMParallel( left, right, disp, params, buffer );
22092213
else
22102214
computeDisparitySGBM( left, right, disp, params, buffer );
22112215

modules/calib3d/test/test_stereomatching.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,33 @@ class CV_StereoSGBMTest : public CV_StereoMatchingTest
784784

785785
TEST(Calib3d_StereoBM, regression) { CV_StereoBMTest test; test.safe_run(); }
786786
TEST(Calib3d_StereoSGBM, regression) { CV_StereoSGBMTest test; test.safe_run(); }
787+
788+
TEST(Calib3d_StereoSGBMPar, idontknowhowtotesthere)
789+
{
790+
// <!-- caseName, datasetName, numDisp, winSize, mode -->
791+
// case_teddy_2 teddy "48" "3" "MODE_HH"
792+
793+
//Ptr<StereoSGBM> StereoSGBM::create(int minDisparity, int numDisparities, int SADWindowSize,
794+
// int P1, int P2, int disp12MaxDiff,
795+
// int preFilterCap, int uniquenessRatio,
796+
// int speckleWindowSize, int speckleRange,
797+
// int mode)
798+
Mat leftImg = imread("/home/q/Work/GitVault/opencv_extra/testdata/cv/stereomatching/datasets/teddy/im2.png");
799+
Mat rightImg = imread("/home/q/Work/GitVault/opencv_extra/testdata/cv/stereomatching/datasets/teddy/im6.png");
800+
{
801+
Mat leftDisp;
802+
Ptr<StereoSGBM> sgbm = StereoSGBM::create( 0, 48, 3, 90, 360, 1, 63, 10, 100, 32, StereoSGBM::MODE_HH);
803+
sgbm->compute( leftImg, rightImg, leftDisp );
804+
CV_Assert( leftDisp.type() == CV_16SC1 );
805+
leftDisp/=8;
806+
imwrite( "/home/q/Work/GitVault/modehh.jpg", leftDisp);
807+
}
808+
{
809+
Mat leftDisp;
810+
Ptr<StereoSGBM> sgbm = StereoSGBM::create( 0, 48, 3, 90, 360, 1, 63, 10, 100, 32, StereoSGBM::MODE_HH4);
811+
sgbm->compute( leftImg, rightImg, leftDisp );
812+
CV_Assert( leftDisp.type() == CV_16SC1 );
813+
leftDisp/=8;
814+
imwrite( "/home/q/Work/GitVault/modehh4.jpg", leftDisp);
815+
}
816+
}

0 commit comments

Comments
 (0)