-
Notifications
You must be signed in to change notification settings - Fork 31
/
TVMin2.cc
109 lines (98 loc) · 3.58 KB
/
TVMin2.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
#include <TVMin2.h>
#include <vw/Image/Filter.h>
#include <vw/Image/Statistics.h>
using namespace vw;
void backdiffx( ImageView<float> const& input,
ImageView<float> & output ) {
output.set_size(input.cols(), input.rows());
crop(output, BBox2i(0,0,1,input.rows())) =
crop(input, BBox2i(0,0,1,input.rows()));
crop(output, BBox2i(1,0,input.cols()-1,input.rows())) =
crop(input, BBox2i(0,0,input.cols()-1,input.rows()));
output = input - output;
}
void backdiffy( ImageView<float> const& input,
ImageView<float> & output ) {
output.set_size(input.cols(), input.rows());
crop(output, BBox2i(0,0,input.cols(),1)) =
crop(input, BBox2i(0,0,input.cols(),1));
crop(output, BBox2i(0,1,input.cols(),input.rows()-1)) =
crop(input, BBox2i(0,0,input.cols(),input.rows()-1));
output = input - output;
}
void forwarddiffx( ImageView<float> const& input,
ImageView<float> & output ) {
output.set_size(input.cols(), input.rows());
crop(output, BBox2i(0,0,input.cols()-1,input.rows())) =
crop(input, BBox2i(1,0,input.cols()-1,input.rows()));
crop(output, BBox2i(input.cols()-1,0,1,input.rows())) =
crop(input, BBox2i(input.cols()-1,0,1,input.rows()));
output -= input;
}
void forwarddiffy( ImageView<float> const& input,
ImageView<float> & output ) {
output.set_size(input.cols(), input.rows());
crop(output, BBox2i(0,0,input.cols(),input.rows()-1)) =
crop(input, BBox2i(0,1,input.cols(),input.rows()-1));
crop(output, BBox2i(0,input.rows()-1,input.cols(),1)) =
crop(input, BBox2i(0,input.rows()-1,input.cols(),1));
output -= input;
}
// To avoid casting higher for uint8 subtraction
template <class PixelT>
struct AbsDiffFunc : public vw::ReturnFixedType<PixelT> {
inline PixelT operator()( PixelT const& a, PixelT const& b ) const {
return fabs( a - b );
}
};
void vw::stereo::imROF( ImageView<float> const& input,
float lambda, int iterations,
ImageView<float> & output ) {
ImageView<float> p1( input.cols(), input.rows() );
ImageView<float> p2( input.cols(), input.rows() );
output.set_size( input.cols(), input.rows() );
ImageView<float> old_output;
ImageView<float> grad_u_x( input.cols(), input.rows() );
ImageView<float> grad_u_y( input.cols(), input.rows() );
ImageView<float> div_p( input.cols(), input.rows() );
ImageView<float> grad_u_mag( input.cols(), input.rows() );
double dt = lambda / 4;
ImageView<float> tmp( input.cols(), input.rows() );
for ( int i = 0; i < iterations; i++ ) {
backdiffx(p1,div_p);
backdiffy(p2,tmp);
div_p += tmp;
old_output = copy(output);
output = input + div_p/lambda;
/*
// See if we should terminate
float eps =
sum_of_pixel_values(abs(output - old_output));
std::cout << i << " " << eps << std::endl;
*/
forwarddiffx(output, grad_u_x);
forwarddiffy(output, grad_u_y);
// Square is not defined
for (int j = 0; j < p1.rows(); j++ ) {
for ( int k = 0; k < p1.cols(); k++ ) {
grad_u_mag(k,j) =
sqrt(grad_u_x(k,j) * grad_u_x(k,j) +
grad_u_y(k,j) * grad_u_y(k,j));
}
}
tmp = float(1) + grad_u_mag * dt;
p1 = dt * grad_u_x + p1;
p2 = dt * grad_u_y + p2;
// Element quotient is not defined for vectors as /
for (int j = 0; j < p1.rows(); j++ ) {
for ( int k = 0; k < p1.cols(); k++ ) {
p1(k,j) = p1(k,j) / tmp(k,j);
}
}
for (int j = 0; j < p1.rows(); j++ ) {
for ( int k = 0; k < p1.cols(); k++ ) {
p2(k,j) = p2(k,j) / tmp(k,j);
}
}
}
}