| 1 |
#ifndef CVD_INC_TENSOR_VOTE_H |
| 2 |
#define CVD_INC_TENSOR_VOTE_H |
| 3 |
|
| 4 |
#include <TooN/TooN.h> |
| 5 |
#include <TooN/helpers.h> |
| 6 |
#include <cvd/image.h> |
| 7 |
#include <utility> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
namespace CVD |
| 11 |
{ |
| 12 |
|
| 13 |
#ifndef DOXYGEN_IGNORE_INTERNAL |
| 14 |
namespace TensorVoting |
| 15 |
{ |
| 16 |
struct TV_coord |
| 17 |
{ |
| 18 |
std::ptrdiff_t o; |
| 19 |
int x; |
| 20 |
int y; |
| 21 |
}; |
| 22 |
|
| 23 |
std::vector<std::pair<TV_coord, TooN::Matrix<2>>> compute_a_tensor_kernel(int radius, double cutoff, double angle, double sigma, double ratio, int row_stride); |
| 24 |
inline unsigned int quantize_half_angle(double r, int num_divs) |
| 25 |
{ |
| 26 |
return ((int)floor((r / M_PI + 100) * num_divs + 0.5)) % num_divs; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
#endif |
| 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 |
template <class C> |
| 81 |
Image<TooN::Matrix<2>> dense_tensor_vote_gradients(const BasicImage<C>& image, double sigma, double ratio, double cutoff = 0.001, unsigned int num_divs = 4096) |
| 82 |
{ |
| 83 |
using std::pair; |
| 84 |
using std::vector; |
| 85 |
using TensorVoting::TV_coord; |
| 86 |
using TooN::Matrix; |
| 87 |
|
| 88 |
Matrix<2> zero(TooN::Zeros); |
| 89 |
Image<Matrix<2>> field(image.size(), zero); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
int kernel_radius = (int)ceil(sigma * sqrt(-log(cutoff))); |
| 96 |
|
| 97 |
|
| 98 |
vector<vector<pair<TV_coord, Matrix<2>>>> kernels; |
| 99 |
for(unsigned int i = 0; i < num_divs; i++) |
| 100 |
{ |
| 101 |
double angle = M_PI * i / num_divs; |
| 102 |
kernels.push_back(TensorVoting::compute_a_tensor_kernel(kernel_radius, cutoff, angle, sigma, ratio, field.row_stride())); |
| 103 |
} |
| 104 |
|
| 105 |
for(int y = kernel_radius; y < field.size().y - kernel_radius; y++) |
| 106 |
for(int x = kernel_radius; x < field.size().x - kernel_radius; x++) |
| 107 |
{ |
| 108 |
double gx = ((double)image[y][x + 1] - image[y][x - 1]) / 2.; |
| 109 |
double gy = ((double)image[y + 1][x] - image[y - 1][x]) / 2.; |
| 110 |
|
| 111 |
double scale = sqrt(gx * gx + gy * gy); |
| 112 |
unsigned int direction = TensorVoting::quantize_half_angle(M_PI / 2 + atan2(gy, gx), num_divs); |
| 113 |
|
| 114 |
const vector<pair<TV_coord, Matrix<2>>>& kernel = kernels[direction]; |
| 115 |
|
| 116 |
Matrix<2>* p = &field[y][x]; |
| 117 |
|
| 118 |
|
| 119 |
for(unsigned int i = 0; i < kernel.size(); i++) |
| 120 |
{ |
| 121 |
p[kernel[i].first.o][0][0] += kernel[i].second[0][0] * scale; |
| 122 |
p[kernel[i].first.o][0][1] += kernel[i].second[0][1] * scale; |
| 123 |
p[kernel[i].first.o][1][1] += kernel[i].second[1][1] * scale; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
for(int y = 1; y < field.size().y - 1; y++) |
| 129 |
{ |
| 130 |
for(int x = 1; x < field.size().x - 1; x++) |
| 131 |
{ |
| 132 |
|
| 133 |
if(y >= kernel_radius && y < field.size().y - kernel_radius && x == kernel_radius) |
| 134 |
x = field.size().x - kernel_radius; |
| 135 |
|
| 136 |
double gx = ((double)image[y][x + 1] - image[y][x - 1]) / 2.; |
| 137 |
double gy = ((double)image[y + 1][x] - image[y - 1][x]) / 2.; |
| 138 |
|
| 139 |
double scale = sqrt(gx * gx + gy * gy); |
| 140 |
unsigned int direction = TensorVoting::quantize_half_angle(M_PI / 2 + atan(gy / gx), num_divs); |
| 141 |
|
| 142 |
const vector<pair<TV_coord, Matrix<2>>>& kernel = kernels[direction]; |
| 143 |
|
| 144 |
Matrix<2>* p = &field[y][x]; |
| 145 |
|
| 146 |
|
| 147 |
for(unsigned int i = 0; i < kernel.size(); i++) |
| 148 |
{ |
| 149 |
if(kernel[i].first.y + y >= 0 && kernel[i].first.y + y < field.size().y && kernel[i].first.x + x >= 0 && kernel[i].first.x + x < field.size().x) |
| 150 |
{ |
| 151 |
p[kernel[i].first.o][0][0] += kernel[i].second[0][0] * scale; |
| 152 |
p[kernel[i].first.o][0][1] += kernel[i].second[0][1] * scale; |
| 153 |
p[kernel[i].first.o][1][1] += kernel[i].second[1][1] * scale; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
for(Image<Matrix<2>>::iterator i = field.begin(); i != field.end(); i++) |
| 161 |
(*i)[1][0] = (*i)[0][1]; |
| 162 |
|
| 163 |
return field; |
| 164 |
} |
| 165 |
|
| 166 |
#ifdef CVD_EXPERIMENTAL |
| 167 |
|
| 168 |
template <class C> |
| 169 |
Image<TooN::Matrix<2>> dense_tensor_vote_gradients_fast(const BasicImage<C>& image, double sigma, double ratio, double cutoff = 0.001, int num_divs = 4096) |
| 170 |
{ |
| 171 |
using std::make_pair; |
| 172 |
using std::pair; |
| 173 |
using std::vector; |
| 174 |
using TensorVoting::TV_coord; |
| 175 |
using TooN::Matrix; |
| 176 |
|
| 177 |
Matrix<2> zero(TooN::Zeros); |
| 178 |
Image<Matrix<2>> ffield(image.size(), zero); |
| 179 |
Image<__m128> field(image.size()); |
| 180 |
field.zero(); |
| 181 |
|
| 182 |
|
| 183 |
int kernel_radius = (int)ceil(sigma * sqrt(-log(cutoff))); |
| 184 |
vector<vector<pair<TV_coord, Matrix<2>>>> matrix_kernels; |
| 185 |
for(int i = 0; i < num_divs; i++) |
| 186 |
{ |
| 187 |
double angle = M_PI * i / num_divs; |
| 188 |
matrix_kernels.push_back(TensorVoting::compute_a_tensor_kernel(kernel_radius, cutoff, angle, sigma, ratio, field.row_stride())); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
vector<vector<int>> kernel_offsets; |
| 194 |
vector<Image<__m128>> kernel_values; |
| 195 |
for(unsigned int i = 0; i < matrix_kernels.size(); i++) |
| 196 |
{ |
| 197 |
vector<int> off(matrix_kernels[i].size()); |
| 198 |
Image<__m128> val(ImageRef(matrix_kernels[i].size(), 1)); |
| 199 |
|
| 200 |
for(unsigned int j = 0; j < matrix_kernels[i].size(); j++) |
| 201 |
{ |
| 202 |
off[j] = matrix_kernels[i][j].first.o; |
| 203 |
Matrix<2>& m = matrix_kernels[i][j].second; |
| 204 |
val.data()[j] = _mm_setr_ps(m[0][0], m[0][1], m[1][0], m[1][1]); |
| 205 |
} |
| 206 |
|
| 207 |
kernel_offsets.push_back(off); |
| 208 |
kernel_values.push_back(val); |
| 209 |
} |
| 210 |
|
| 211 |
#pragma omp parallel for |
| 212 |
for(int y = kernel_radius; y < field.size().y - kernel_radius; y++) |
| 213 |
for(int x = kernel_radius; x < field.size().x - kernel_radius; x++) |
| 214 |
{ |
| 215 |
float gx = ((float)image[y][x + 1] - image[y][x - 1]) / 2.; |
| 216 |
float gy = ((float)image[y + 1][x] - image[y - 1][x]) / 2.; |
| 217 |
|
| 218 |
float scale = sqrt(gx * gx + gy * gy); |
| 219 |
unsigned int direction = TensorVoting::quantize_half_angle(M_PI / 2 + atan(gy / gx), num_divs); |
| 220 |
|
| 221 |
const vector<int>& off = kernel_offsets[direction]; |
| 222 |
__m128* val = kernel_values[direction].data(); |
| 223 |
__m128* p = &field[y][x]; |
| 224 |
__m128 s = _mm_set1_ps(scale); |
| 225 |
|
| 226 |
for(unsigned int i = 0; i < off.size(); i++) |
| 227 |
p[off[i]] = _mm_add_ps(p[off[i]], _mm_mul_ps(val[i], s)); |
| 228 |
} |
| 229 |
|
| 230 |
for(int y = 0; y < field.size().y; y++) |
| 231 |
for(int x = 0; x < field.size().x; x++) |
| 232 |
{ |
| 233 |
float f[4]; |
| 234 |
_mm_storeu_ps(f, field[y][x]); |
| 235 |
ffield[y][x][0][0] = f[0]; |
| 236 |
ffield[y][x][0][1] = f[1]; |
| 237 |
ffield[y][x][1][0] = f[2]; |
| 238 |
ffield[y][x][1][1] = f[3]; |
| 239 |
} |
| 240 |
|
| 241 |
return ffield; |
| 242 |
} |
| 243 |
#endif |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
#endif |