Skip to content

Commit 12b530c

Browse files
committed
modified stereo for better results
1 parent 1ae4d2a commit 12b530c

File tree

3 files changed

+46
-53
lines changed

3 files changed

+46
-53
lines changed

modules/stereo/src/matching.hpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -281,81 +281,81 @@ namespace cv
281281
}
282282
};
283283
//!median 1x9 paralelized filter
284+
template <typename T>
284285
class Median1x9:public ParallelLoopBody
285286
{
286287
private:
287-
uint8_t *original;
288-
uint8_t *filtered;
289-
int height, width,_stride;
288+
T *original;
289+
T *filtered;
290+
int height, width;
290291
public:
291292
Median1x9(const Mat &originalImage, Mat &filteredImage)
292293
{
293-
original = originalImage.data;
294-
filtered = filteredImage.data;
294+
original = (T *)originalImage.data;
295+
filtered = (T *)filteredImage.data;
295296
height = originalImage.rows;
296297
width = originalImage.cols;
297-
_stride = (int)originalImage.step;
298298
}
299299
void operator()(const cv::Range &r) const{
300300
for (int m = r.start; m <= r.end; m++)
301301
{
302302
for (int n = 4; n < width - 4; ++n)
303303
{
304304
int k = 0;
305-
uint8_t window[9];
305+
T window[9];
306306
for (int i = n - 4; i <= n + 4; ++i)
307-
window[k++] = original[m * _stride + i];
307+
window[k++] = original[m * width + i];
308308
for (int j = 0; j < 5; ++j)
309309
{
310310
int min = j;
311311
for (int l = j + 1; l < 9; ++l)
312312
if (window[l] < window[min])
313313
min = l;
314-
const uint8_t temp = window[j];
314+
const T temp = window[j];
315315
window[j] = window[min];
316316
window[min] = temp;
317317
}
318-
filtered[m * _stride + n] = window[4];
318+
filtered[m * width + n] = window[4];
319319
}
320320
}
321321
}
322322
};
323323
//!median 9x1 paralelized filter
324+
template <typename T>
324325
class Median9x1:public ParallelLoopBody
325326
{
326327
private:
327-
uint8_t *original;
328-
uint8_t *filtered;
329-
int height, width, _stride;
328+
T *original;
329+
T *filtered;
330+
int height, width;
330331
public:
331332
Median9x1(const Mat &originalImage, Mat &filteredImage)
332333
{
333-
original = originalImage.data;
334-
filtered = filteredImage.data;
334+
original = (T *)originalImage.data;
335+
filtered = (T *)filteredImage.data;
335336
height = originalImage.rows;
336337
width = originalImage.cols;
337-
_stride = (int)originalImage.step;
338338
}
339339
void operator()(const Range &r) const{
340340
for (int n = r.start; n <= r.end; ++n)
341341
{
342342
for (int m = 4; m < height - 4; ++m)
343343
{
344344
int k = 0;
345-
uint8_t window[9];
345+
T window[9];
346346
for (int i = m - 4; i <= m + 4; ++i)
347-
window[k++] = original[i * _stride + n];
347+
window[k++] = original[i * width + n];
348348
for (int j = 0; j < 5; j++)
349349
{
350350
int min = j;
351351
for (int l = j + 1; l < 9; ++l)
352352
if (window[l] < window[min])
353353
min = l;
354-
const uint8_t temp = window[j];
354+
const T temp = window[j];
355355
window[j] = window[min];
356356
window[min] = temp;
357357
}
358-
filtered[m * _stride + n] = window[4];
358+
filtered[m * width + n] = window[4];
359359
}
360360
}
361361
}
@@ -471,6 +471,7 @@ namespace cv
471471
parallel_for_(cv::Range(win + 1,height - win - 1), agregateCost(partialSums,windowSize,maxDisp,cost));
472472
}
473473
//!remove small regions that have an area smaller than t, we fill the region with the average of the good pixels around it
474+
template <typename T>
474475
void smallRegionRemoval(const Mat &currentMap, int t, Mat &out)
475476
{
476477
CV_Assert(currentMap.cols == out.cols);
@@ -480,11 +481,11 @@ namespace cv
480481
int *specklePointX = (int *)speckleX.data;
481482
int *specklePointY = (int *)speckleY.data;
482483
memset(pus, 0, previous_size * sizeof(pus[0]));
483-
uint8_t *map = currentMap.data;
484-
uint8_t *outputMap = out.data;
484+
T *map = (T *)currentMap.data;
485+
T *outputMap = (T *)out.data;
485486
int height = currentMap.rows;
486487
int width = currentMap.cols;
487-
uint8_t k = 1;
488+
T k = 1;
488489
int st, dr;
489490
int di[] = { -1, -1, -1, 0, 1, 1, 1, 0 },
490491
dj[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
@@ -502,8 +503,8 @@ namespace cv
502503
}
503504
else if (map[iw + j] == 0)
504505
{
505-
int nr = 1;
506-
int avg = 0;
506+
T nr = 1;
507+
T avg = 0;
507508
speckle_size = dr;
508509
specklePointX[dr] = i;
509510
specklePointY[dr] = j;
@@ -520,7 +521,7 @@ namespace cv
520521
if (ii + di[d] >= 0 && ii + di[d] < height && jj + dj[d] >= 0 && jj + dj[d] < width &&
521522
pus[(ii + di[d]) * width + jj + dj[d]] == 0)
522523
{
523-
int val = map[(ii + di[d]) * width + jj + dj[d]];
524+
T val = map[(ii + di[d]) * width + jj + dj[d]];
524525
if (val == 0)
525526
{
526527
map[(ii + di[d]) * width + jj + dj[d]] = k;
@@ -529,7 +530,7 @@ namespace cv
529530
dr++;
530531
pus[(ii + di[d]) * width + jj + dj[d]] = 1;
531532
}//this means that my point is a good point to be used in computing the final filling value
532-
else if (val > 2 && val < 250)
533+
else if (val >= 1 && val < 250)
533534
{
534535
avg += val;
535536
nr++;
@@ -540,7 +541,7 @@ namespace cv
540541
}//if hole size is smaller than a specified threshold we fill the respective hole with the average of the good neighbours
541542
if (st - speckle_size <= t)
542543
{
543-
uint8_t fillValue = (uint8_t)(avg / nr);
544+
T fillValue = (T)(avg / nr);
544545
while (speckle_size < st)
545546
{
546547
int ii = specklePointX[speckle_size];
@@ -573,18 +574,20 @@ namespace cv
573574
public:
574575
//!a median filter of 1x9 and 9x1
575576
//!1x9 median filter
577+
template<typename T>
576578
void Median1x9Filter(const Mat &originalImage, Mat &filteredImage)
577579
{
578580
CV_Assert(originalImage.rows == filteredImage.rows);
579581
CV_Assert(originalImage.cols == filteredImage.cols);
580-
parallel_for_(Range(1,originalImage.rows - 2), Median1x9(originalImage,filteredImage));
582+
parallel_for_(Range(1,originalImage.rows - 2), Median1x9<T>(originalImage,filteredImage));
581583
}
582584
//!9x1 median filter
585+
template<typename T>
583586
void Median9x1Filter(const Mat &originalImage, Mat &filteredImage)
584587
{
585588
CV_Assert(originalImage.cols == filteredImage.cols);
586589
CV_Assert(originalImage.cols == filteredImage.cols);
587-
parallel_for_(Range(1,originalImage.cols - 2), Median9x1(originalImage,filteredImage));
590+
parallel_for_(Range(1,originalImage.cols - 2), Median9x1<T>(originalImage,filteredImage));
588591
}
589592
//!constructor for the matching class
590593
//!maxDisp - represents the maximum disparity

modules/stereo/src/stereo_binary_bm.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ namespace cv
324324

325325
int width = left0.cols;
326326
int height = left0.rows;
327-
328327
if(previous_size != width * height)
329328
{
330329
previous_size = width * height;
@@ -341,6 +340,8 @@ namespace cv
341340

342341
preFilteredImg0.create(left0.size(), CV_8U);
343342
preFilteredImg1.create(left0.size(), CV_8U);
343+
344+
aux.create(height,width,CV_8UC1);
344345
}
345346

346347
Mat left = preFilteredImg0, right = preFilteredImg1;
@@ -405,12 +406,12 @@ namespace cv
405406
costGathering(hammingDistance, partialSumsLR);
406407
blockAgregation(partialSumsLR, params.agregationWindowSize, agregatedHammingLRCost);
407408
dispartyMapFormation(agregatedHammingLRCost, disp0, 3);
408-
Median1x9Filter(disp0, disp0);
409-
Median9x1Filter(disp0,disp0);
409+
Median1x9Filter<uint8_t>(disp0, aux);
410+
Median9x1Filter<uint8_t>(aux,disp0);
410411

411412
if(params.regionRemoval == CV_SPECKLE_REMOVAL_AVG_ALGORITHM)
412413
{
413-
smallRegionRemoval(disp0,params.speckleWindowSize,disp0);
414+
smallRegionRemoval<uint8_t>(disp0,params.speckleWindowSize,disp0);
414415
}
415416
else if(params.regionRemoval == CV_SPECKLE_REMOVAL_ALGORITHM)
416417
{
@@ -511,6 +512,7 @@ namespace cv
511512
Mat hammingDistance;
512513
Mat partialSumsLR;
513514
Mat agregatedHammingLRCost;
515+
Mat aux;
514516
static const char* name_;
515517
};
516518

modules/stereo/src/stereo_binary_sgbm.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -701,32 +701,20 @@ namespace cv
701701
speckleY.create(height,width,CV_32SC4);
702702
puss.create(height,width,CV_32SC4);
703703
}
704-
double minVal; double maxVal;
705-
Mat imgDisparity8U2;
706-
imgDisparity8U2.create(height,width,CV_8UC1);
707704
Mat aux;
708-
aux.create(height,width,CV_8UC1);
709-
minMaxLoc(disp, &minVal, &maxVal);
710-
disp.convertTo(imgDisparity8U2, CV_8UC1, 255 / (maxVal - minVal));
711-
Median1x9Filter(imgDisparity8U2, aux);
712-
Median9x1Filter(aux,imgDisparity8U2);
713-
smallRegionRemoval(imgDisparity8U2,params.speckleWindowSize,imgDisparity8U2);
714-
imgDisparity8U2.convertTo(disp, CV_16S);
705+
aux.create(height,width,CV_16S);
706+
Median1x9Filter<short>(disp, aux);
707+
Median9x1Filter<short>(aux,disp);
708+
smallRegionRemoval<short>(disp, params.speckleWindowSize, disp);
715709
}
716710
else if(params.regionRemoval == CV_SPECKLE_REMOVAL_ALGORITHM)
717711
{
718712
int width = left.cols;
719713
int height = left.rows;
720-
double minVal; double maxVal;
721-
Mat imgDisparity8U2;
722-
imgDisparity8U2.create(height,width,CV_8UC1);
723714
Mat aux;
724-
aux.create(height,width,CV_8UC1);
725-
minMaxLoc(disp, &minVal, &maxVal);
726-
disp.convertTo(imgDisparity8U2, CV_8UC1, 255 / (maxVal - minVal));
727-
Median1x9Filter(imgDisparity8U2, aux);
728-
Median9x1Filter(aux,imgDisparity8U2);
729-
imgDisparity8U2.convertTo(disp, CV_16S);
715+
aux.create(height,width,CV_16S);
716+
Median1x9Filter<short>(disp, aux);
717+
Median9x1Filter<short>(aux,disp);
730718
if( params.speckleWindowSize > 0 )
731719
filterSpeckles(disp, (params.minDisparity - 1) * StereoMatcher::DISP_SCALE, params.speckleWindowSize,
732720
StereoMatcher::DISP_SCALE * params.speckleRange, buffer);

0 commit comments

Comments
 (0)