From 9178dc9d384f18ab2f23656fb6b01a3b444526ef Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Tue, 8 Oct 2013 19:53:59 +0600 Subject: Rework detector API and implement Harris detector Switch the detector API to a single function which accepts a float image and detector options. This makes usage of feature detection more unified across different algorithms. Options structure is pretty much straightforward and contains detector to be used and all the detector-specific settings. Also implemented Harris feature detection algorithm which is not as fast as FAST one but is expected to detect more robust feature points. It is also likely that less features are detected, but better quality than quantity. Blender will now use Harris detector by default, later we'll remove FAST detector. --- source/blender/blenkernel/intern/tracking_detect.c | 77 ++++++++++++---------- 1 file changed, 41 insertions(+), 36 deletions(-) (limited to 'source/blender/blenkernel/intern/tracking_detect.c') diff --git a/source/blender/blenkernel/intern/tracking_detect.c b/source/blender/blenkernel/intern/tracking_detect.c index b262844d40f..65fd997ae6c 100644 --- a/source/blender/blenkernel/intern/tracking_detect.c +++ b/source/blender/blenkernel/intern/tracking_detect.c @@ -113,8 +113,11 @@ static void detect_retrieve_libmv_features(MovieTracking *tracking, ListBase *tr libmv_getFeature(features, a, &x, &y, &score, &size); - xu = x / width; - yu = y / height; + /* In Libmv integer coordinate points to pixel center, in blender + * it's not. Need to add 0.5px offset to center. + */ + xu = (x + 0.5f) / width; + yu = (y + 0.5f) / height; if (layer) ok = check_point_in_layer(layer, xu, yu) != place_outside_layer; @@ -128,36 +131,26 @@ static void detect_retrieve_libmv_features(MovieTracking *tracking, ListBase *tr } } -/* Get a gray-scale unsigned char buffer from given image buffer - * wich will be used for feature detection. - */ -static unsigned char *detect_get_frame_ucharbuf(ImBuf *ibuf) +static void run_configured_detector(MovieTracking *tracking, ListBase *tracksbase, + ImBuf *ibuf, int framenr, bGPDlayer *layer, bool place_outside_layer, + libmv_DetectOptions *options) { - int x, y; - unsigned char *pixels, *cp; - - cp = pixels = MEM_callocN(ibuf->x * ibuf->y * sizeof(unsigned char), "tracking ucharBuf"); - for (y = 0; y < ibuf->y; y++) { - for (x = 0; x < ibuf->x; x++) { - int pixel = ibuf->x * y + x; - - if (ibuf->rect_float) { - const float *rrgbf = ibuf->rect_float + pixel * 4; - const float gray_f = 0.2126f * rrgbf[0] + 0.7152f * rrgbf[1] + 0.0722f * rrgbf[2]; + struct libmv_Features *features = NULL; - *cp = FTOCHAR(gray_f); - } - else { - const unsigned char *rrgb = (unsigned char *)ibuf->rect + pixel * 4; + if (ibuf->rect_float) { + features = libmv_detectFeaturesFloat(ibuf->rect_float, ibuf->x, ibuf->y, 4, options); + } + else if (ibuf->rect) { + features = libmv_detectFeaturesByte((unsigned char *) ibuf->rect, ibuf->x, ibuf->y, 4, options); + } - *cp = 0.2126f * rrgb[0] + 0.7152f * rrgb[1] + 0.0722f * rrgb[2]; - } + if (features != NULL) { + detect_retrieve_libmv_features(tracking, tracksbase, features, + framenr, ibuf->x, ibuf->y, layer, + place_outside_layer); - cp++; - } + libmv_featuresDestroy(features); } - - return pixels; } /* Detect features using FAST detector */ @@ -165,17 +158,29 @@ void BKE_tracking_detect_fast(MovieTracking *tracking, ListBase *tracksbase, ImB int framenr, int margin, int min_trackness, int min_distance, bGPDlayer *layer, bool place_outside_layer) { - struct libmv_Features *features; - unsigned char *pixels = detect_get_frame_ucharbuf(ibuf); + libmv_DetectOptions options = {0}; - features = libmv_detectFeaturesFAST(pixels, ibuf->x, ibuf->y, ibuf->x, - margin, min_trackness, min_distance); + options.detector = LIBMV_DETECTOR_FAST; + options.margin = margin; + options.min_distance = min_distance; + options.fast_min_trackness = min_trackness; - MEM_freeN(pixels); + run_configured_detector(tracking, tracksbase, ibuf, framenr, layer, + place_outside_layer, &options); +} + +/* Detect features using Harris detector */ +void BKE_tracking_detect_harris(MovieTracking *tracking, ListBase *tracksbase, ImBuf *ibuf, + int framenr, int margin, float threshold, int min_distance, bGPDlayer *layer, + bool place_outside_layer) +{ + libmv_DetectOptions options = {0}; - detect_retrieve_libmv_features(tracking, tracksbase, features, - framenr, ibuf->x, ibuf->y, layer, - place_outside_layer); + options.detector = LIBMV_DETECTOR_HARRIS; + options.margin = margin; + options.min_distance = min_distance; + options.harris_threshold = threshold; - libmv_featuresDestroy(features); + run_configured_detector(tracking, tracksbase, ibuf, framenr, layer, + place_outside_layer, &options); } -- cgit v1.2.3