| 1 |
#ifndef CVD_INCLUDE_EDGE_HPP |
| 2 |
#define CVD_INCLUDE_EDGE_HPP |
| 3 |
|
| 4 |
#include <TooN/TooN.h> |
| 5 |
#include <cmath> |
| 6 |
#include <cvd/convolution.h> |
| 7 |
#include <cvd/image.h> |
| 8 |
#include <queue> |
| 9 |
|
| 10 |
namespace CVD |
| 11 |
{ |
| 12 |
|
| 13 |
template <class T> |
| 14 |
void canny_gradient(const BasicImage<T>& im, BasicImage<TooN::Vector<2>>& grad, const double sigma = 1.0) |
| 15 |
{ |
| 16 |
Image<T> out(im.size()); |
| 17 |
convolveGaussian(im, out, sigma); |
| 18 |
|
| 19 |
for(int y = 1; y < im.size().y - 1; y++) |
| 20 |
{ |
| 21 |
for(int x = 1; x < im.size().x - 1; x++) |
| 22 |
{ |
| 23 |
const double xgrad = ((out[y - 1][x - 1] + 2 * out[y][x - 1] + out[y + 1][x - 1]) - (out[y - 1][x + 1] + 2 * out[y][x + 1] + out[y + 1][x + 1])); |
| 24 |
const double ygrad = ((out[y - 1][x - 1] + 2 * out[y - 1][x] + out[y - 1][x + 1]) - (out[y + 1][x - 1] + 2 * out[y + 1][x] + out[y + 1][x + 1])); |
| 25 |
grad[y][x][0] = xgrad; |
| 26 |
grad[y][x][1] = ygrad; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
template <class T> |
| 32 |
void canny(const BasicImage<T>& im, BasicImage<T>& out, const double sigma = 1.0, const double lower_threshold = 0.1, const double upper_threshold = 0.2) |
| 33 |
{ |
| 34 |
|
| 35 |
const int STRONG_EDGE = 4; |
| 36 |
const int WEAK_EDGE = 5; |
| 37 |
const int REJECT_EDGE = 6; |
| 38 |
const double pi = std::atan(1.0) * 4; |
| 39 |
Image<double> mags(out.size()); |
| 40 |
Image<byte> bins(out.size()); |
| 41 |
convolveGaussian(im, out, sigma); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
for(int y = 1; y < im.size().y - 1; y++) |
| 54 |
{ |
| 55 |
for(int x = 1; x < im.size().x - 1; x++) |
| 56 |
{ |
| 57 |
const double ygrad = ((out[y - 1][x - 1] + 2 * out[y - 1][x] + out[y - 1][x + 1]) - (out[y + 1][x - 1] + 2 * out[y + 1][x] + out[y + 1][x + 1])); |
| 58 |
const double xgrad = ((out[y - 1][x - 1] + 2 * out[y][x - 1] + out[y + 1][x - 1]) - (out[y - 1][x + 1] + 2 * out[y][x + 1] + out[y + 1][x + 1])); |
| 59 |
const double mag = std::sqrt(xgrad * xgrad + ygrad * ygrad); |
| 60 |
mags[y][x] = mag; |
| 61 |
if(mag >= lower_threshold) |
| 62 |
{ |
| 63 |
double dir = std::atan(ygrad / xgrad); |
| 64 |
if(dir < 0) |
| 65 |
{ |
| 66 |
dir += pi; |
| 67 |
} |
| 68 |
dir = (180.0 * dir) / pi; |
| 69 |
int dirbin = static_cast<int>((dir + 22.5) / 45.0) % 4; |
| 70 |
bins[y][x] = dirbin; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
bins[y][x] = REJECT_EDGE; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
std::queue<ImageRef> edge_locations; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
for(int y = 2; y < im.size().y - 2; y++) |
| 89 |
{ |
| 90 |
for(int x = 2; x < im.size().x - 2; x++) |
| 91 |
{ |
| 92 |
out[y][x] = 0.0; |
| 93 |
const double mag = mags[y][x]; |
| 94 |
|
| 95 |
|
| 96 |
if(bins[y][x] == REJECT_EDGE) |
| 97 |
{ |
| 98 |
continue; |
| 99 |
} |
| 100 |
bool edge = false; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
switch(bins[y][x]) |
| 106 |
{ |
| 107 |
case 0: |
| 108 |
if(mag > mags[y][x - 1] && mag > mags[y][x + 1]) |
| 109 |
{ |
| 110 |
edge = true; |
| 111 |
} |
| 112 |
break; |
| 113 |
case 1: |
| 114 |
if(mag > mags[y - 1][x - 1] && mag > mags[y + 1][x + 1]) |
| 115 |
{ |
| 116 |
edge = true; |
| 117 |
} |
| 118 |
break; |
| 119 |
case 2: |
| 120 |
if(mag > mags[y - 1][x] && mag > mags[y + 1][x]) |
| 121 |
{ |
| 122 |
edge = true; |
| 123 |
} |
| 124 |
break; |
| 125 |
case 3: |
| 126 |
if(mag > mags[y + 1][x - 1] && mag > mags[y - 1][x + 1]) |
| 127 |
{ |
| 128 |
edge = true; |
| 129 |
} |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
if(edge && mag > upper_threshold) |
| 137 |
{ |
| 138 |
bins[y][x] = STRONG_EDGE; |
| 139 |
edge_locations.push(ImageRef(x, y)); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
else if(edge && mag > lower_threshold) |
| 145 |
{ |
| 146 |
bins[y][x] = WEAK_EDGE; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
else |
| 152 |
{ |
| 153 |
bins[y][x] = REJECT_EDGE; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
while(edge_locations.size() > 0) |
| 161 |
{ |
| 162 |
|
| 163 |
ImageRef loc(edge_locations.front()); |
| 164 |
edge_locations.pop(); |
| 165 |
|
| 166 |
|
| 167 |
out[loc] = mags[loc]; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if(bins[loc.y][loc.x] == STRONG_EDGE) |
| 173 |
{ |
| 174 |
if(bins[loc.y - 1][loc.x - 1] == WEAK_EDGE) |
| 175 |
{ |
| 176 |
bins[loc.y - 1][loc.x - 1] = STRONG_EDGE; |
| 177 |
edge_locations.push(ImageRef(loc.x - 1, loc.y - 1)); |
| 178 |
} |
| 179 |
if(bins[loc.y - 1][loc.x] == WEAK_EDGE) |
| 180 |
{ |
| 181 |
bins[loc.y - 1][loc.x] = STRONG_EDGE; |
| 182 |
edge_locations.push(ImageRef(loc.x, loc.y - 1)); |
| 183 |
} |
| 184 |
if(bins[loc.y - 1][loc.x + 1] == WEAK_EDGE) |
| 185 |
{ |
| 186 |
bins[loc.y - 1][loc.x + 1] = STRONG_EDGE; |
| 187 |
edge_locations.push(ImageRef(loc.x + 1, loc.y - 1)); |
| 188 |
} |
| 189 |
if(bins[loc.y][loc.x - 1] == WEAK_EDGE) |
| 190 |
{ |
| 191 |
bins[loc.y][loc.x - 1] = STRONG_EDGE; |
| 192 |
edge_locations.push(ImageRef(loc.x - 1, loc.y)); |
| 193 |
} |
| 194 |
if(bins[loc.y][loc.x] == WEAK_EDGE) |
| 195 |
{ |
| 196 |
bins[loc.y][loc.x] = STRONG_EDGE; |
| 197 |
edge_locations.push(ImageRef(loc.x, loc.y)); |
| 198 |
} |
| 199 |
if(bins[loc.y][loc.x + 1] == WEAK_EDGE) |
| 200 |
{ |
| 201 |
bins[loc.y][loc.x + 1] = STRONG_EDGE; |
| 202 |
edge_locations.push(ImageRef(loc.x + 1, loc.y)); |
| 203 |
} |
| 204 |
if(bins[loc.y + 1][loc.x - 1] == WEAK_EDGE) |
| 205 |
{ |
| 206 |
bins[loc.y + 1][loc.x - 1] = STRONG_EDGE; |
| 207 |
edge_locations.push(ImageRef(loc.x - 1, loc.y + 1)); |
| 208 |
} |
| 209 |
if(bins[loc.y + 1][loc.x] == WEAK_EDGE) |
| 210 |
{ |
| 211 |
bins[loc.y + 1][loc.x] = STRONG_EDGE; |
| 212 |
edge_locations.push(ImageRef(loc.x, loc.y + 1)); |
| 213 |
} |
| 214 |
if(bins[loc.y + 1][loc.x + 1] == WEAK_EDGE) |
| 215 |
{ |
| 216 |
bins[loc.y + 1][loc.x + 1] = STRONG_EDGE; |
| 217 |
edge_locations.push(ImageRef(loc.x + 1, loc.y + 1)); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
template <class T> |
| 225 |
void canny2(const BasicImage<T>& im, BasicImage<T>& out, double sigma = 1.0, double lower_threshold = 0.1, double upper_threshold = 0.2) |
| 226 |
{ |
| 227 |
|
| 228 |
const int STRONG_EDGE = 4; |
| 229 |
const int WEAK_EDGE = 5; |
| 230 |
const int REJECT_EDGE = 6; |
| 231 |
Image<double> mags(out.size()); |
| 232 |
Image<byte> bins(out.size()); |
| 233 |
convolveGaussian(im, out, sigma); |
| 234 |
lower_threshold = lower_threshold * lower_threshold; |
| 235 |
upper_threshold = upper_threshold * upper_threshold; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
for(int y = 1; y < im.size().y - 1; y++) |
| 248 |
{ |
| 249 |
for(int x = 1; x < im.size().x - 1; x++) |
| 250 |
{ |
| 251 |
const double ygrad = ((out[y - 1][x - 1] + 2 * out[y - 1][x] + out[y - 1][x + 1]) - (out[y + 1][x - 1] + 2 * out[y + 1][x] + out[y + 1][x + 1])); |
| 252 |
const double xgrad = ((out[y - 1][x - 1] + 2 * out[y][x - 1] + out[y + 1][x - 1]) - (out[y - 1][x + 1] + 2 * out[y][x + 1] + out[y + 1][x + 1])); |
| 253 |
const double axgrad = std::abs(xgrad); |
| 254 |
const double aygrad = std::abs(ygrad); |
| 255 |
|
| 256 |
const double mag = xgrad * xgrad + ygrad * ygrad; |
| 257 |
mags[y][x] = mag; |
| 258 |
if(mag >= lower_threshold) |
| 259 |
{ |
| 260 |
const bool xgrad_pos = xgrad >= 0; |
| 261 |
const bool ygrad_pos = ygrad >= 0; |
| 262 |
const bool xgrad_neg = xgrad <= 0; |
| 263 |
const bool ygrad_neg = ygrad <= 0; |
| 264 |
const bool axgrad_sup = axgrad >= aygrad; |
| 265 |
const bool aygrad_sup = axgrad <= aygrad; |
| 266 |
if((xgrad_pos && ygrad_pos && axgrad_sup) |
| 267 |
|| (xgrad_neg && ygrad_neg && axgrad_sup)) |
| 268 |
{ |
| 269 |
bins[y][x] = 0; |
| 270 |
} |
| 271 |
else if((xgrad_pos && ygrad_pos && aygrad_sup) |
| 272 |
|| (xgrad_neg && ygrad_neg && aygrad_sup)) |
| 273 |
{ |
| 274 |
bins[y][x] = 1; |
| 275 |
} |
| 276 |
else if((xgrad_neg && ygrad_pos && aygrad_sup) |
| 277 |
|| (xgrad_pos && ygrad_neg && aygrad_sup)) |
| 278 |
{ |
| 279 |
bins[y][x] = 2; |
| 280 |
} |
| 281 |
else if((xgrad_neg && ygrad_pos && axgrad_sup) |
| 282 |
|| (xgrad_pos && ygrad_neg && axgrad_sup)) |
| 283 |
{ |
| 284 |
bins[y][x] = 3; |
| 285 |
} |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
bins[y][x] = REJECT_EDGE; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
std::queue<ImageRef> edge_locations; |
| 300 |
|
| 301 |
for(int y = 2; y < im.size().y - 2; y++) |
| 302 |
{ |
| 303 |
out[y][0] = 0; |
| 304 |
out[y][1] = 0; |
| 305 |
out[y][im.size().x - 2] = 0; |
| 306 |
out[y][im.size().x - 1] = 0; |
| 307 |
} |
| 308 |
|
| 309 |
for(int x = 1; x < im.size().x - 1; x++) |
| 310 |
{ |
| 311 |
out[0][x] = 0; |
| 312 |
out[1][x] = 0; |
| 313 |
out[im.size().y - 2][x] = 0; |
| 314 |
out[im.size().y - 1][x] = 0; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
for(int y = 2; y < im.size().y - 2; y++) |
| 320 |
{ |
| 321 |
for(int x = 2; x < im.size().x - 2; x++) |
| 322 |
{ |
| 323 |
out[y][x] = 0.0; |
| 324 |
const double mag = mags[y][x]; |
| 325 |
|
| 326 |
if(bins[y][x] == REJECT_EDGE) |
| 327 |
{ |
| 328 |
continue; |
| 329 |
} |
| 330 |
bool edge = false; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
switch(bins[y][x]) |
| 336 |
{ |
| 337 |
case 0: |
| 338 |
if(mag > mags[y][x - 1] && mag > mags[y][x + 1]) |
| 339 |
{ |
| 340 |
edge = true; |
| 341 |
} |
| 342 |
break; |
| 343 |
case 1: |
| 344 |
if(mag > mags[y - 1][x - 1] && mag > mags[y + 1][x + 1]) |
| 345 |
{ |
| 346 |
edge = true; |
| 347 |
} |
| 348 |
break; |
| 349 |
case 2: |
| 350 |
if(mag > mags[y - 1][x] && mag > mags[y + 1][x]) |
| 351 |
{ |
| 352 |
edge = true; |
| 353 |
} |
| 354 |
break; |
| 355 |
case 3: |
| 356 |
if(mag > mags[y + 1][x - 1] && mag > mags[y - 1][x + 1]) |
| 357 |
{ |
| 358 |
edge = true; |
| 359 |
} |
| 360 |
break; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if(edge && mag > upper_threshold) |
| 367 |
{ |
| 368 |
bins[y][x] = STRONG_EDGE; |
| 369 |
edge_locations.push(ImageRef(x, y)); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
else if(edge && mag > lower_threshold) |
| 375 |
{ |
| 376 |
bins[y][x] = WEAK_EDGE; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
else |
| 382 |
{ |
| 383 |
bins[y][x] = REJECT_EDGE; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
while(edge_locations.size() > 0) |
| 391 |
{ |
| 392 |
|
| 393 |
ImageRef loc(edge_locations.front()); |
| 394 |
edge_locations.pop(); |
| 395 |
|
| 396 |
|
| 397 |
out[loc] = mags[loc]; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
if(bins[loc.y][loc.x] == STRONG_EDGE) |
| 403 |
{ |
| 404 |
if(bins[loc.y - 1][loc.x - 1] == WEAK_EDGE) |
| 405 |
{ |
| 406 |
bins[loc.y - 1][loc.x - 1] = STRONG_EDGE; |
| 407 |
edge_locations.push(ImageRef(loc.x - 1, loc.y - 1)); |
| 408 |
} |
| 409 |
if(bins[loc.y - 1][loc.x] == WEAK_EDGE) |
| 410 |
{ |
| 411 |
bins[loc.y - 1][loc.x] = STRONG_EDGE; |
| 412 |
edge_locations.push(ImageRef(loc.x, loc.y - 1)); |
| 413 |
} |
| 414 |
if(bins[loc.y - 1][loc.x + 1] == WEAK_EDGE) |
| 415 |
{ |
| 416 |
bins[loc.y - 1][loc.x + 1] = STRONG_EDGE; |
| 417 |
edge_locations.push(ImageRef(loc.x + 1, loc.y - 1)); |
| 418 |
} |
| 419 |
if(bins[loc.y][loc.x - 1] == WEAK_EDGE) |
| 420 |
{ |
| 421 |
bins[loc.y][loc.x - 1] = STRONG_EDGE; |
| 422 |
edge_locations.push(ImageRef(loc.x - 1, loc.y)); |
| 423 |
} |
| 424 |
if(bins[loc.y][loc.x] == WEAK_EDGE) |
| 425 |
{ |
| 426 |
bins[loc.y][loc.x] = STRONG_EDGE; |
| 427 |
edge_locations.push(ImageRef(loc.x, loc.y)); |
| 428 |
} |
| 429 |
if(bins[loc.y][loc.x + 1] == WEAK_EDGE) |
| 430 |
{ |
| 431 |
bins[loc.y][loc.x + 1] = STRONG_EDGE; |
| 432 |
edge_locations.push(ImageRef(loc.x + 1, loc.y)); |
| 433 |
} |
| 434 |
if(bins[loc.y + 1][loc.x - 1] == WEAK_EDGE) |
| 435 |
{ |
| 436 |
bins[loc.y + 1][loc.x - 1] = STRONG_EDGE; |
| 437 |
edge_locations.push(ImageRef(loc.x - 1, loc.y + 1)); |
| 438 |
} |
| 439 |
if(bins[loc.y + 1][loc.x] == WEAK_EDGE) |
| 440 |
{ |
| 441 |
bins[loc.y + 1][loc.x] = STRONG_EDGE; |
| 442 |
edge_locations.push(ImageRef(loc.x, loc.y + 1)); |
| 443 |
} |
| 444 |
if(bins[loc.y + 1][loc.x + 1] == WEAK_EDGE) |
| 445 |
{ |
| 446 |
bins[loc.y + 1][loc.x + 1] = STRONG_EDGE; |
| 447 |
edge_locations.push(ImageRef(loc.x + 1, loc.y + 1)); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
template <class T> |
| 460 |
void compute_gradient_direction_at(const BasicImage<T>& in, const BasicImage<T>& mask, const double threshold, |
| 461 |
BasicImage<T>& grad_dir_map) |
| 462 |
{ |
| 463 |
zero_border(grad_dir_map); |
| 464 |
for(int y = 1; y < in.size().y - 1; y++) |
| 465 |
{ |
| 466 |
for(int x = 1; x < in.size().x - 1; x++) |
| 467 |
{ |
| 468 |
if(mask[y][x] > threshold) |
| 469 |
{ |
| 470 |
const double ygrad = ((in[y - 1][x - 1] + 2 * in[y - 1][x] + in[y - 1][x + 1]) - (in[y + 1][x - 1] + 2 * in[y + 1][x] + in[y + 1][x + 1])); |
| 471 |
const double xgrad = ((in[y - 1][x - 1] + 2 * in[y][x - 1] + in[y + 1][x - 1]) - (in[y - 1][x + 1] + 2 * in[y][x + 1] + in[y + 1][x + 1])); |
| 472 |
double dir = std::atan2(xgrad, ygrad); |
| 473 |
grad_dir_map[y][x] = dir; |
| 474 |
} |
| 475 |
else |
| 476 |
{ |
| 477 |
grad_dir_map[y][x] = 0.0; |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
#endif |