From 5d2355cf1812611c370093f50af8e216cd508b7a Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Mon, 7 Jul 2014 11:00:17 +0400 Subject: [PATCH] Refactoring --- src/SvImage.cpp | 56 ++++++++++ src/SvImage.h | 32 ++++++ src/SvProcessorV1.cpp | 116 ++++++++++++++++++++ src/SvProcessorV1.h | 37 +++++++ src/SvProcessorV2.cpp | 51 +++++++++ src/SvProcessorV2.h | 36 +++++++ src/Test.cpp | 241 ------------------------------------------ src/dyn_prog.c | 120 --------------------- src/hsv.c | 94 ---------------- src/main.cpp | 31 ++++++ src/my.c | 0 11 files changed, 359 insertions(+), 455 deletions(-) create mode 100644 src/SvImage.cpp create mode 100644 src/SvImage.h create mode 100644 src/SvProcessorV1.cpp create mode 100644 src/SvProcessorV1.h create mode 100644 src/SvProcessorV2.cpp create mode 100644 src/SvProcessorV2.h delete mode 100644 src/Test.cpp delete mode 100644 src/dyn_prog.c delete mode 100644 src/hsv.c create mode 100644 src/main.cpp delete mode 100644 src/my.c diff --git a/src/SvImage.cpp b/src/SvImage.cpp new file mode 100644 index 0000000..858b360 --- /dev/null +++ b/src/SvImage.cpp @@ -0,0 +1,56 @@ +/* + * SvImage.cpp + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#include "SvImage.h" + +int SvImage::getPixel(int x, int y, int channel = -1) { + if (y >= this->image->rows || y < 0 || x >= this->image->cols || x < 0) { + return 0; + } + + uchar* pixel = this->image->ptr(y, x); + uchar value; + + if (channel == -1 || channel > 2) { + value = 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2]; + } else { + value = pixel[channel]; + } + + return value; +} + +void SvImage::putPixel(int x, int y, int value) +{ + uchar* data; + + if (x >= 0 && x < this->image->cols && y >= 0 && y < this->image->rows) { + data = this->image->ptr(y, x); + data[0] = value; + } +} + +int SvImage::getHeight() { + return this->image->rows; +} + +int SvImage::getWidth() { + return this->image->cols; +} + +Mat& SvImage::getCvMatrix() { + return *this->image; +} + +SvImage::SvImage(Mat& image) { + this->image = ℑ +} + +SvImage::~SvImage() { + +} + diff --git a/src/SvImage.h b/src/SvImage.h new file mode 100644 index 0000000..4516488 --- /dev/null +++ b/src/SvImage.h @@ -0,0 +1,32 @@ +/* + * SvImage.h + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#ifndef SVIMAGE_H_ +#define SVIMAGE_H_ + +#include + +using namespace cv; + +class SvImage { + +protected: + Mat* image; + +public: + int getPixel(int x, int y, int channel); + void putPixel(int x, int y, int val); + + int getHeight(); + int getWidth(); + Mat& getCvMatrix(); + + SvImage(Mat& image); + virtual ~SvImage(); +}; + +#endif /* SVIMAGE_H_ */ diff --git a/src/SvProcessorV1.cpp b/src/SvProcessorV1.cpp new file mode 100644 index 0000000..4067f37 --- /dev/null +++ b/src/SvProcessorV1.cpp @@ -0,0 +1,116 @@ +/* + * SvProcessor.cpp + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#include "SvProcessorV1.h" + +SvProcessorV1::SvProcessorV1(SvImage& left, SvImage& right) { + this->left = &left; + this->right = &right; +} + +int SvProcessorV1::getPixelColor(int cursor) { + float val = ((1.0 + cursor) / this->windowSize); + return (val * val) * 255.0; +} +/* +int SvProcessorV1::match(int x, int y, int j) { + int error = 0; + + for (int i = 0; i <= 2; i++) { + for (int g = 0; g <= 3; g++) { + error += diff(x + i, y + g, x + i + j, y + g); + } + } + + return error; +} +*/ +int SvProcessorV1::diff(int lx, int ly, int rx, int ry) { + int Rvalue, Gvalue, Bvalue; + + Rvalue = abs(this->left->getPixel(lx, ly, 0) - this->right->getPixel(rx, ry, 0)); + Gvalue = abs(this->left->getPixel(lx, ly, 1) - this->right->getPixel(rx, ry, 1)); + Bvalue = abs(this->left->getPixel(lx, ly, 2) - this->right->getPixel(rx, ry, 2)); + + return (Gvalue + Rvalue + Bvalue) + + ((abs(Gvalue - Rvalue) + abs(Bvalue - Gvalue)) * 4); +} + +int SvProcessorV1::match(int x, int y, int j) { + int error = 0; + int ms = 3; + int c = diff(x, y, x + j, y), + l = 0, r = 0, t = 0, b = 0; + + error = c; + + for (int i = 1; i <= ms; i++) { + l += diff(x - i, y, x + j - i, y); + r += diff(x + i, y, x + j + i, y); + t += diff(x, y - i, x + j, y - i); + b += diff(x, y + i, x + j, y + i); + } + + error += l > r ? r : l; + error += t > b ? b : t; + //error += l + r+b+t; + + return error; +} + +void SvProcessorV1::run(SvImage& stereo, ImageType base = LEFT) { + int x, y, cursor, closest, tmp; + int minErrorValue, tmpSmoothed, matched; + int precursor, preprecursor; + + for (y = 0; y < stereo.getHeight(); y++) { + cursor = 0; + precursor = 0; + preprecursor = 0; + for (x = 0; x < stereo.getWidth(); x++) { + closest = -1; minErrorValue = -1;matched=0; + minErrorValue=-1; + + for (int i = 1; i < this->windowSize; i++) { + tmp = this->match(x, y, i); + tmpSmoothed = (tmp) + (abs(cursor - i) * 5); + /*if (tmpSmoothed > 150 ) { + continue; + }*/ + + if (tmpSmoothed < minErrorValue || minErrorValue == -1) { + minErrorValue = tmpSmoothed; + closest = i; + matched = 1; + } else if (tmpSmoothed == minErrorValue) { + if (abs(cursor - i) < abs(cursor - closest)) { + closest = i; + matched++; + } + } + } + + if (matched == 1) { + cursor = closest; + } + + //stereo.putPixel(x, y, minErrorValue/20); + + if (cursor != precursor) { + stereo.putPixel(x, y, this->getPixelColor(cursor)); + } + + preprecursor = precursor; + precursor = cursor; + } + } +} + +SvProcessorV1::~SvProcessorV1() { + +} + diff --git a/src/SvProcessorV1.h b/src/SvProcessorV1.h new file mode 100644 index 0000000..f32ad08 --- /dev/null +++ b/src/SvProcessorV1.h @@ -0,0 +1,37 @@ +/* + * SvProcessor.h + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#ifndef SVPROCESSORV1_H_ +#define SVPROCESSORV1_H_ + +#include +#include "SvImage.h" + +using namespace std; + +class SvProcessorV1 { + +protected: + SvImage* left; + SvImage* right; + + const int windowSize = 90; + +public: + enum ImageType { + LEFT, RIGHT + }; + int diff(int lx, int ly, int rx, int ry); + int match(int x, int y, int i); + void run(SvImage& stereo, ImageType base); + int getPixelColor(int cursor); + + SvProcessorV1(SvImage& left, SvImage& right); + virtual ~SvProcessorV1(); +}; + +#endif /* SVPROCESSORV1_H_ */ diff --git a/src/SvProcessorV2.cpp b/src/SvProcessorV2.cpp new file mode 100644 index 0000000..a94db5b --- /dev/null +++ b/src/SvProcessorV2.cpp @@ -0,0 +1,51 @@ +/* + * SvProcessor.cpp + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#include "SvProcessorV2.h" + +SvProcessorV2::SvProcessorV2(SvImage& left, SvImage& right) { + this->left = &left; + this->right = &right; + this->lineLeft = new std::vector(); + this->lineRight = new std::vector(); +} + +void SvProcessorV2::scanLine(int pos, ImageType type) { + SvImage* image; + std::vector* line; + + if (type == LEFT) { + image = this->left; + line = this->lineLeft; + } else { + image = this->right; + line = this->lineRight; + } +} + +void SvProcessorV2::run(SvImage& stereo, ImageType base = LEFT) { + int x, y, cl, pv, ppv; + int flag = 0, tmp; + + for (y = 0; y < stereo.getHeight(); y++) { + flag = false; + for (x = 0; x < stereo.getWidth(); x++) { + cl = (this->left->getPixel(x, y, -1)); + tmp = abs((pv - ppv) + (cl - pv)) > 10; + stereo.putPixel(x, y, abs((pv - ppv) + (cl - pv))); + + ppv = pv; + pv = cl; + } + } +} + +SvProcessorV2::~SvProcessorV2() { + delete this->lineLeft; + delete this->lineRight; +} + diff --git a/src/SvProcessorV2.h b/src/SvProcessorV2.h new file mode 100644 index 0000000..52fb0ef --- /dev/null +++ b/src/SvProcessorV2.h @@ -0,0 +1,36 @@ +/* + * SvProcessor.h + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#ifndef SVPROCESSOR_H_ +#define SVPROCESSOR_H_ + +#include +#include "SvImage.h" + +using namespace std; + +class SvProcessorV2 { +protected: + SvImage* left; + SvImage* right; + std::vector* lineLeft; + std::vector* lineRight; + +public: + enum ImageType { + LEFT, RIGHT + }; + + void scanLine(int pos, ImageType type); + + void run(SvImage& stereo, ImageType base); + + SvProcessorV2(SvImage& left, SvImage& right); + virtual ~SvProcessorV2(); +}; + +#endif /* SVPROCESSOR_H_ */ diff --git a/src/Test.cpp b/src/Test.cpp deleted file mode 100644 index aa68d23..0000000 --- a/src/Test.cpp +++ /dev/null @@ -1,241 +0,0 @@ -//============================================================================ -// Name : Test.cpp -// Author : -// Version : -// Copyright : Your copyright notice -// Description : Hello World in C++, Ansi-style -//============================================================================ - -#include -#include -#include - -using namespace std; -using namespace cv; - -typedef struct { - Mat* left; - Mat* right; - Mat* stereo; -} StereoData; - -#define WINDOW_START 0 -#define WINDOW_SIZE 80 -#define MATCH_SIZE_X 2 -#define MATCH_SIZE_Y 2 -#define ERROR_LEVEL 1 -#define Y_CORRECTION 0 -#define COLOR_SCALE 1 - -int getPixelValue(Mat* data, int x, int y, int offset = -1) -{ - if (y >= data->rows || y < 0 || x >= data->cols || x < 0) { - return 0; - } - - uchar* pixel = data->ptr(y, x); - uchar value; - - if (offset == -1 || offset > 2) { - value = 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2]; - } else { - value = pixel[offset]; - } - - return value; -} - -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(y, i); - //data[0] = a * i ; - } -} - -int match(StereoData ¶ms, int x, int y, int j) { - int error = 0; - int Rvalue, Gvalue, Bvalue; - - for (int i = 0; i < MATCH_SIZE_X; i++) { - if (x + j + i >= params.right->cols) { - return 999; - } - - for (int g = 0; g < MATCH_SIZE_Y; g++) { - if (y + g >= params.right->rows) { - return 999; - } - - Rvalue = abs(getPixelValue(params.left, x + i, y + g, 0) - getPixelValue(params.right, x + j + i, y + g, 0)); - Gvalue = abs(getPixelValue(params.left, x + i, y + g, 1) - getPixelValue(params.right, x + j + i, y + g, 1)); - Bvalue = abs(getPixelValue(params.left, x + i, y + g, 2) - getPixelValue(params.right, x + j + i, y + g, 2)); - - error += Rvalue * COLOR_SCALE; - error += Gvalue * COLOR_SCALE; - error += Bvalue * COLOR_SCALE; - } - } - - return error; -} - -int rmatch(StereoData ¶ms, int x, int y, int j) { - int error = 0; - - for (int i = 0; i < MATCH_SIZE_X; i++) { - if (x - j - i < 0) { - return 999; - } - - for (int g = 0; g < MATCH_SIZE_Y; g++) { - if (y + g >= params.left->rows) { - return 999; - } - - error += abs(getPixelValue(params.right, x - i, y + g, 0) - getPixelValue(params.left, x - j - i, y + g, 0)); - error += abs(getPixelValue(params.right, x - i, y + g, 1) - getPixelValue(params.left, x - j - i, y + g, 1)); - error += abs(getPixelValue(params.right, x - i, y + g, 2) - getPixelValue(params.left, x - j - i, y + g, 2)); - } - } - - return error; -} - -int getPixelColor(int cursor) -{ - float val = ((1.0 + cursor) / WINDOW_SIZE); - return (val * val) * 255.0; -} - -void putPixel(Mat* mat, int x, int y, int cursor) -{ - uchar* data; - - if (x >= 0 && x < mat->cols && y >= 0 && y < mat->rows) { - data = mat->ptr(y, x); - data[0] = getPixelColor(cursor); - } -} - -void calcDepthMapMy2(StereoData ¶ms) { - int x, y, cursor, closest, tmp; - int minErrorValue, tmpSmoothed, matched; - int precursor, preprecursor; - - for (y = 0; y < params.stereo->rows; y++) { - cursor = WINDOW_SIZE/2; - precursor = WINDOW_SIZE/2; - preprecursor = WINDOW_SIZE/2; - for (x = 0; x < params.stereo->cols; x++) { - closest = 999; minErrorValue = 999;matched=0; - minErrorValue=999; - - for (int i = WINDOW_START; i < WINDOW_SIZE; i++) { - tmp = match(params, x, y, i); - tmpSmoothed = tmp + (0 * abs(cursor - i)); - if (tmpSmoothed < minErrorValue) { - minErrorValue = tmpSmoothed; - closest = i; - matched = 1; - } else if (tmpSmoothed == minErrorValue) { - if (abs(cursor - i) < abs(cursor - closest)) { - closest = i; - matched++; - } - } - } - - if (matched) { - cursor = closest; - - /*if (cursor-precursor > 0 && cursor-precursor < 10) { - for (int i = precursor; i < cursor; i++) { - putPixel(params.stereo, x + i, y, 0); - } - }*/ - } - - //if (cursor - precursor != 0) { - putPixel(params.stereo, x + cursor, y, cursor); - //} - - preprecursor = precursor; - precursor = cursor; - } - - - /*cursor = 25;precursor=25; - for (x = params.stereo->cols; x >= 0; x--) { - closest = 999; minErrorValue = 999;matched=0; - minErrorValue=999; - - for (int i = WINDOW_START; i < WINDOW_SIZE; i++) { - tmp = rmatch(params, x, y, i); - tmpSmoothed = tmp + (0.0 * abs(cursor - i)); - if (tmpSmoothed < minErrorValue) { - minErrorValue = tmpSmoothed; - closest = i; - matched = 1; - } else if (tmpSmoothed == minErrorValue) { - if (abs(cursor - i) < abs(cursor - closest)) { - closest = i; - matched++; - } - } - } - - if (matched) { - cursor = closest; - } - - //if (abs(cursor - precursor)) { - //if (abs(getPixelColor(cursor) - getPixelValue(params.stereo, x, y)) < 50) { - putPixel(params.stereo, x, y, cursor); - //} else { - /// putPixel(params.stereo, x, y, 0); - //} - //} - - precursor=cursor; - }*/ - } -} - -int main(int argc, char** argv) { - StereoData params; - - Mat left = imread("left8.png", 1); - Mat right = imread("right8.png", 1); - - Mat stereo(Mat::zeros(left.rows, left.cols, CV_8U)); - - params.left = &left; - params.right = &right; - params.stereo = &stereo; - - //calcDepthMapDynProgr(params); - calcDepthMapMy2(params); - - imshow("Display Image Dyn Progr", stereo); - waitKey(); - - return 0; -} diff --git a/src/dyn_prog.c b/src/dyn_prog.c deleted file mode 100644 index 558e249..0000000 --- a/src/dyn_prog.c +++ /dev/null @@ -1,120 +0,0 @@ -/*int dispForPixel(Mat &left, Mat &right, int x, int y, int cursor) -{ - int leftValue = getPixelValue(left, x, y); - int rightValue, diff, minimum = 2555; - int result = cursor, b, c, d, m,g = 0,h; - - for (int i = 0; i < WINDOW_SIZE; i++) { - rightValue = getPixelValue(right, x + i, y); - diff = abs(leftValue - rightValue); - if (diff < minimum) { - minimum = diff; - h = i; - g = 0; - } - - if (diff == minimum) { - g++; - } - } - - minimum = abs(leftValue - getPixelValue(right, x + cursor, y)); - if (g == 1) { - result = h; - } else if (x == 0) { - for (int i = 0; i < WINDOW_SIZE; i++) { - rightValue = getPixelValue(right, x + i, y); - diff = abs(leftValue - rightValue); - if (diff < minimum) { - minimum = diff; - result = i; - } - } - } else { - for (int i = 0; i < 255; i++) { - b = abs(getPixelValue(left, x + 1, y) - getPixelValue(right, x + cursor, y)); - c = abs(getPixelValue(left, x, y) - getPixelValue(right, x + cursor + 1, y)); - d = abs(getPixelValue(left, x + 1, y) - getPixelValue(right, x + cursor + 1, y)); - - m = d; - if (b < m) { - m = b; - } - if (c < m) { - m = c; - } - - if (m == d) { - result = cursor; - break; - } else if (m == b) { - cursor--; - } else if (m == c) { - cursor++; - } - } - } - - return result; -}*/ - -/* -int rdispForPixel(Mat &left, Mat &right, int x, int y, int cursor) -{ - int leftValue = getPixelValue(left, x, y); - int rightValue, diff, minimum = abs(leftValue - getPixelValue(right, x - cursor, y)); - int result = cursor, b, c, d, m; - - if (x == 0) { - for (int i = 0; i < WINDOW_SIZE; i++) { - rightValue = getPixelValue(right, x + i, y); - diff = abs(leftValue - rightValue); - if (diff < minimum) { - minimum = diff; - result = i; - } - } - } else { - for (int i = 0; i < 255; i++) { - b = abs(getPixelValue(left, x - 1, y) - getPixelValue(right, x - cursor, y)); - c = abs(getPixelValue(left, x, y) - getPixelValue(right, x - cursor - 1, y)); - d = abs(getPixelValue(left, x - 1, y) - getPixelValue(right, x - cursor - 1, y)); - - m = d; - if (b < m) { - m = b; - } - if (c < m) { - m = c; - } - - if (m == d) { - result = cursor; - break; - } else if (m == b) { - cursor--; - } else if (m == c) { - cursor++; - } - } - } - - return result; -} - - -void calcDepthMapDynProgr(StereoData ¶ms, Mat &stereo, Mat &left, Mat &right) { - int x, y, cursor; - uchar* data; - - for (y = 0; y < stereo.rows; y++) { - cursor = 0; - for (x = 0; x < stereo.cols; x++) { - //cursor = rdispForPixel(left, right, stereo.cols - x, y, cursor); - cursor = dispForPixel(left, right, x, y, cursor); - data = stereo.ptr(y, x); - data[0] = cursor; - } - } -} -*/ diff --git a/src/hsv.c b/src/hsv.c deleted file mode 100644 index 703e6da..0000000 --- a/src/hsv.c +++ /dev/null @@ -1,94 +0,0 @@ -/*#include - -CvScalar HSVtoRGBcvScalar(int H, int S, int V) { - - int bH = H; // H component - int bS = S; // S component - int bV = V; // V component - float fH, fS, fV; - float fR, fG, fB; - const float FLOAT_TO_BYTE = 255.0f; - const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE; - -// Convert from 8-bit integers to floats - fH = (float) bH * BYTE_TO_FLOAT; - fS = (float) bS * BYTE_TO_FLOAT; - fV = (float) bV * BYTE_TO_FLOAT; - -// Convert from HSV to RGB, using float ranges 0.0 to 1.0 - int iI; - float fI, fF, p, q, t; - - if (bS == 0) { -// achromatic (grey) - fR = fG = fB = fV; - } else { -// If Hue == 1.0, then wrap it around the circle to 0.0 - if (fH >= 1.0f) - fH = 0.0f; - - fH *= 6.0; // sector 0 to 5 - fI = floor(fH); // integer part of h (0,1,2,3,4,5 or 6) - iI = (int) fH; // " " " " - fF = fH - fI; // factorial part of h (0 to 1) - - p = fV * (1.0f - fS); - q = fV * (1.0f - fS * fF); - t = fV * (1.0f - fS * (1.0f - fF)); - - switch (iI) { - case 0: - fR = fV; - fG = t; - fB = p; - break; - case 1: - fR = q; - fG = fV; - fB = p; - break; - case 2: - fR = p; - fG = fV; - fB = t; - break; - case 3: - fR = p; - fG = q; - fB = fV; - break; - case 4: - fR = t; - fG = p; - fB = fV; - break; - default: // case 5 (or 6): - fR = fV; - fG = p; - fB = q; - break; - } - } - -// Convert from floats to 8-bit integers - int bR = (int) (fR * FLOAT_TO_BYTE); - int bG = (int) (fG * FLOAT_TO_BYTE); - int bB = (int) (fB * FLOAT_TO_BYTE); - -// Clip the values to make sure it fits within the 8bits. - if (bR > 255) - bR = 255; - if (bR < 0) - bR = 0; - if (bG > 255) - bG = 255; - if (bG < 0) - bG = 0; - if (bB > 255) - bB = 255; - if (bB < 0) - bB = 0; - -// Set the RGB cvScalar with G B R, you can use this values as you want too.. - return cvScalar(bB, bG, bR); // R component -}*/ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..33f4cdb --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,31 @@ +/* + * main.cpp + * + * Created on: 27 июня 2014 г. + * Author: andrey + */ + +#include +#include "SvImage.h" +#include "SvProcessorV1.h" +#include "SvProcessorV2.h" + +using namespace cv; + +int main(int argc, char** argv) { + Mat matLeft = imread("left8.png", 1); + Mat matRight = imread("right8.png", 1); + Mat matStereo(Mat::zeros(matLeft.rows, matLeft.cols, CV_8U)); + + SvImage left(matLeft); + SvImage right(matRight); + SvImage stereo(matStereo); + + SvProcessorV1 proc(left, right); + proc.run(stereo, SvProcessorV1::LEFT); + + imshow("Display Image", stereo.getCvMatrix()); + waitKey(); + + return 0; +} diff --git a/src/my.c b/src/my.c deleted file mode 100644 index e69de29..0000000