Noize Reduce

This commit is contained in:
Andrey Tkachenko 2014-06-09 19:16:48 +04:00
parent bb59ba0d7e
commit 5045cea5f9
3 changed files with 65 additions and 40 deletions

BIN
left8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 KiB

BIN
right8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 275 KiB

View File

@ -21,7 +21,7 @@ typedef struct {
} StereoData;
#define WINDOW_START 0
#define WINDOW_SIZE 78
#define WINDOW_SIZE 70
#define MATCH_SIZE 3
#define ERROR_LEVEL 1
@ -29,11 +29,36 @@ int getPixelValue(Mat* data, int x, int y)
{
uchar* pixel = data->ptr(y, x);
return pixel[0];
return pixel[0] + pixel[1] + pixel[2];
}
void normalize(Mat* mat, int x_from, int x_to, int y)
{
int n = x_to - x_from, val;
int sum_xy = 0, sum_x = 0, sum_y = 0, sum_x2 = 0;
float a, b;
int tmp;
uchar* data;
for (int i = x_from; i < x_to; i++) {
val = getPixelValue(mat, i, y);
sum_xy += i * val;
sum_x += i;
sum_y += val;
sum_x2 += i * i;
}
a = 0; //((n * sum_xy) - (sum_x * sum_y)) / ((n * sum_x2) - (sum_x * sum_x));
b = (sum_y - a * sum_x) / n;
for (int i = x_from; i < x_to; i++) {
data = mat->ptr<uchar>(y, i);
//data[0] = a * i ;
}
}
int match(StereoData &params, int x, int y, int j) {
int error = 0;
int error = 0, val;
for (int i = 0; i < MATCH_SIZE; i++) {
if (x + j + i >= params.right->cols) {
return 999;
@ -43,7 +68,9 @@ int match(StereoData &params, int x, int y, int j) {
if (y + g >= params.right->rows) {
return 999;
}
error += abs(getPixelValue(params.left, x + i, y + g) - getPixelValue(params.right, x + j + i, y + g));
val = abs(getPixelValue(params.left, x + i, y + g) - getPixelValue(params.right, x + j + i, y + g));
error += val;
}
}
@ -86,25 +113,24 @@ void putPixel(Mat* mat, int x, int y, int cursor)
void calcDepthMapMy2(StereoData &params) {
int x, y, cursor, cursor2, closest;
int minErrorValue, tmp, minLevel = 0, tmpSmoothed;
int rightLevel = 0, matched;
int x, y, cursor, closest, tmp;
int minErrorValue, tmpSmoothed, matched;
for (y = 0; y < params.stereo->rows; y++) {
cursor = 0; cursor2 = 0;rightLevel=0;
cursor = 0;
for (x = 0; x < params.stereo->cols; x++) {
closest = 999; minErrorValue = 999;matched=0;
for (int i = 0; i < WINDOW_SIZE; i++) {
for (int i = WINDOW_START; i < WINDOW_SIZE; i++) {
tmp = match(params, x, y, i);
tmpSmoothed = tmp;
tmpSmoothed = tmp + (1 * abs(cursor - i));
if (tmp < minErrorValue) {
minErrorValue = tmp;
closest = i;
if (tmpSmoothed < minErrorValue) {
minErrorValue = tmpSmoothed;
matched = 1;
} else if (tmp == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) {
closest = i ;
} else if (tmpSmoothed == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) {
closest = i;
matched++;
}
}
@ -112,50 +138,50 @@ void calcDepthMapMy2(StereoData &params) {
if (matched) {
cursor = closest;
putPixel(params.stereo, x, y, cursor);
putPixel(params.stereo, x + cursor, y, cursor);
}
}
}
/*for (y = 0; y < params.stereo->rows; y++) {
cursor = 0;
for (x = 0; x < params.stereo->cols; x++) {
/*
closest = 999;minErrorValue = 999;
closest = 999;minErrorValue = 999;matched=0;
for (int i = WINDOW_START; i < WINDOW_SIZE; i++) {
tmp = rmatch(params, params.stereo->cols - x, y, i) ;
if (tmp < minErrorValue) {
minErrorValue = tmp;
closest = i;
}
tmp = rmatch(params, params.stereo->cols - x, y, i);
tmpSmoothed = tmp + (1 * abs(cursor - i));
if (tmp == minErrorValue) {
if (abs(cursor2 - i) < abs(cursor2 - closest)) {
if (tmpSmoothed < minErrorValue) {
minErrorValue = tmpSmoothed;
matched = 1;
closest = i ;
} else if (tmpSmoothed == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) {
closest = i;
matched++;
}
}
}
if (closest != 999) {
if (cursor2 != closest) {
cursor2 = closest;
if (matched) {
cursor = closest;
putPixel(params.stereo, params.stereo->cols - x - cursor - 1, y, cursor);
}
}
putPixel(params.stereo, params.stereo->cols - x - cursor2, y, cursor2);
}*/
}
}
}
int main(int argc, char** argv) {
StereoData params;
Mat left = imread("left2.png", 1);
Mat right = imread("right2.png", 1);
cvtColor(left, left, CV_BGR2GRAY);
cvtColor(right, right, CV_BGR2GRAY);
Mat left = imread("left6.png", 1);
Mat right = imread("right6.png", 1);
Mat stereo(Mat::zeros(left.rows, left.cols, CV_8U));
Mat visited(Mat::zeros(left.rows, left.cols, CV_8U));
params.visited = &visited;
params.left = &left;
params.right = &right;
params.stereo = &stereo;
@ -164,7 +190,6 @@ int main(int argc, char** argv) {
calcDepthMapMy2(params);
imshow("Display Image Dyn Progr", stereo);
//imshow("Display Image Dyn Progr1", visited);
waitKey();
return 0;