/
SingleThreadedDisparityMapGenerator.cpp
137 lines (112 loc) · 4.41 KB
/
SingleThreadedDisparityMapGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "../include/SingleThreadedDisparityMapGenerator.hpp"
SingleThreadedDisparityMapGenerator::SingleThreadedDisparityMapGenerator(
const DisparityMapAlgorithmParameters_t& parameters)
: parameters_(parameters) {
this->ensureParametersValid();
this->disparityBuf_.resize(this->parameters_.rightScanSteps + this->parameters_.leftScanSteps + 1, 0);
}
void SingleThreadedDisparityMapGenerator::setParameters(
const DisparityMapAlgorithmParameters_t& parameters) {
this->parameters_ = parameters;
this->ensureParametersValid();
this->disparityBuf_.resize(this->parameters_.rightScanSteps + this->parameters_.leftScanSteps + 1, 0);
}
const DisparityMapAlgorithmParameters_t& SingleThreadedDisparityMapGenerator::getParameters() const {
return this->parameters_;
}
void SingleThreadedDisparityMapGenerator::computeDisparity(
const cv::Mat& leftImage,
const cv::Mat& rightImage,
cv::Mat& disparity) {
for (int y = 0; y < disparity.rows; y++) {
for (int x = 0; x < disparity.cols; x++) {
disparity.at<float>(y, x) = computeDisparityForPixel(
y,
x,
leftImage,
rightImage);
}
}
}
void SingleThreadedDisparityMapGenerator::ensureParametersValid() {
if (this->parameters_.blockSize < 0) {
throw std::runtime_error("Error: block size is less than zero.");
}
if (this->parameters_.blockSize % 2 == 0) {
throw std::runtime_error("Error: block size is not odd.");
}
if (this->parameters_.leftScanSteps < 0) {
throw std::runtime_error("Error: left scan steps is negative.");
}
if (this->parameters_.rightScanSteps < 0) {
throw std::runtime_error("Error: right scan steps is negative.");
}
}
float SingleThreadedDisparityMapGenerator::computeDisparityForPixel(
int y,
int x,
const cv::Mat& leftImage,
const cv::Mat& rightImage) {
int maxBlockStep = (this->parameters_.blockSize - 1) / 2;
int templateLeftHalfWidth = std::min(x, maxBlockStep);
int templateRightHalfWidth = std::min(leftImage.cols - x - 1, maxBlockStep);
int templateTopHalfHeight = std::min(y, maxBlockStep);
int templateBottomHalfHeight = std::min(leftImage.rows - y - 1, maxBlockStep);
int templateWidth = templateLeftHalfWidth + templateRightHalfWidth + 1;
int templateHeight = templateTopHalfHeight + templateBottomHalfHeight + 1;
int leftMinY = y - templateTopHalfHeight;
int leftMinX = x - templateLeftHalfWidth;
int rightMinStartX = std::max(0, x - this->parameters_.leftScanSteps - templateLeftHalfWidth);
int rightMaxStartX = std::min(leftImage.cols - templateWidth /*- 1*/, x + this->parameters_.rightScanSteps - templateLeftHalfWidth);
int numSteps = rightMaxStartX - rightMinStartX;
int bestIndex = 0;
int bestSadValue = std::numeric_limits<int>::max();
int zeroDisparityIndex = x - rightMinStartX - templateLeftHalfWidth;
for (int xx = rightMinStartX; xx <= rightMaxStartX; xx++) {
int sad = computeSadOverBlock(
leftMinY,
leftMinX,
leftMinY, // Ys are aligned for the two images
xx,
templateWidth,
templateHeight,
leftImage,
rightImage);
this->disparityBuf_[xx - rightMinStartX] = sad;
if (sad < bestSadValue) {
bestSadValue = sad;
bestIndex = xx - rightMinStartX;
}
}
float disparity = static_cast<float>(std::abs(bestIndex - zeroDisparityIndex));
if ((bestIndex == 0)
||
(bestIndex == numSteps)
||
(bestSadValue == 0)) {
return disparity;
}
float c3 = disparityBuf_[bestIndex+1];
float c2 = disparityBuf_[bestIndex];
float c1 = disparityBuf_[bestIndex-1];
return disparity - (0.5 * ((c3 - c1) / (c1 - (2*c2) + c3)));
}
int SingleThreadedDisparityMapGenerator::computeSadOverBlock(
int minYL,
int minXL,
int minYR,
int minXR,
int width,
int height,
const cv::Mat& leftImage,
const cv::Mat& rightImage) {
int sum = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
sum += std::abs(
leftImage.at<unsigned char>(y + minYL, x + minXL)
- rightImage.at<unsigned char>(y + minYR, x + minXR));
}
}
return sum;
}