Move images

This commit is contained in:
Andrey Tkachenko 2014-07-07 11:03:56 +04:00
commit 6544aaf145
27 changed files with 359 additions and 438 deletions

View File

Before

Width:  |  Height:  |  Size: 170 KiB

After

Width:  |  Height:  |  Size: 170 KiB

View File

Before

Width:  |  Height:  |  Size: 259 KiB

After

Width:  |  Height:  |  Size: 259 KiB

View File

Before

Width:  |  Height:  |  Size: 2.3 MiB

After

Width:  |  Height:  |  Size: 2.3 MiB

View File

Before

Width:  |  Height:  |  Size: 682 KiB

After

Width:  |  Height:  |  Size: 682 KiB

View File

Before

Width:  |  Height:  |  Size: 335 KiB

After

Width:  |  Height:  |  Size: 335 KiB

View File

Before

Width:  |  Height:  |  Size: 356 KiB

After

Width:  |  Height:  |  Size: 356 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

Before

Width:  |  Height:  |  Size: 267 KiB

After

Width:  |  Height:  |  Size: 267 KiB

View File

Before

Width:  |  Height:  |  Size: 171 KiB

After

Width:  |  Height:  |  Size: 171 KiB

View File

Before

Width:  |  Height:  |  Size: 258 KiB

After

Width:  |  Height:  |  Size: 258 KiB

View File

Before

Width:  |  Height:  |  Size: 2.3 MiB

After

Width:  |  Height:  |  Size: 2.3 MiB

View File

Before

Width:  |  Height:  |  Size: 682 KiB

After

Width:  |  Height:  |  Size: 682 KiB

View File

Before

Width:  |  Height:  |  Size: 334 KiB

After

Width:  |  Height:  |  Size: 334 KiB

View File

Before

Width:  |  Height:  |  Size: 354 KiB

After

Width:  |  Height:  |  Size: 354 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

Before

Width:  |  Height:  |  Size: 275 KiB

After

Width:  |  Height:  |  Size: 275 KiB

56
src/SvImage.cpp Normal file
View File

@ -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 = &image;
}
SvImage::~SvImage() {
}

32
src/SvImage.h Normal file
View File

@ -0,0 +1,32 @@
/*
* SvImage.h
*
* Created on: 27 июня 2014 г.
* Author: andrey
*/
#ifndef SVIMAGE_H_
#define SVIMAGE_H_
#include <opencv2/opencv.hpp>
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_ */

116
src/SvProcessorV1.cpp Normal file
View File

@ -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() {
}

37
src/SvProcessorV1.h Normal file
View File

@ -0,0 +1,37 @@
/*
* SvProcessor.h
*
* Created on: 27 июня 2014 г.
* Author: andrey
*/
#ifndef SVPROCESSORV1_H_
#define SVPROCESSORV1_H_
#include <iostream>
#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_ */

51
src/SvProcessorV2.cpp Normal file
View File

@ -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<int>();
this->lineRight = new std::vector<int>();
}
void SvProcessorV2::scanLine(int pos, ImageType type) {
SvImage* image;
std::vector<int>* 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;
}

36
src/SvProcessorV2.h Normal file
View File

@ -0,0 +1,36 @@
/*
* SvProcessor.h
*
* Created on: 27 июня 2014 г.
* Author: andrey
*/
#ifndef SVPROCESSOR_H_
#define SVPROCESSOR_H_
#include <iostream>
#include "SvImage.h"
using namespace std;
class SvProcessorV2 {
protected:
SvImage* left;
SvImage* right;
std::vector<int>* lineLeft;
std::vector<int>* 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_ */

View File

@ -1,224 +0,0 @@
//============================================================================
// Name : Test.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
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 1
#define MATCH_SIZE_Y 1
#define ERROR_LEVEL 300
#define COLOR_SCALE 0
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;
}
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 normalize(StereoData& params, 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, bool backwise=false) {
int error = 0, offset;
int Rvalue, Gvalue, Bvalue;
int sign=backwise?-1:1;
int MX2 = MATCH_SIZE_X >> 1, MY2 = MATCH_SIZE_Y >> 1;
for (int i = -MX2; i <= MX2; i++) {
offset = x + (j*sign) + (i*sign);
if (offset < 0 || offset >= params.right->cols) {
return 999;
}
for (int g = -MY2; g <= MY2; g++) {
if (y + g >= params.right->rows) {
return 999;
}
Rvalue = abs(getPixelValue(params.left, x + (i*sign), y + g, 0) - getPixelValue(params.right, offset, y + g, 0));
Gvalue = abs(getPixelValue(params.left, x + (i*sign), y + g, 1) - getPixelValue(params.right, offset, y + g, 1));
Bvalue = abs(getPixelValue(params.left, x + (i*sign), y + g, 2) - getPixelValue(params.right, offset, y + g, 2));
error += Rvalue << COLOR_SCALE;
error += Gvalue << COLOR_SCALE;
error += Bvalue << COLOR_SCALE;
}
}
return error;
}
int closestMatch(StereoData& params, int cursor, int x, int y, bool backwise = false) {
int tmp, tmpSmoothed=999, minErrorValue=999, matched=0;
int closest=999;
for (int i = WINDOW_START; i < WINDOW_SIZE; i++) {
tmp = match(params, x, y, i, backwise);
if (tmp > ERROR_LEVEL) continue;
tmpSmoothed = tmp + (0.1 * 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++;
}
}
}
return matched ? closest : -1;
}
void calcLineBackwise(StereoData& params, int y) {
int closest, diff;
int cursor = WINDOW_SIZE/2;
int precursor = WINDOW_SIZE/2;
int preprecursor = WINDOW_SIZE/2;
for (int x = params.stereo->cols - 1; x >= 0 ; x--) {
closest = closestMatch(params, cursor, x, y, true);
if (closest != -1) {
diff = closest - cursor;
if (diff > 0 /*&& diff < 5*/) {
for (int i = cursor; i < closest; i++) {
putPixel(params.stereo, x - i, y, 0);
}
}
cursor = closest;
putPixel(params.stereo, x - cursor, y, cursor);
}
preprecursor = precursor;
precursor = cursor;
}
}
void calcLine(StereoData& params, int y) {
int closest, diff;
int cursor = WINDOW_SIZE/2;
int precursor = WINDOW_SIZE/2;
int preprecursor = WINDOW_SIZE/2;
for (int x = 0; x < params.stereo->cols; x++) {
closest = closestMatch(params, cursor, x, y);
if (closest != -1) {
diff = closest - cursor;
if (diff > 0 && diff < 5) {
for (int i = cursor; i <= closest; i++) {
putPixel(params.stereo, x + i, y, 0);
}
}
cursor = closest;
putPixel(params.stereo, x + cursor, y, cursor);
}
preprecursor = precursor;
precursor = cursor;
}
}
void calcDepthMapMy2(StereoData &params) {
for (int y = 0; y < params.stereo->rows; y++) {
calcLine(params, y);
}
}
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;
}

View File

@ -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 &params, 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;
}
}
}
*/

View File

@ -1,94 +0,0 @@
/*#include <opencv2/opencv.hpp>
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
}*/

31
src/main.cpp Normal file
View File

@ -0,0 +1,31 @@
/*
* main.cpp
*
* Created on: 27 июня 2014 г.
* Author: andrey
*/
#include <opencv2/opencv.hpp>
#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;
}

View File