double SIFT_ADAPTER::getMagnif() const { if (!m_has_extracted) { if (std::abs(m_magnif - DEFAULT_MAGNIF_INVALID) < __SIFT_ADAPTER_EPS) return DEFAULT_MAGNIF; else return m_magnif; } return vl_sift_get_magnif(m_sift_model); }
void Sift::extract(Image &image, std::vector<Feature> *list) { float *fdata = new float[image.cv->width * image.cv->height]; BOOST_VERIFY(fdata); { float *d = fdata; unsigned char *l = (unsigned char *)image.cv->imageData; for (int h = 0; h < image.cv->height; ++h) { for (int w = 0; w < image.cv->width; ++w) { *d = float(l[w]); d++; } l += image.cv->widthStep; } } VlSiftFilt *filt = vl_sift_new (image.cv->width, image.cv->height, O, S, omin) ; BOOST_VERIFY(filt); if (edge_thresh >= 0) vl_sift_set_edge_thresh (filt, edge_thresh) ; if (peak_thresh >= 0) vl_sift_set_peak_thresh (filt, peak_thresh) ; if (magnif >= 0) vl_sift_set_magnif (filt, magnif) ; magnif = vl_sift_get_magnif(filt); list->clear(); int err = vl_sift_process_first_octave (filt, fdata); while (err == 0) { vl_sift_detect (filt) ; VlSiftKeypoint const *keys = vl_sift_get_keypoints(filt) ; int nkeys = vl_sift_get_nkeypoints(filt) ; for (int i = 0; i < nkeys ; ++i) { VlSiftKeypoint const *key = keys + i; double angles [4] ; int nangles = 0; if (do_angle) { nangles = vl_sift_calc_keypoint_orientations(filt, angles, key) ; } else { nangles = 1; angles[0] = 0; } for (int q = 0 ; q < nangles ; ++q) { list->push_back(Feature()); Feature &f = list->back(); f.desc.resize(DIM); /* compute descriptor (if necessary) */ vl_sift_calc_keypoint_descriptor(filt, &f.desc[0], key, angles[q]) ; BOOST_FOREACH(float &v, f.desc) { /* v = round(v * SIFT_RANGE); if (v > SIFT_RANGE) v = SIFT_RANGE; */ v *= 2; if (v > 1.0) v = 1.0; } f.scale = image.getScale(); f.x = key->x; f.y = key->y; f.dir = angles[q] / M_PI / 2; f.size = key->sigma * magnif; if ((entropy(f.desc) < e_th) || !checkBlackList(f.desc)) { list->pop_back(); } } } err = vl_sift_process_next_octave (filt) ; } vl_sift_delete (filt) ; delete [] fdata; }
void mexFunction(int nout, mxArray *out[], int nin, const mxArray *in[]) { enum {IN_GRAD=0,IN_FRAMES,IN_END} ; enum {OUT_DESCRIPTORS} ; int verbose = 0 ; int opt ; int next = IN_END ; mxArray const *optarg ; mxArray *grad_array ; vl_sift_pix *grad ; int M, N ; vl_bool floatDescriptors = 0 ; double magnif = -1 ; double *ikeys = 0 ; int nikeys = 0 ; int i,j ; VL_USE_MATLAB_ENV ; /* ----------------------------------------------------------------- * Check the arguments * -------------------------------------------------------------- */ if (nin < 2) { mexErrMsgTxt("Two arguments required.") ; } else if (nout > 1) { mexErrMsgTxt("Too many output arguments."); } if (mxGetNumberOfDimensions (in[IN_GRAD]) != 3 || mxGetClassID (in[IN_GRAD]) != mxSINGLE_CLASS || mxGetDimensions (in[IN_GRAD])[0] != 2 ) { mexErrMsgTxt("GRAD must be a 2xMxN matrix of class SINGLE.") ; } if (!vlmxIsMatrix(in[IN_FRAMES], 4, -1)) { mexErrMsgTxt("FRAMES must be a 4xN matrix.") ; } nikeys = mxGetN (in[IN_FRAMES]) ; ikeys = mxGetPr(in[IN_FRAMES]) ; while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) { switch (opt) { case opt_verbose : ++ verbose ; break ; case opt_magnif : if (!vlmxIsPlainScalar(optarg) || (magnif = *mxGetPr(optarg)) < 0) { mexErrMsgTxt("MAGNIF must be a non-negative scalar.") ; } break ; case opt_float_descriptors : floatDescriptors = 1 ; break ; default : abort() ; } } grad_array = mxDuplicateArray(in[IN_GRAD]) ; grad = (vl_sift_pix*) mxGetData (grad_array) ; M = mxGetDimensions(in[IN_GRAD])[1] ; N = mxGetDimensions(in[IN_GRAD])[2] ; /* transpose angles */ for (i = 1 ; i < 2*M*N ; i+=2) { grad [i] = VL_PI/2 - grad [i] ; } /* ----------------------------------------------------------------- * Do job * -------------------------------------------------------------- */ { VlSiftFilt * filt = 0 ; void * descr = 0 ; /* create a filter to process the image */ filt = vl_sift_new (M, N, -1, -1, 0) ; if (magnif >= 0) vl_sift_set_magnif (filt, magnif) ; if (verbose) { mexPrintf("vl_siftdescriptor: filter settings:\n") ; mexPrintf("vl_siftdescriptor: magnif = %g\n", vl_sift_get_magnif (filt)) ; mexPrintf("vl_siftdescriptor: num of frames = %d\n", nikeys) ; mexPrintf("vl_siftdescriptor: float descriptor = %d\n", floatDescriptors) ; } { mwSize dims [2] ; dims [0] = 128 ; dims [1] = nikeys ; out[OUT_DESCRIPTORS]= mxCreateNumericArray (2, dims, floatDescriptors ? mxSINGLE_CLASS : mxUINT8_CLASS, mxREAL) ; descr = mxGetData(out[OUT_DESCRIPTORS]) ; } /* ............................................................... * Process each octave * ............................................................ */ for (i = 0 ; i < nikeys ; ++i) { vl_sift_pix buf [128], rbuf [128] ; double y = *ikeys++ - 1 ; double x = *ikeys++ - 1 ; double s = *ikeys++ ; double th = VL_PI / 2 - *ikeys++ ; vl_sift_calc_raw_descriptor (filt, grad, buf, M, N, x, y, s, th) ; transpose_descriptor (rbuf, buf) ; if (! floatDescriptors) { vl_uint8 * descr_ = descr ; for (j = 0 ; j < 128 ; ++j) { float x = 512.0F * rbuf [j] ; x = (x < 255.0F) ? x : 255.0F ; *descr_++ = (vl_uint8) (x) ; } descr = descr_ ; } else { float * descr_ = descr ; for (j = 0 ; j < 128 ; ++j) { *descr_++ = 512.0F * rbuf [j] ; } descr = descr_ ; } } /* cleanup */ mxDestroyArray (grad_array) ; vl_sift_delete (filt) ; } /* job done */ }
/** @brief SIFT driver entry point **/ int Sift::Detect() { /* algorithm parameters */ double edge_thresh = -1 ; double peak_thresh = -1 ; double magnif = -1 ; int O = -1, S = 3, omin = -1 ; vl_bool err = VL_ERR_OK ; vl_bool force_orientations = 0 ; VlSiftFilt *filt = 0 ; vl_size q ; int i ; vl_bool first ; TimeMeasureBase& measure = *TimeMeasureBase::getInstance(); measure.startTimer("Sift::Detect()"); #define WERR(name,op) \ if (err == VL_ERR_OVERFLOW) { \ snprintf(err_msg, sizeof(err_msg), \ "Output file name too long.") ; \ goto done ; \ } else if (err) { \ snprintf(err_msg, sizeof(err_msg), \ "Could not open '%s' for " #op, name) ; \ goto done ; \ } /* ............................................................... * Make filter * ............................................................ */ filt = vl_sift_new (pim.width, pim.height, O, S, omin) ; Logger::debug(Logger::SIFT, "new filter(vl_sift_new) created:%x", filt) ; if (edge_thresh >= 0) vl_sift_set_edge_thresh (filt, edge_thresh) ; if (peak_thresh >= 0) vl_sift_set_peak_thresh (filt, peak_thresh) ; if (magnif >= 0) vl_sift_set_magnif (filt, magnif) ; if (!filt) { Logger::error(Logger::SIFT, "Detect: could not create SIFT-fiter."); throw SiftException("could not create SIFT-fiter."); } Logger::debug(Logger::SIFT, "sift: filter settings:") ; Logger::debug(Logger::SIFT, "sift: octaves (O) = %d", vl_sift_get_noctaves (filt)) ; Logger::debug(Logger::SIFT, "sift: levels (S) = %d", vl_sift_get_nlevels (filt)) ; Logger::debug(Logger::SIFT, "sift: first octave (o_min) = %d", vl_sift_get_octave_first (filt)) ; Logger::debug(Logger::SIFT, "sift: edge thresh = %g", vl_sift_get_edge_thresh (filt)) ; Logger::debug(Logger::SIFT, "sift: peak thresh = %g", vl_sift_get_peak_thresh (filt)) ; Logger::debug(Logger::SIFT, "sift: magnif = %g", vl_sift_get_magnif (filt)) ; Logger::debug(Logger::SIFT, "sift: will force orientations? %s", force_orientations ? "yes" : "no") ; /* ............................................................... * Process each octave * ............................................................ */ i = 0 ; first = 1 ; while (1) { VlSiftKeypoint const *keys = 0 ; int nkeys ; Logger::debug(Logger::SIFT, "sift: computing octave"); /* calculate the GSS for the next octave .................... */ measure.startTimer("process_octave"); if (first) { measure.startTimer("process_f_octave"); first = 0 ; err = vl_sift_process_first_octave(filt, fdata) ; measure.stopTimer("process_f_octave"); } else { measure.startTimer("process_n_octave"); err = vl_sift_process_next_octave(filt); measure.stopTimer("process_n_octave"); } measure.stopTimer("process_octave"); if (err) { err = VL_ERR_OK ; break ; } Logger::debug(Logger::SIFT, "sift: GSS octave %d computed", vl_sift_get_octave_index (filt)); /* run detector ............................................. */ measure.startTimer("sift_detect"); vl_sift_detect (filt) ; measure.stopTimer("sift_detect"); keys = vl_sift_get_keypoints(filt) ; nkeys = vl_sift_get_nkeypoints(filt) ; i = 0 ; Logger::debug(Logger::SIFT, "sift: detected %d (unoriented) keypoints", nkeys) ; /* for each keypoint ........................................ */ for (; i < nkeys ; ++i) { measure.startTimer("per_kpoint"); double angles [4] ; int nangles ; //VlSiftKeypoint ik ; VlSiftKeypoint const *k ; /* obtain keypoint orientations ........................... */ k = keys + i ; nangles = vl_sift_calc_keypoint_orientations (filt, angles, k) ; /* for each orientation ................................... */ for (q = 0 ; q < (unsigned) nangles ; ++q) { KeyPointDescriptor newKeyPoint; /* compute descriptor (if necessary) */ //if (out.active || dsc.active) { vl_sift_calc_keypoint_descriptor(filt, newKeyPoint.descr, k, angles [q]) ; //} newKeyPoint.keypoint = *k; newKeyPoint.angle = angles[q]; detected_keypoints.push_back(newKeyPoint); } measure.stopTimer("per_kpoint"); } } /* ............................................................... * Finish up * ............................................................ */ /* release filter */ if (filt) { Logger::debug(Logger::SIFT, "freeing filt: %x", filt); vl_sift_delete (filt) ; filt = 0 ; } measure.stopTimer("Sift::Detect()"); measure.printStatistic(); /* quit */ return 0; }
/** @brief SIFT driver entry point **/ int main(int argc, char **argv) { /* algorithm parameters */ double edge_thresh = -1 ; double peak_thresh = -1 ; double magnif = -1 ; int O = -1, S = 3, omin = -1 ; vl_bool err = VL_ERR_OK ; char err_msg [1024] ; int n ; int exit_code = 0 ; int verbose = 0 ; vl_bool force_output = 0 ; vl_bool force_orientations = 0 ; VlFileMeta out = {1, "%.sift", VL_PROT_ASCII, "", 0} ; VlFileMeta frm = {0, "%.frame", VL_PROT_ASCII, "", 0} ; VlFileMeta dsc = {0, "%.descr", VL_PROT_ASCII, "", 0} ; VlFileMeta met = {0, "%.meta", VL_PROT_ASCII, "", 0} ; VlFileMeta gss = {0, "%.pgm", VL_PROT_ASCII, "", 0} ; VlFileMeta ifr = {0, "%.frame", VL_PROT_ASCII, "", 0} ; #define ERRF(msg, arg) { \ err = VL_ERR_BAD_ARG ; \ snprintf(err_msg, sizeof(err_msg), msg, arg) ; \ break ; \ } #define ERR(msg) { \ err = VL_ERR_BAD_ARG ; \ snprintf(err_msg, sizeof(err_msg), msg) ; \ break ; \ } /* ----------------------------------------------------------------- * Parse options * -------------------------------------------------------------- */ while (!err) { int ch = getopt_long(argc, argv, opts, longopts, 0) ; /* If there are no files passed as input, print the help and settings */ if (ch == -1 && argc - optind == 0) ch = 'h'; /* end of option list? */ if (ch == -1) break; switch (ch) { case '?' : /* unkown option ............................................ */ ERRF("Invalid option '%s'.", argv [optind - 1]) ; break ; case ':' : /* missing argument ......................................... */ ERRF("Missing mandatory argument for option '%s'.", argv [optind - 1]) ; break ; case 'h' : /* --help ................................................... */ printf (help_message, argv [0]) ; printf ("SIFT filespec: `%s'\n", out.pattern) ; printf ("Frames filespec: `%s'\n", frm.pattern) ; printf ("Descriptors filespec: `%s'\n", dsc.pattern) ; printf ("Meta filespec: `%s'\n", met.pattern) ; printf ("GSS filespec: '%s'\n", gss.pattern) ; printf ("Read frames filespec: '%s'\n", ifr.pattern) ; printf ("Version: driver %s; libvl %s\n", VL_XSTRINGIFY(VL_SIFT_DRIVER_VERSION), vl_get_version_string()) ; exit (0) ; break ; case 'v' : /* --verbose ................................................ */ ++ verbose ; break ; case 'o' : /* --output ................................................ */ err = vl_file_meta_parse (&out, optarg) ; if (err) ERRF("The arguments of '%s' is invalid.", argv [optind - 1]) ; force_output = 1 ; break ; case opt_frames : /* --frames ................................................ */ err = vl_file_meta_parse (&frm, optarg) ; if (err) ERRF("The arguments of '%s' is invalid.", argv [optind - 1]) ; break ; case opt_descriptors : /* --descriptor ............................................. */ err = vl_file_meta_parse (&dsc, optarg) ; if (err) ERRF("The arguments of '%s' is invalid.", argv [optind - 1]) ; break; case opt_meta : /* --meta ................................................... */ err = vl_file_meta_parse (&met, optarg) ; if (err) ERRF("The arguments of '%s' is invalid.", argv [optind - 1]) ; if (met.protocol != VL_PROT_ASCII) ERR("meta file supports only ASCII protocol") ; break ; case opt_read_frames : /* --read_frames ............................................ */ err = vl_file_meta_parse (&ifr, optarg) ; if (err) ERRF("The arguments of '%s' is invalid.", argv [optind - 1]) ; break ; case opt_gss : /* --gss .................................................... */ err = vl_file_meta_parse (&gss, optarg) ; if (err) ERRF("The arguments of '%s' is invalid.", argv [optind - 1]) ; break ; case 'O' : /* --octaves ............................................... */ n = sscanf (optarg, "%d", &O) ; if (n == 0 || O < 0) ERRF("The argument of '%s' must be a non-negative integer.", argv [optind - 1]) ; break ; case 'S' : /* --levels ............................................... */ n = sscanf (optarg, "%d", &S) ; if (n == 0 || S < 0) ERRF("The argument of '%s' must be a non-negative integer.", argv [optind - 1]) ; break ; case opt_first_octave : /* --first-octave ......................................... */ n = sscanf (optarg, "%d", &omin) ; if (n == 0) ERRF("The argument of '%s' must be an integer.", argv [optind - 1]) ; break ; case opt_edge_thresh : /* --edge-thresh ........................................... */ n = sscanf (optarg, "%lf", &edge_thresh) ; if (n == 0 || edge_thresh < 1) ERRF("The argument of '%s' must be not smaller than 1.", argv [optind - 1]) ; break ; case opt_peak_thresh : /* --edge-thresh ........................................... */ n = sscanf (optarg, "%lf", &peak_thresh) ; if (n == 0 || peak_thresh < 0) ERRF("The argument of '%s' must be a non-negative float.", argv [optind - 1]) ; break ; case opt_magnif : /* --magnif .............................................. */ n = sscanf (optarg, "%lf", &magnif) ; if (n == 0 || magnif < 1) ERRF("The argument of '%s' must be a non-negative float.", argv [optind - 1]) ; break ; case opt_orientations : /* --orientations ......................................... */ force_orientations = 1 ; break ; case 0 : default : /* should not get here ...................................... */ assert (0) ; break ; } } /* check for parsing errors */ if (err) { fprintf(stderr, "%s: error: %s (%d)\n", argv [0], err_msg, err) ; exit (1) ; } /* parse other arguments (filenames) */ argc -= optind ; argv += optind ; /* if --output is not specified, specifying --frames or --descriptors prevent the aggregate outout file to be produced. */ if (! force_output && (frm.active || dsc.active)) { out.active = 0 ; } if (verbose > 1) { #define PRNFO(name,fm) \ printf("sift: " name) ; \ printf("%3s ", (fm).active ? "yes" : "no") ; \ printf("%-6s ", vl_string_protocol_name ((fm).protocol)) ; \ printf("%-10s\n", (fm).pattern) ; PRNFO("write aggregate . ", out) ; PRNFO("write frames .... ", frm) ; PRNFO("write descriptors ", dsc) ; PRNFO("write meta ...... ", met) ; PRNFO("write GSS ....... ", gss) ; PRNFO("read frames .... ", ifr) ; if (force_orientations) printf("sift: will compute orientations\n") ; } /* ------------------------------------------------------------------ * Process one image per time * --------------------------------------------------------------- */ while (argc--) { char basename [1024] ; char const *name = *argv++ ; FILE *in = 0 ; vl_uint8 *data = 0 ; vl_sift_pix *fdata = 0 ; VlPgmImage pim ; VlSiftFilt *filt = 0 ; int q, i ; vl_bool first ; double *ikeys = 0 ; int nikeys = 0, ikeys_size = 0 ; /* ............................................................... * Determine files * ............................................................ */ /* get basenmae from filename */ q = vl_string_basename (basename, sizeof(basename), name, 1) ; err = (q >= sizeof(basename)) ; if (err) { snprintf(err_msg, sizeof(err_msg), "Basename of '%s' is too long", name); err = VL_ERR_OVERFLOW ; goto done ; } if (verbose) { printf ("sift: <== '%s'\n", name) ; } if (verbose > 1) { printf ("sift: basename is '%s'\n", basename) ; } /* open input file */ in = fopen (name, "rb") ; if (!in) { err = VL_ERR_IO ; snprintf(err_msg, sizeof(err_msg), "Could not open '%s' for reading.", name) ; goto done ; } /* ............................................................... * Read data * ............................................................ */ /* read PGM header */ err = vl_pgm_extract_head (in, &pim) ; if (err) { switch (vl_err_no) { case VL_ERR_PGM_IO : snprintf(err_msg, sizeof(err_msg), "Cannot read from '%s'.", name) ; err = VL_ERR_IO ; break ; case VL_ERR_PGM_INV_HEAD : snprintf(err_msg, sizeof(err_msg), "'%s' contains a malformed PGM header.", name) ; err = VL_ERR_IO ; goto done ; } } if (verbose) printf ("sift: image is %d by %d pixels\n", pim. width, pim. height) ; /* allocate buffer */ data = malloc(vl_pgm_get_npixels (&pim) * vl_pgm_get_bpp (&pim) * sizeof (vl_uint8) ) ; fdata = malloc(vl_pgm_get_npixels (&pim) * vl_pgm_get_bpp (&pim) * sizeof (vl_sift_pix)) ; if (!data || !fdata) { err = VL_ERR_ALLOC ; snprintf(err_msg, sizeof(err_msg), "Could not allocate enough memory.") ; goto done ; } /* read PGM body */ err = vl_pgm_extract_data (in, &pim, data) ; if (err) { snprintf(err_msg, sizeof(err_msg), "PGM body malformed.") ; err = VL_ERR_IO ; goto done ; } /* convert data type */ for (q = 0 ; q < pim.width * pim.height ; ++q) fdata [q] = data [q] ; /* ............................................................... * Optionally source keypoints * ............................................................ */ #define WERR(name,op) \ if (err == VL_ERR_OVERFLOW) { \ snprintf(err_msg, sizeof(err_msg), \ "Output file name too long.") ; \ goto done ; \ } else if (err) { \ snprintf(err_msg, sizeof(err_msg), \ "Could not open '%s' for " #op, name) ; \ goto done ; \ } if (ifr.active) { /* open file */ err = vl_file_meta_open (&ifr, basename, "rb") ; WERR(ifr.name, reading) ; #define QERR \ if (err ) { \ snprintf (err_msg, sizeof(err_msg), \ "'%s' malformed", ifr.name) ; \ err = VL_ERR_IO ; \ goto done ; \ } while (1) { double x, y, s, th ; /* read next guy */ err = vl_file_meta_get_double (&ifr, &x) ; if (err == VL_ERR_EOF) break; else QERR ; err = vl_file_meta_get_double (&ifr, &y ) ; QERR ; err = vl_file_meta_get_double (&ifr, &s ) ; QERR ; err = vl_file_meta_get_double (&ifr, &th) ; if (err == VL_ERR_EOF) break; else QERR ; /* make enough space */ if (ikeys_size < nikeys + 1) { ikeys_size += 10000 ; ikeys = realloc (ikeys, 4 * sizeof(double) * ikeys_size) ; } /* add the guy to the buffer */ ikeys [4 * nikeys + 0] = x ; ikeys [4 * nikeys + 1] = y ; ikeys [4 * nikeys + 2] = s ; ikeys [4 * nikeys + 3] = th ; ++ nikeys ; } /* now order by scale */ qsort (ikeys, nikeys, 4 * sizeof(double), korder) ; if (verbose) { printf ("sift: read %d keypoints from '%s'\n", nikeys, ifr.name) ; } /* close file */ vl_file_meta_close (&ifr) ; } /* ............................................................... * Open output files * ............................................................ */ err = vl_file_meta_open (&out, basename, "wb") ; WERR(out.name, writing) ; err = vl_file_meta_open (&dsc, basename, "wb") ; WERR(dsc.name, writing) ; err = vl_file_meta_open (&frm, basename, "wb") ; WERR(frm.name, writing) ; err = vl_file_meta_open (&met, basename, "wb") ; WERR(met.name, writing) ; if (verbose > 1) { if (out.active) printf("sift: writing all ....... to . '%s'\n", out.name); if (frm.active) printf("sift: writing frames .... to . '%s'\n", frm.name); if (dsc.active) printf("sift: writing descriptors to . '%s'\n", dsc.name); if (met.active) printf("sift: writign meta ...... to . '%s'\n", met.name); } /* ............................................................... * Make filter * ............................................................ */ filt = vl_sift_new (pim.width, pim.height, O, S, omin) ; if (edge_thresh >= 0) vl_sift_set_edge_thresh (filt, edge_thresh) ; if (peak_thresh >= 0) vl_sift_set_peak_thresh (filt, peak_thresh) ; if (magnif >= 0) vl_sift_set_magnif (filt, magnif) ; if (!filt) { snprintf (err_msg, sizeof(err_msg), "Could not create SIFT filter.") ; err = VL_ERR_ALLOC ; goto done ; } if (verbose > 1) { printf ("sift: filter settings:\n") ; printf ("sift: octaves (O) = %d\n", vl_sift_get_noctaves (filt)) ; printf ("sift: levels (S) = %d\n", vl_sift_get_nlevels (filt)) ; printf ("sift: first octave (o_min) = %d\n", vl_sift_get_octave_first (filt)) ; printf ("sift: edge thresh = %g\n", vl_sift_get_edge_thresh (filt)) ; printf ("sift: peak thresh = %g\n", vl_sift_get_peak_thresh (filt)) ; printf ("sift: magnif = %g\n", vl_sift_get_magnif (filt)) ; printf ("sift: will source frames? %s\n", ikeys ? "yes" : "no") ; printf ("sift: will force orientations? %s\n", force_orientations ? "yes" : "no") ; } /* ............................................................... * Process each octave * ............................................................ */ i = 0 ; first = 1 ; while (1) { VlSiftKeypoint const *keys ; int nkeys ; /* calculate the GSS for the next octave .................... */ if (first) { first = 0 ; err = vl_sift_process_first_octave (filt, fdata) ; } else { err = vl_sift_process_next_octave (filt) ; } if (err) { err = VL_ERR_OK ; break ; } if (verbose > 1) { printf("sift: GSS octave %d computed\n", vl_sift_get_octave_index (filt)); } /* optionally save GSS */ if (gss.active) { err = save_gss (filt, &gss, basename, verbose) ; if (err) { snprintf (err_msg, sizeof(err_msg), "Could not write GSS to PGM file.") ; goto done ; } } /* run detector ............................................. */ if (ikeys == 0) { vl_sift_detect (filt) ; keys = vl_sift_get_keypoints (filt) ; nkeys = vl_sift_get_nkeypoints (filt) ; i = 0 ; if (verbose > 1) { printf ("sift: detected %d (unoriented) keypoints\n", nkeys) ; } } else { nkeys = nikeys ; } /* for each keypoint ........................................ */ for (; i < nkeys ; ++i) { double angles [4] ; int nangles ; VlSiftKeypoint ik ; VlSiftKeypoint const *k ; /* obtain keypoint orientations ........................... */ if (ikeys) { vl_sift_keypoint_init (filt, &ik, ikeys [4 * i + 0], ikeys [4 * i + 1], ikeys [4 * i + 2]) ; if (ik.o != vl_sift_get_octave_index (filt)) { break ; } k = &ik ; /* optionally compute orientations too */ if (force_orientations) { nangles = vl_sift_calc_keypoint_orientations (filt, angles, k) ; } else { angles [0] = ikeys [4 * i + 3] ; nangles = 1 ; } } else { k = keys + i ; nangles = vl_sift_calc_keypoint_orientations (filt, angles, k) ; } /* for each orientation ................................... */ for (q = 0 ; q < nangles ; ++q) { vl_sift_pix descr [128] ; /* compute descriptor (if necessary) */ if (out.active || dsc.active) { vl_sift_calc_keypoint_descriptor (filt, descr, k, angles [q]) ; } if (out.active) { int l ; vl_file_meta_put_double (&out, k -> x ) ; vl_file_meta_put_double (&out, k -> y ) ; vl_file_meta_put_double (&out, k -> sigma ) ; vl_file_meta_put_double (&out, angles [q] ) ; for (l = 0 ; l < 128 ; ++l) { vl_file_meta_put_uint8 (&out, (vl_uint8) (512.0 * descr [l])) ; } if (out.protocol == VL_PROT_ASCII) fprintf(out.file, "\n") ; } if (frm.active) { vl_file_meta_put_double (&frm, k -> x ) ; vl_file_meta_put_double (&frm, k -> y ) ; vl_file_meta_put_double (&frm, k -> sigma ) ; vl_file_meta_put_double (&frm, angles [q] ) ; if (frm.protocol == VL_PROT_ASCII) fprintf(frm.file, "\n") ; } if (dsc.active) { int l ; for (l = 0 ; l < 128 ; ++l) { double x = 512.0 * descr[l] ; x = (x < 255.0) ? x : 255.0 ; vl_file_meta_put_uint8 (&dsc, (vl_uint8) (x)) ; } if (dsc.protocol == VL_PROT_ASCII) fprintf(dsc.file, "\n") ; } } } } /* ............................................................... * Finish up * ............................................................ */ if (met.active) { fprintf(met.file, "<sift\n") ; fprintf(met.file, " input = '%s'\n", name) ; if (dsc.active) { fprintf(met.file, " descriptors = '%s'\n", dsc.name) ; } if (frm.active) { fprintf(met.file," frames = '%s'\n", frm.name) ; } fprintf(met.file, ">\n") ; } done : /* release input keys buffer */ if (ikeys) { free (ikeys) ; ikeys_size = nikeys = 0 ; ikeys = 0 ; } /* release filter */ if (filt) { vl_sift_delete (filt) ; filt = 0 ; } /* release image data */ if (fdata) { free (fdata) ; fdata = 0 ; } /* release image data */ if (data) { free (data) ; data = 0 ; } /* close files */ if (in) { fclose (in) ; in = 0 ; } vl_file_meta_close (&out) ; vl_file_meta_close (&frm) ; vl_file_meta_close (&dsc) ; vl_file_meta_close (&met) ; vl_file_meta_close (&gss) ; vl_file_meta_close (&ifr) ; /* if bad print error message */ if (err) { fprintf (stderr, "sift: err: %s (%d)\n", err_msg, err) ; exit_code = 1 ; } } /* quit */ return exit_code ; }
PyObject * vl_siftdescriptor_python( PyArrayObject & in_grad, PyArrayObject & in_frames) { // TODO: check types and dim // "GRAD must be a 2xMxN matrix of class SINGLE." assert(in_grad.descr->type_num == PyArray_FLOAT); assert(in_frames.descr->type_num == PyArray_FLOAT64); assert(in_grad.flags & NPY_FORTRAN); assert(in_frames.flags & NPY_FORTRAN); int verbose = 0; int opt; // TODO: check if we need to do a copy of the grad array float * grad_array; vl_sift_pix *grad; int M, N; double magnif = -1; double *ikeys = 0; int nikeys = 0; int i, j; /* ----------------------------------------------------------------- * Check the arguments * -------------------------------------------------------------- */ // get frames nb and data pointer nikeys = in_frames.dimensions[1]; ikeys = (double *) in_frames.data; // TODO: deal with optional params // while ((opt = uNextOption(in, nin, options, &next, &optarg)) >= 0) { // switch (opt) { // // case opt_verbose : // ++ verbose ; // break ; // // case opt_magnif : // if (!uIsRealScalar(optarg) || (magnif = *mxGetPr(optarg)) < 0) { // mexErrMsgTxt("MAGNIF must be a non-negative scalar.") ; // } // break ; // // default : // assert(0) ; // break ; // } // } // TODO: convert to Python //grad_array = mxDuplicateArray(in[IN_GRAD]); (copy?) grad = (float*) in_grad.data; //mxGetData(grad_array); M = in_grad.dimensions[1]; N = in_grad.dimensions[2]; /* transpose angles */ for (i = 1; i < 2 * M * N; i += 2) { grad[i] = VL_PI / 2 - grad[i]; } /* ----------------------------------------------------------------- * Do job * -------------------------------------------------------------- */ PyArrayObject * _descr; { VlSiftFilt *filt = 0; vl_uint8 *descr = 0; /* create a filter to process the image */ filt = vl_sift_new(M, N, -1, -1, 0); if (magnif >= 0) vl_sift_set_magnif(filt, magnif); if (verbose) { printf("siftdescriptor: filter settings:\n"); printf( "siftdescriptor: magnif = %g\n", vl_sift_get_magnif(filt)); printf("siftdescriptor: num of frames = %d\n", nikeys); } { npy_intp dims[2]; dims[0] = 128; dims[1] = nikeys; _descr = (PyArrayObject*) PyArray_NewFromDescr( &PyArray_Type, PyArray_DescrFromType(PyArray_UBYTE), 2, dims, NULL, NULL, NPY_F_CONTIGUOUS, NULL); descr = (vl_uint8*) _descr->data; } /* ............................................................... * Process each octave * ............................................................ */ for (i = 0; i < nikeys; ++i) { vl_sift_pix buf[128], rbuf[128]; double y = *ikeys++; double x = *ikeys++; double s = *ikeys++; double th = VL_PI / 2 - *ikeys++; vl_sift_calc_raw_descriptor(filt, grad, buf, M, N, x, y, s, th); transpose_descriptor(rbuf, buf); for (j = 0; j < 128; ++j) { double x = 512.0 * rbuf[j]; x = (x < 255.0) ? x : 255.0; *descr++ = (vl_uint8) (x); } } /* cleanup */ // mxDestroyArray(grad_array); vl_sift_delete(filt); } /* job done */ return PyArray_Return(_descr); }