computer_vision/fast.cpp
/*******************************************************
* Copyright (c) 2015, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* https://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <cstdio>
#include <cstdlib>
using namespace af;
static void fast_demo(bool console) {
// Load image
array img_color;
if (console)
img_color = loadImage(ASSETS_DIR "/examples/images/square.png", true);
else
img_color = loadImage(ASSETS_DIR "/examples/images/man.jpg", true);
// Convert the image from RGB to gray-scale
array img = colorSpace(img_color, AF_GRAY, AF_RGB);
// For visualization in ArrayFire, color images must be in the [0.0f-1.0f]
// interval
img_color /= 255.f;
features feat = fast(img, 20.0f, 9, true, 0.05);
float* h_x = feat.getX().host<float>();
float* h_y = feat.getY().host<float>();
// Draw draw_len x draw_len crosshairs where the corners are
const int draw_len = 3;
for (size_t f = 0; f < feat.getNumFeatures(); f++) {
int x = h_x[f];
int y = h_y[f];
img_color(y, seq(x - draw_len, x + draw_len), 0) = 0.f;
img_color(y, seq(x - draw_len, x + draw_len), 1) = 1.f;
img_color(y, seq(x - draw_len, x + draw_len), 2) = 0.f;
// Draw vertical line of (draw_len * 2 + 1) pixels centered on the
// corner Set only the first channel to 1 (green lines)
img_color(seq(y - draw_len, y + draw_len), x, 0) = 0.f;
img_color(seq(y - draw_len, y + draw_len), x, 1) = 1.f;
img_color(seq(y - draw_len, y + draw_len), x, 2) = 0.f;
}
freeHost(h_x);
freeHost(h_y);
printf("Features found: %zu\n", feat.getNumFeatures());
if (!console) {
af::Window wnd("FAST Feature Detector");
// Previews color image with green crosshairs
while (!wnd.close()) wnd.image(img_color);
} else {
af_print(feat.getX());
af_print(feat.getY());
}
}
int main(int argc, char** argv) {
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
try {
af::setDevice(device);
printf("** ArrayFire FAST Feature Detector Demo **\n\n");
fast_demo(console);
} catch (af::exception& ae) {
fprintf(stderr, "%s\n", ae.what());
throw;
}
return 0;
}
af::Window::image
void image(const array &in, const char *title=NULL)
Renders the input array as an image to the window.
af::seq
seq is used to create sequences for indexing af::array
Definition: seq.h:46
af::array::host
T * host() const
Copy array data to host and return host pointer.
af::info
AFAPI void info()
af::setDevice
AFAPI void setDevice(const int device)
Sets the current device.
af::array
A multi dimensional data container.
Definition: array.h:35
af
Definition: algorithm.h:15
af_print
#define af_print(...)
Definition: util.h:148
af::Window::close
bool close()
Check if window is marked for close.
af::features::getY
array getY() const
Returns an af::array which represents the y locations of a feature.
af::features::getNumFeatures
size_t getNumFeatures() const
Returns the number of features represented by this object.
af::exception
An ArrayFire exception class.
Definition: exception.h:29
af::freeHost
AFAPI void freeHost(const void *ptr)
Free memory allocated internally by ArrayFire.
AF_RGB
@ AF_RGB
3-channel RGB
Definition: defines.h:341
af::colorSpace
AFAPI array colorSpace(const array &image, const CSpace to, const CSpace from)
C++ Interface wrapper for colorspace conversion.
af::fast
AFAPI features fast(const array &in, const float thr=20.0f, const unsigned arc_length=9, const bool non_max=true, const float feature_ratio=0.05f, const unsigned edge=3)
C++ Interface for FAST feature detector.
af::features
Represents a feature returned by a feature detector.
Definition: features.h:24
af::loadImage
AFAPI array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
arrayfire.h
af::features::getX
array getX() const
Returns an af::array which represents the x locations of a feature.
AF_GRAY
@ AF_GRAY
Grayscale.
Definition: defines.h:340
af::exception::what
virtual const char * what() const
Returns an error message for the exception in a string format.
Definition: exception.h:60
af::Window
Window object to render af::arrays.
Definition: graphics.h:37