|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2015, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <cstdio> |
| 11 | +#include <arrayfire.h> |
| 12 | +#include <cstdlib> |
| 13 | + |
| 14 | +using namespace af; |
| 15 | + |
| 16 | +static void fast_demo(bool console) |
| 17 | +{ |
| 18 | + // Load image |
| 19 | + array img_color; |
| 20 | + if (console) |
| 21 | + img_color = loadImage(ASSETS_DIR "/examples/images/square.png", true); |
| 22 | + else |
| 23 | + img_color = loadImage(ASSETS_DIR "/examples/images/lena.ppm", true); |
| 24 | + // Convert the image from RGB to gray-scale |
| 25 | + array img = colorSpace(img_color, AF_GRAY, AF_RGB); |
| 26 | + // For visualization in ArrayFire, color images must be in the [0.0f-1.0f] interval |
| 27 | + img_color /= 255.f; |
| 28 | + |
| 29 | + features feat = fast(img, 20.0f, 9, true, 0.05); |
| 30 | + |
| 31 | + float* h_x = feat.getX().host<float>(); |
| 32 | + float* h_y = feat.getY().host<float>(); |
| 33 | + |
| 34 | + // Draw draw_len x draw_len crosshairs where the corners are |
| 35 | + const int draw_len = 3; |
| 36 | + for (size_t f = 0; f < feat.getNumFeatures(); f++) { |
| 37 | + int x = h_x[f]; |
| 38 | + int y = h_y[f]; |
| 39 | + img_color(y, seq(x-draw_len, x+draw_len), 0) = 0.f; |
| 40 | + img_color(y, seq(x-draw_len, x+draw_len), 1) = 1.f; |
| 41 | + img_color(y, seq(x-draw_len, x+draw_len), 2) = 0.f; |
| 42 | + |
| 43 | + // Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner |
| 44 | + // Set only the first channel to 1 (green lines) |
| 45 | + img_color(seq(y-draw_len, y+draw_len), x, 0) = 0.f; |
| 46 | + img_color(seq(y-draw_len, y+draw_len), x, 1) = 1.f; |
| 47 | + img_color(seq(y-draw_len, y+draw_len), x, 2) = 0.f; |
| 48 | + } |
| 49 | + |
| 50 | + printf("Features found: %lu\n", feat.getNumFeatures()); |
| 51 | + |
| 52 | + if (!console) { |
| 53 | + af::Window wnd("FAST Feature Detector"); |
| 54 | + |
| 55 | + // Previews color image with green crosshairs |
| 56 | + while(!wnd.close()) |
| 57 | + wnd.image(img_color); |
| 58 | + } else { |
| 59 | + af_print(feat.getX()); |
| 60 | + af_print(feat.getY()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +int main(int argc, char** argv) |
| 65 | +{ |
| 66 | + int device = argc > 1 ? atoi(argv[1]) : 0; |
| 67 | + bool console = argc > 2 ? argv[2][0] == '-' : false; |
| 68 | + |
| 69 | + try { |
| 70 | + af::setDevice(device); |
| 71 | + af::info(); |
| 72 | + std::cout << "** ArrayFire FAST Feature Detector Demo **" << std::endl << std::endl; |
| 73 | + fast_demo(console); |
| 74 | + |
| 75 | + } catch (af::exception& ae) { |
| 76 | + std::cout << ae.what() << std::endl; |
| 77 | + throw; |
| 78 | + } |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments