Commit 12b530c6 authored by Muresan Mircea Paul's avatar Muresan Mircea Paul

modified stereo for better results

parent 1ae4d2a8
...@@ -281,20 +281,20 @@ namespace cv ...@@ -281,20 +281,20 @@ namespace cv
} }
}; };
//!median 1x9 paralelized filter //!median 1x9 paralelized filter
template <typename T>
class Median1x9:public ParallelLoopBody class Median1x9:public ParallelLoopBody
{ {
private: private:
uint8_t *original; T *original;
uint8_t *filtered; T *filtered;
int height, width,_stride; int height, width;
public: public:
Median1x9(const Mat &originalImage, Mat &filteredImage) Median1x9(const Mat &originalImage, Mat &filteredImage)
{ {
original = originalImage.data; original = (T *)originalImage.data;
filtered = filteredImage.data; filtered = (T *)filteredImage.data;
height = originalImage.rows; height = originalImage.rows;
width = originalImage.cols; width = originalImage.cols;
_stride = (int)originalImage.step;
} }
void operator()(const cv::Range &r) const{ void operator()(const cv::Range &r) const{
for (int m = r.start; m <= r.end; m++) for (int m = r.start; m <= r.end; m++)
...@@ -302,39 +302,39 @@ namespace cv ...@@ -302,39 +302,39 @@ namespace cv
for (int n = 4; n < width - 4; ++n) for (int n = 4; n < width - 4; ++n)
{ {
int k = 0; int k = 0;
uint8_t window[9]; T window[9];
for (int i = n - 4; i <= n + 4; ++i) for (int i = n - 4; i <= n + 4; ++i)
window[k++] = original[m * _stride + i]; window[k++] = original[m * width + i];
for (int j = 0; j < 5; ++j) for (int j = 0; j < 5; ++j)
{ {
int min = j; int min = j;
for (int l = j + 1; l < 9; ++l) for (int l = j + 1; l < 9; ++l)
if (window[l] < window[min]) if (window[l] < window[min])
min = l; min = l;
const uint8_t temp = window[j]; const T temp = window[j];
window[j] = window[min]; window[j] = window[min];
window[min] = temp; window[min] = temp;
} }
filtered[m * _stride + n] = window[4]; filtered[m * width + n] = window[4];
} }
} }
} }
}; };
//!median 9x1 paralelized filter //!median 9x1 paralelized filter
template <typename T>
class Median9x1:public ParallelLoopBody class Median9x1:public ParallelLoopBody
{ {
private: private:
uint8_t *original; T *original;
uint8_t *filtered; T *filtered;
int height, width, _stride; int height, width;
public: public:
Median9x1(const Mat &originalImage, Mat &filteredImage) Median9x1(const Mat &originalImage, Mat &filteredImage)
{ {
original = originalImage.data; original = (T *)originalImage.data;
filtered = filteredImage.data; filtered = (T *)filteredImage.data;
height = originalImage.rows; height = originalImage.rows;
width = originalImage.cols; width = originalImage.cols;
_stride = (int)originalImage.step;
} }
void operator()(const Range &r) const{ void operator()(const Range &r) const{
for (int n = r.start; n <= r.end; ++n) for (int n = r.start; n <= r.end; ++n)
...@@ -342,20 +342,20 @@ namespace cv ...@@ -342,20 +342,20 @@ namespace cv
for (int m = 4; m < height - 4; ++m) for (int m = 4; m < height - 4; ++m)
{ {
int k = 0; int k = 0;
uint8_t window[9]; T window[9];
for (int i = m - 4; i <= m + 4; ++i) for (int i = m - 4; i <= m + 4; ++i)
window[k++] = original[i * _stride + n]; window[k++] = original[i * width + n];
for (int j = 0; j < 5; j++) for (int j = 0; j < 5; j++)
{ {
int min = j; int min = j;
for (int l = j + 1; l < 9; ++l) for (int l = j + 1; l < 9; ++l)
if (window[l] < window[min]) if (window[l] < window[min])
min = l; min = l;
const uint8_t temp = window[j]; const T temp = window[j];
window[j] = window[min]; window[j] = window[min];
window[min] = temp; window[min] = temp;
} }
filtered[m * _stride + n] = window[4]; filtered[m * width + n] = window[4];
} }
} }
} }
...@@ -471,6 +471,7 @@ namespace cv ...@@ -471,6 +471,7 @@ namespace cv
parallel_for_(cv::Range(win + 1,height - win - 1), agregateCost(partialSums,windowSize,maxDisp,cost)); parallel_for_(cv::Range(win + 1,height - win - 1), agregateCost(partialSums,windowSize,maxDisp,cost));
} }
//!remove small regions that have an area smaller than t, we fill the region with the average of the good pixels around it //!remove small regions that have an area smaller than t, we fill the region with the average of the good pixels around it
template <typename T>
void smallRegionRemoval(const Mat &currentMap, int t, Mat &out) void smallRegionRemoval(const Mat &currentMap, int t, Mat &out)
{ {
CV_Assert(currentMap.cols == out.cols); CV_Assert(currentMap.cols == out.cols);
...@@ -480,11 +481,11 @@ namespace cv ...@@ -480,11 +481,11 @@ namespace cv
int *specklePointX = (int *)speckleX.data; int *specklePointX = (int *)speckleX.data;
int *specklePointY = (int *)speckleY.data; int *specklePointY = (int *)speckleY.data;
memset(pus, 0, previous_size * sizeof(pus[0])); memset(pus, 0, previous_size * sizeof(pus[0]));
uint8_t *map = currentMap.data; T *map = (T *)currentMap.data;
uint8_t *outputMap = out.data; T *outputMap = (T *)out.data;
int height = currentMap.rows; int height = currentMap.rows;
int width = currentMap.cols; int width = currentMap.cols;
uint8_t k = 1; T k = 1;
int st, dr; int st, dr;
int di[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, int di[] = { -1, -1, -1, 0, 1, 1, 1, 0 },
dj[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; dj[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
...@@ -502,8 +503,8 @@ namespace cv ...@@ -502,8 +503,8 @@ namespace cv
} }
else if (map[iw + j] == 0) else if (map[iw + j] == 0)
{ {
int nr = 1; T nr = 1;
int avg = 0; T avg = 0;
speckle_size = dr; speckle_size = dr;
specklePointX[dr] = i; specklePointX[dr] = i;
specklePointY[dr] = j; specklePointY[dr] = j;
...@@ -520,7 +521,7 @@ namespace cv ...@@ -520,7 +521,7 @@ namespace cv
if (ii + di[d] >= 0 && ii + di[d] < height && jj + dj[d] >= 0 && jj + dj[d] < width && if (ii + di[d] >= 0 && ii + di[d] < height && jj + dj[d] >= 0 && jj + dj[d] < width &&
pus[(ii + di[d]) * width + jj + dj[d]] == 0) pus[(ii + di[d]) * width + jj + dj[d]] == 0)
{ {
int val = map[(ii + di[d]) * width + jj + dj[d]]; T val = map[(ii + di[d]) * width + jj + dj[d]];
if (val == 0) if (val == 0)
{ {
map[(ii + di[d]) * width + jj + dj[d]] = k; map[(ii + di[d]) * width + jj + dj[d]] = k;
...@@ -529,7 +530,7 @@ namespace cv ...@@ -529,7 +530,7 @@ namespace cv
dr++; dr++;
pus[(ii + di[d]) * width + jj + dj[d]] = 1; pus[(ii + di[d]) * width + jj + dj[d]] = 1;
}//this means that my point is a good point to be used in computing the final filling value }//this means that my point is a good point to be used in computing the final filling value
else if (val > 2 && val < 250) else if (val >= 1 && val < 250)
{ {
avg += val; avg += val;
nr++; nr++;
...@@ -540,7 +541,7 @@ namespace cv ...@@ -540,7 +541,7 @@ namespace cv
}//if hole size is smaller than a specified threshold we fill the respective hole with the average of the good neighbours }//if hole size is smaller than a specified threshold we fill the respective hole with the average of the good neighbours
if (st - speckle_size <= t) if (st - speckle_size <= t)
{ {
uint8_t fillValue = (uint8_t)(avg / nr); T fillValue = (T)(avg / nr);
while (speckle_size < st) while (speckle_size < st)
{ {
int ii = specklePointX[speckle_size]; int ii = specklePointX[speckle_size];
...@@ -573,18 +574,20 @@ namespace cv ...@@ -573,18 +574,20 @@ namespace cv
public: public:
//!a median filter of 1x9 and 9x1 //!a median filter of 1x9 and 9x1
//!1x9 median filter //!1x9 median filter
template<typename T>
void Median1x9Filter(const Mat &originalImage, Mat &filteredImage) void Median1x9Filter(const Mat &originalImage, Mat &filteredImage)
{ {
CV_Assert(originalImage.rows == filteredImage.rows); CV_Assert(originalImage.rows == filteredImage.rows);
CV_Assert(originalImage.cols == filteredImage.cols); CV_Assert(originalImage.cols == filteredImage.cols);
parallel_for_(Range(1,originalImage.rows - 2), Median1x9(originalImage,filteredImage)); parallel_for_(Range(1,originalImage.rows - 2), Median1x9<T>(originalImage,filteredImage));
} }
//!9x1 median filter //!9x1 median filter
template<typename T>
void Median9x1Filter(const Mat &originalImage, Mat &filteredImage) void Median9x1Filter(const Mat &originalImage, Mat &filteredImage)
{ {
CV_Assert(originalImage.cols == filteredImage.cols); CV_Assert(originalImage.cols == filteredImage.cols);
CV_Assert(originalImage.cols == filteredImage.cols); CV_Assert(originalImage.cols == filteredImage.cols);
parallel_for_(Range(1,originalImage.cols - 2), Median9x1(originalImage,filteredImage)); parallel_for_(Range(1,originalImage.cols - 2), Median9x1<T>(originalImage,filteredImage));
} }
//!constructor for the matching class //!constructor for the matching class
//!maxDisp - represents the maximum disparity //!maxDisp - represents the maximum disparity
......
...@@ -324,7 +324,6 @@ namespace cv ...@@ -324,7 +324,6 @@ namespace cv
int width = left0.cols; int width = left0.cols;
int height = left0.rows; int height = left0.rows;
if(previous_size != width * height) if(previous_size != width * height)
{ {
previous_size = width * height; previous_size = width * height;
...@@ -341,6 +340,8 @@ namespace cv ...@@ -341,6 +340,8 @@ namespace cv
preFilteredImg0.create(left0.size(), CV_8U); preFilteredImg0.create(left0.size(), CV_8U);
preFilteredImg1.create(left0.size(), CV_8U); preFilteredImg1.create(left0.size(), CV_8U);
aux.create(height,width,CV_8UC1);
} }
Mat left = preFilteredImg0, right = preFilteredImg1; Mat left = preFilteredImg0, right = preFilteredImg1;
...@@ -405,12 +406,12 @@ namespace cv ...@@ -405,12 +406,12 @@ namespace cv
costGathering(hammingDistance, partialSumsLR); costGathering(hammingDistance, partialSumsLR);
blockAgregation(partialSumsLR, params.agregationWindowSize, agregatedHammingLRCost); blockAgregation(partialSumsLR, params.agregationWindowSize, agregatedHammingLRCost);
dispartyMapFormation(agregatedHammingLRCost, disp0, 3); dispartyMapFormation(agregatedHammingLRCost, disp0, 3);
Median1x9Filter(disp0, disp0); Median1x9Filter<uint8_t>(disp0, aux);
Median9x1Filter(disp0,disp0); Median9x1Filter<uint8_t>(aux,disp0);
if(params.regionRemoval == CV_SPECKLE_REMOVAL_AVG_ALGORITHM) if(params.regionRemoval == CV_SPECKLE_REMOVAL_AVG_ALGORITHM)
{ {
smallRegionRemoval(disp0,params.speckleWindowSize,disp0); smallRegionRemoval<uint8_t>(disp0,params.speckleWindowSize,disp0);
} }
else if(params.regionRemoval == CV_SPECKLE_REMOVAL_ALGORITHM) else if(params.regionRemoval == CV_SPECKLE_REMOVAL_ALGORITHM)
{ {
...@@ -511,6 +512,7 @@ namespace cv ...@@ -511,6 +512,7 @@ namespace cv
Mat hammingDistance; Mat hammingDistance;
Mat partialSumsLR; Mat partialSumsLR;
Mat agregatedHammingLRCost; Mat agregatedHammingLRCost;
Mat aux;
static const char* name_; static const char* name_;
}; };
......
...@@ -701,32 +701,20 @@ namespace cv ...@@ -701,32 +701,20 @@ namespace cv
speckleY.create(height,width,CV_32SC4); speckleY.create(height,width,CV_32SC4);
puss.create(height,width,CV_32SC4); puss.create(height,width,CV_32SC4);
} }
double minVal; double maxVal;
Mat imgDisparity8U2;
imgDisparity8U2.create(height,width,CV_8UC1);
Mat aux; Mat aux;
aux.create(height,width,CV_8UC1); aux.create(height,width,CV_16S);
minMaxLoc(disp, &minVal, &maxVal); Median1x9Filter<short>(disp, aux);
disp.convertTo(imgDisparity8U2, CV_8UC1, 255 / (maxVal - minVal)); Median9x1Filter<short>(aux,disp);
Median1x9Filter(imgDisparity8U2, aux); smallRegionRemoval<short>(disp, params.speckleWindowSize, disp);
Median9x1Filter(aux,imgDisparity8U2);
smallRegionRemoval(imgDisparity8U2,params.speckleWindowSize,imgDisparity8U2);
imgDisparity8U2.convertTo(disp, CV_16S);
} }
else if(params.regionRemoval == CV_SPECKLE_REMOVAL_ALGORITHM) else if(params.regionRemoval == CV_SPECKLE_REMOVAL_ALGORITHM)
{ {
int width = left.cols; int width = left.cols;
int height = left.rows; int height = left.rows;
double minVal; double maxVal;
Mat imgDisparity8U2;
imgDisparity8U2.create(height,width,CV_8UC1);
Mat aux; Mat aux;
aux.create(height,width,CV_8UC1); aux.create(height,width,CV_16S);
minMaxLoc(disp, &minVal, &maxVal); Median1x9Filter<short>(disp, aux);
disp.convertTo(imgDisparity8U2, CV_8UC1, 255 / (maxVal - minVal)); Median9x1Filter<short>(aux,disp);
Median1x9Filter(imgDisparity8U2, aux);
Median9x1Filter(aux,imgDisparity8U2);
imgDisparity8U2.convertTo(disp, CV_16S);
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);
......
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