-
Notifications
You must be signed in to change notification settings - Fork 31
/
PatchMatch2NCC.cc
226 lines (206 loc) · 8.52 KB
/
PatchMatch2NCC.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <PatchMatch2NCC.h>
#include <vw/Core/Exception.h>
#include <vw/Math/BBox.h>
#include <vw/Math/Vector.h>
#include <vw/Image/ImageView.h>
#include <boost/random/uniform_01.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/linear_congruential.hpp>
using namespace vw;
void
stereo::PMNCCBase::add_uniform_noise(BBox2i const& range_of_noise_to_add, // Inclusive
BBox2i const& max_search_range, // Inclusive
BBox2i const& other_image_bbox, // Exclusive
ImageView<stereo::PMNCCBase::DispT>& disparity ) const {
typedef boost::random::uniform_01<float> DistributionT;
typedef boost::variate_generator<GenT, DistributionT > vargen_type;
vargen_type random_source(GenT(0), DistributionT());
BBox2i local_search_range_bounds, local_search_range;
for (int j = 0; j < disparity.rows(); j++) {
local_search_range_bounds.min().y() =
std::max(max_search_range.min().y(),
other_image_bbox.min().y() - j);
local_search_range_bounds.max().y() =
std::min(max_search_range.max().y(),
other_image_bbox.max().y() - j - 1);
for (int i = 0; i < disparity.cols(); i++) {
local_search_range_bounds.min().x() =
std::max(max_search_range.min().x(),
other_image_bbox.min().x() - i);
local_search_range_bounds.max().x() =
std::min(max_search_range.max().x(),
other_image_bbox.max().x() - i - 1);
local_search_range = range_of_noise_to_add;
local_search_range.min() += disparity(i, j);
local_search_range.max() += disparity(i, j);
local_search_range.crop(local_search_range_bounds);
#ifdef DEBUG
DispT d =
elem_prod(Vector2f(random_source(),random_source()),
local_search_range.size()) + local_search_range.min();
VW_DEBUG_ASSERT(other_image_bbox.contains(Vector2i(i,j) + d),
MathErr() << "Modified disparity points outside of other image.");
disparity(i,j) = d;
#else
disparity(i,j) =
elem_prod(Vector2f(random_source(),random_source()),
local_search_range.size()) + local_search_range.min();
#endif
}
}
}
// Simple square kernels
float stereo::PMNCCBase::calculate_cost( Vector2i const& a_loc, Vector2i const& disparity,
ImageView<float> const& a, ImageView<float> const& b,
BBox2i const& a_roi, BBox2i const& b_roi,
BBox2i const& kernel_roi) const {
#define left_kernel_TEMP crop( a, kernel_roi + a_loc - a_roi.min() )
#define right_kernel_TEMP crop( b, kernel_roi + a_loc + disparity - b_roi.min() )
float cov_lr =
sum_of_pixel_values
(left_kernel_TEMP * right_kernel_TEMP);
float cov_ll =
sum_of_pixel_values
(left_kernel_TEMP * left_kernel_TEMP);
float cov_rr =
sum_of_pixel_values
(right_kernel_TEMP * right_kernel_TEMP);
//printf("%f %f %f\n", cov_lr, cov_ll, cov_rr);
return 1 - cov_lr / sqrt(cov_ll * cov_rr);
}
// Evaluates current disparity and writes its cost
void stereo::PMNCCBase::evaluate_disparity( ImageView<float> const& a, ImageView<float> const& b,
BBox2i const& a_roi, BBox2i const& b_roi,
ImageView<DispT>& ab_disparity,
ImageView<float>& ab_cost ) const {
typedef ImageView<float>::pixel_accessor CAccT;
typedef ImageView<DispT>::pixel_accessor DAccT;
CAccT cost_row = ab_cost.origin();
DAccT disp_row = ab_disparity.origin();
Vector2i loc;
for ( loc[1] = 0; loc[1] < ab_disparity.rows(); loc[1]++ ) {
CAccT cost_col = cost_row;
DAccT disp_col = disp_row;
for ( loc[0] = 0; loc[0] < ab_disparity.cols(); loc[0]++ ) {
*cost_col =
calculate_cost( loc, *disp_col,
a, b, a_roi, b_roi, m_kernel_roi);
cost_col.next_col();
disp_col.next_col();
}
cost_row.next_row();
disp_row.next_row();
}
}
void stereo::PMNCCBase::keep_lowest_cost( ImageView<DispT> const& src_disp,
ImageView<float> const& src_cost,
ImageView<DispT>& dest_disp,
ImageView<float>& dest_cost ) const {
for ( int j = 0; j < dest_disp.rows(); j++ ) {
for ( int i = 0; i < dest_disp.cols(); i++ ) {
if ( dest_cost(i,j) > src_cost(i,j) ) {
dest_cost(i,j) = src_cost(i,j);
dest_disp(i,j) = src_disp(i,j);
}
}
}
}
// Propogates from the 3x3 neighbor hood
void stereo::PMNCCBase::evaluate_8_connected( ImageView<float> const& a,
ImageView<float> const& b,
BBox2i const& a_roi, BBox2i const& b_roi,
ImageView<DispT> const& ba_disparity,
BBox2i const& ba_roi,
ImageView<DispT>& ab_disparity,
ImageView<float>& ab_cost) const {
float cost;
DispT d_new;
for ( int j = 0; j < ab_disparity.rows(); j++ ) {
for ( int i = 0; i < ab_disparity.cols(); i++ ) {
DispT loc(i,j);
float curr_best_cost = ab_cost(i,j);
DispT curr_best_d = ab_disparity(i, j);
DispT starting_d = ab_disparity(i, j);
#define EVALUATE_AND_KEEP_BEST \
if ( ba_roi.contains(d_new + loc) && curr_best_d != d_new && starting_d != d_new) { \
cost = calculate_cost(loc, d_new, a, b, a_roi, b_roi, \
m_kernel_roi); \
if (cost < curr_best_cost) { \
curr_best_cost = cost; \
curr_best_d = d_new; \
} \
}
if ( i > 0 ) {
// Compare left
d_new = ab_disparity(i-1,j);
EVALUATE_AND_KEEP_BEST
}
if ( j > 0 ) {
// Compare up
d_new = ab_disparity(i,j - 1);
EVALUATE_AND_KEEP_BEST
}
if ( i < ab_disparity.cols() - 1) {
// Compare right
d_new = ab_disparity(i+1,j);
EVALUATE_AND_KEEP_BEST
}
if ( j < ab_disparity.rows() - 1 ) {
// Compare lower
d_new = ab_disparity(i,j+1);
EVALUATE_AND_KEEP_BEST
}
{
// Compare LR alternative
DispT d = ab_disparity(i,j);
d_new = -ba_disparity(i + d[0] - ba_roi.min().x(),
j + d[1] - ba_roi.min().y());
EVALUATE_AND_KEEP_BEST
}
ab_cost(i,j) = curr_best_cost;
ab_disparity(i,j) = curr_best_d;
}
}
#undef EVALUATE_AND_KEEP_BEST
}
void
stereo::PMNCCBase::cross_corr_consistency_check(ImageView<DispT> const& ab_disparity,
ImageView<DispT> const& ba_disparity,
BBox2i const& ab_roi,
BBox2i const& ba_roi,
ImageView<PixelMask<DispT> >& ab_masked_disp) const {
if (m_consistency_threshold < 0) {
ab_masked_disp = ab_disparity;
return;
}
for ( int j = 0; j < ab_disparity.rows(); j++ ) {
for ( int i = 0; i < ab_disparity.cols(); i++ ) {
Vector2i other =
Vector2i(i, j) + Vector2i(ab_disparity(i, j))
+ ab_roi.min() - ba_roi.min();
if (norm_2(Vector2f(ab_disparity(i, j)) +
Vector2f(ba_disparity(other.x(), other.y()))) < m_consistency_threshold) {
// It is good
ab_masked_disp(i, j) = ab_disparity(i, j);
} else {
ab_masked_disp(i, j) = PixelMask<DispT>();
}
}
}
}
stereo::PMNCCBase::PMNCCBase( BBox2i const& search_region, Vector2i const& kernel,
float consistency_threshold,
int32 max_iterations) :
m_search_region( search_region ),
m_search_region_rl( -search_region.max(), -search_region.min() ),
m_kernel_size( kernel ),
m_consistency_threshold( consistency_threshold ),
m_max_iterations(max_iterations) {
m_expansion = m_kernel_size / 2;
m_expansion +=
Vector2i( BilinearInterpolation::pixel_buffer,
BilinearInterpolation::pixel_buffer );
m_kernel_roi = BBox2i(-m_kernel_size.x()/2,
-m_kernel_size.y()/2,
m_kernel_size.x(), m_kernel_size.y());
}