| 1 |
|
| 2 |
#ifndef CVD_INC_INTERPOLATE_H |
| 3 |
#define CVD_INC_INTERPOLATE_H |
| 4 |
|
| 5 |
#include <cvd/config.h> |
| 6 |
#include <cvd/image.h> |
| 7 |
#include <cvd/vector_image_ref.h> |
| 8 |
#include <cvd/vision.h> |
| 9 |
|
| 10 |
namespace CVD |
| 11 |
{ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
inline double interpolate_extremum(double d1, double d2, double d3) |
| 24 |
{ |
| 25 |
assert(d2 >= d1 && d2 > d3 || d2 > d1 && d2 >= d3 || d2 <= d1 && d2 < d3 || d2 < d1 && d2 <= d3); |
| 26 |
|
| 27 |
|
| 28 |
return -0.5 * (d3 - d1) / (d3 + d1 - 2 * d2); |
| 29 |
} |
| 30 |
|
| 31 |
#if defined CVD_HAVE_TOON || defined DOXYGEN_IGNORE_INTERNAL |
| 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 |
inline TooN::Vector<2> interpolate_extremum(double I__1__1, |
| 57 |
double I__1_0, |
| 58 |
double I__1_1, |
| 59 |
double I_0__1, |
| 60 |
double I_0_0, |
| 61 |
double I_0_1, |
| 62 |
double I_1__1, |
| 63 |
double I_1_0, |
| 64 |
double I_1_1) |
| 65 |
{ |
| 66 |
|
| 67 |
double gx = 0.5 * (I_1_0 - I__1_0); |
| 68 |
double gy = 0.5 * (I_0_1 - I_0__1); |
| 69 |
|
| 70 |
|
| 71 |
double gxx = I_1_0 - 2 * I_0_0 + I__1_0; |
| 72 |
double gyy = I_0_1 - 2 * I_0_0 + I_0__1; |
| 73 |
double gxy = 0.25 * (I_1_1 + I__1__1 - I_1__1 - I__1_1); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
double Dinv = 1. / (gxx * gyy - gxy * gxy); |
| 78 |
|
| 79 |
TooN::Vector<2> v; |
| 80 |
v[0] = -Dinv * (gyy * gx - gxy * gy); |
| 81 |
v[1] = -Dinv * (-gxy * gx + gxx * gy); |
| 82 |
|
| 83 |
return v; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
template <class I> |
| 92 |
inline TooN::Vector<2> interpolate_extremum(const BasicImage<I>& i, ImageRef p) |
| 93 |
{ |
| 94 |
CVD_IMAGE_ASSERT(p.x > 0 && p.y > 0 && p.x < i.size().x - 1 && p.y < i.size().y - 1, ImageError::AccessOutsideImage); |
| 95 |
|
| 96 |
|
| 97 |
double I__1__1 = i[p + ImageRef(-1, -1)]; |
| 98 |
double I__1_0 = i[p + ImageRef(-1, 0)]; |
| 99 |
double I__1_1 = i[p + ImageRef(-1, 1)]; |
| 100 |
double I_0__1 = i[p + ImageRef(0, -1)]; |
| 101 |
double I_0_0 = i[p + ImageRef(0, 0)]; |
| 102 |
double I_0_1 = i[p + ImageRef(0, 1)]; |
| 103 |
double I_1__1 = i[p + ImageRef(1, -1)]; |
| 104 |
double I_1_0 = i[p + ImageRef(1, 0)]; |
| 105 |
double I_1_1 = i[p + ImageRef(1, 1)]; |
| 106 |
return interpolate_extremum(I__1__1, I__1_0, I__1_1, I_0__1, I_0_0, I_0_1, I_1__1, I_1_0, I_1_1) + vec(p); |
| 107 |
} |
| 108 |
|
| 109 |
#endif |
| 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 |
inline std::pair<TooN::Vector<2>, double> interpolate_extremum_value(double I__1__1, |
| 136 |
double I__1_0, |
| 137 |
double I__1_1, |
| 138 |
double I_0__1, |
| 139 |
double I_0_0, |
| 140 |
double I_0_1, |
| 141 |
double I_1__1, |
| 142 |
double I_1_0, |
| 143 |
double I_1_1) |
| 144 |
{ |
| 145 |
|
| 146 |
double gx = 0.5 * (I_1_0 - I__1_0); |
| 147 |
double gy = 0.5 * (I_0_1 - I_0__1); |
| 148 |
|
| 149 |
|
| 150 |
double gxx = I_1_0 - 2 * I_0_0 + I__1_0; |
| 151 |
double gyy = I_0_1 - 2 * I_0_0 + I_0__1; |
| 152 |
double gxy = 0.25 * (I_1_1 + I__1__1 - I_1__1 - I__1_1); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
double Dinv = 1. / (gxx * gyy - gxy * gxy); |
| 157 |
|
| 158 |
TooN::Vector<2> v; |
| 159 |
v[0] = -Dinv * (gyy * gx - gxy * gy); |
| 160 |
v[1] = -Dinv * (-gxy * gx + gxx * gy); |
| 161 |
|
| 162 |
double value = I_0_0 + (gx + 0.5 * gxx * v[0] + gxy * v[1]) * v[0] + (gy + 0.5 * gyy * v[1]) * v[1]; |
| 163 |
|
| 164 |
return std::make_pair(v, value); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
template <class I> |
| 173 |
inline std::pair<TooN::Vector<2>, double> interpolate_extremum_value(const BasicImage<I>& i, ImageRef p) |
| 174 |
{ |
| 175 |
CVD_IMAGE_ASSERT(p.x > 0 && p.y > 0 && p.x < i.size().x - 1 && p.y < i.size().y - 1, ImageError::AccessOutsideImage); |
| 176 |
|
| 177 |
|
| 178 |
double I__1__1 = i[p + ImageRef(-1, -1)]; |
| 179 |
double I__1_0 = i[p + ImageRef(-1, 0)]; |
| 180 |
double I__1_1 = i[p + ImageRef(-1, 1)]; |
| 181 |
double I_0__1 = i[p + ImageRef(0, -1)]; |
| 182 |
double I_0_0 = i[p + ImageRef(0, 0)]; |
| 183 |
double I_0_1 = i[p + ImageRef(0, 1)]; |
| 184 |
double I_1__1 = i[p + ImageRef(1, -1)]; |
| 185 |
double I_1_0 = i[p + ImageRef(1, 0)]; |
| 186 |
double I_1_1 = i[p + ImageRef(1, 1)]; |
| 187 |
std::pair<TooN::Vector<2>, double> result = interpolate_extremum_value(I__1__1, I__1_0, I__1_1, I_0__1, I_0_0, I_0_1, I_1__1, I_1_0, I_1_1); |
| 188 |
result.first += vec(p); |
| 189 |
return result; |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
#endif |