Create Threshold

This commit is contained in:
Andrey Tkachenko 2014-06-10 19:07:43 +04:00
parent 5045cea5f9
commit 695aff67a2

View File

@ -17,19 +17,19 @@ typedef struct {
Mat* left; Mat* left;
Mat* right; Mat* right;
Mat* stereo; Mat* stereo;
Mat* visited;
} StereoData; } StereoData;
#define WINDOW_START 0 #define WINDOW_START 0
#define WINDOW_SIZE 70 #define WINDOW_SIZE 150
#define MATCH_SIZE 3 #define MATCH_SIZE 2
#define ERROR_LEVEL 1 #define ERROR_LEVEL 1
int getPixelValue(Mat* data, int x, int y) int getPixelValue(Mat* data, int x, int y)
{ {
uchar* pixel = data->ptr(y, x); uchar* pixel = data->ptr(y, x);
uchar value = 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2];
return pixel[0] + pixel[1] + pixel[2]; return value;
} }
void normalize(Mat* mat, int x_from, int x_to, int y) void normalize(Mat* mat, int x_from, int x_to, int y)
@ -58,7 +58,7 @@ void normalize(Mat* mat, int x_from, int x_to, int y)
} }
int match(StereoData &params, int x, int y, int j) { int match(StereoData &params, int x, int y, int j) {
int error = 0, val; int error = 0;
for (int i = 0; i < MATCH_SIZE; i++) { for (int i = 0; i < MATCH_SIZE; i++) {
if (x + j + i >= params.right->cols) { if (x + j + i >= params.right->cols) {
return 999; return 999;
@ -68,9 +68,8 @@ int match(StereoData &params, int x, int y, int j) {
if (y + g >= params.right->rows) { if (y + g >= params.right->rows) {
return 999; return 999;
} }
val = abs(getPixelValue(params.left, x + i, y + g) - getPixelValue(params.right, x + j + i, y + g));
error += val; error += abs(getPixelValue(params.left, x + i, y + g) - getPixelValue(params.right, x + j + i, y + g));
} }
} }
@ -99,7 +98,7 @@ int rmatch(StereoData &params, int x, int y, int j) {
int getPixelColor(int cursor) int getPixelColor(int cursor)
{ {
float val = ((1.0 + cursor) / WINDOW_SIZE); float val = ((1.0 + cursor) / WINDOW_SIZE);
return (val * val * val) * 255.0; return (val * val) * 255.0;
} }
void putPixel(Mat* mat, int x, int y, int cursor) void putPixel(Mat* mat, int x, int y, int cursor)
@ -111,23 +110,24 @@ void putPixel(Mat* mat, int x, int y, int cursor)
data[0] = getPixelColor(cursor); data[0] = getPixelColor(cursor);
} }
void calcDepthMapMy2(StereoData &params) { void calcDepthMapMy2(StereoData &params) {
int x, y, cursor, closest, tmp; int x, y, cursor, closest, tmp;
int minErrorValue, tmpSmoothed, matched; int minErrorValue, tmpSmoothed, matched;
int precursor;
for (y = 0; y < params.stereo->rows; y++) { for (y = 0; y < params.stereo->rows; y++) {
cursor = 0; cursor = 25;precursor=25;
for (x = 0; x < params.stereo->cols; x++) { for (x = 0; x < params.stereo->cols; x++) {
closest = 999; minErrorValue = 999;matched=0; closest = 999; minErrorValue = 999;matched=0;
minErrorValue=999;
for (int i = WINDOW_START; i < WINDOW_SIZE; i++) { for (int i = WINDOW_START; i < WINDOW_SIZE; i++) {
tmp = match(params, x, y, i); tmp = match(params, x, y, i);
tmpSmoothed = tmp + (1 * abs(cursor - i)); tmpSmoothed = tmp + (0.2 * abs(cursor - i));
if (tmpSmoothed < minErrorValue) { if (tmpSmoothed < minErrorValue) {
minErrorValue = tmpSmoothed; minErrorValue = tmpSmoothed;
matched = 1;
closest = i; closest = i;
matched = 1;
} else if (tmpSmoothed == minErrorValue) { } else if (tmpSmoothed == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) { if (abs(cursor - i) < abs(cursor - closest)) {
closest = i; closest = i;
@ -138,8 +138,17 @@ void calcDepthMapMy2(StereoData &params) {
if (matched) { if (matched) {
cursor = closest; cursor = closest;
putPixel(params.stereo, x + cursor, y, cursor);
} }
if (cursor - precursor > 0) {
for (int i = precursor; i < cursor; i++) {
putPixel(params.stereo, x + i, y, 0);
}
}
putPixel(params.stereo, x + cursor, y, cursor);
precursor=cursor;
} }
} }
@ -167,18 +176,22 @@ void calcDepthMapMy2(StereoData &params) {
if (matched) { if (matched) {
cursor = closest; cursor = closest;
putPixel(params.stereo, params.stereo->cols - x - cursor - 1, y, cursor);
if (minErrorValue < 50) {
putPixel(params.stereo, params.stereo->cols - x, y, 0);
}
} }
} }
}*/ }*/
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
StereoData params; StereoData params;
Mat left = imread("left6.png", 1); Mat left = imread("left4.png", 1);
Mat right = imread("right6.png", 1); Mat right = imread("right4.png", 1);
Mat stereo(Mat::zeros(left.rows, left.cols, CV_8U)); Mat stereo(Mat::zeros(left.rows, left.cols, CV_8U));