opencv - Dense SIFT Bag of Features -
i trying dense sift feature extraction bag of features (bof) using opencv. idea follow this tutorial little change use dense sift features instead.
as know, first part in bof paradigm, create visual dictionary. code modification goes this:
#include<iostream> #include<vector> #include<dirent.h> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/nonfree/features2d.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/nonfree/nonfree.hpp> // dense sampling of sift descriptors. using namespace std; using namespace cv; void dense_sift_bow(mat img_raw, mat &featuresunclustered); void sift_matcher(mat img_raw, mat &dictionary, mat &bowtry, ptr<descriptormatcher> &matcher); #define dictionary_build 0 int main() { #if dictionary_build == 1 initmodule_nonfree(); // store detected image key points. mat featuresunclustered; dir *pdir = nullptr; string imgdir("d:/willowactions backup/willow 300_200/sample/");// directory of images. (change according images are) string imgpath; pdir = opendir(imgdir.c_str()); string dirname; mat img_raw; // our loaded image. struct dirent *pent = nullptr; // dirent structure directory manipulation. if(pdir == nullptr) { cout << "directory pointer not initialized correctly ! " << endl; // checking. return 1; } int count = 0; cout << "please wait..." << endl; while((pent = readdir(pdir)) != nullptr) { if(pent == nullptr) { cout << " dirent struct not initialized correctly !" << endl; return 1; } if(!strcmp(pent->d_name,".")||!strcmp(pent->d_name,"..")) { } else { // loop trough directory read image 1 one , extract dense sift dirname = pent->d_name; imgpath = imgdir+dirname; // overall image path... feed imread(); img_raw = imread(imgpath,1);// read image extract densesift features dense_sift_bow(img_raw, featuresunclustered); count++; } } cout << "number of files in folder: " << count << endl; cout << featuresunclustered.size() << endl; //construct bow k-means trainer // number of bags int dictionarysize = 5; //define term criteria termcriteria tc(cv_termcrit_iter, 100, 0.001); // retry number int retries = 1; //necessary flags int flags = kmeans_pp_centers; //create bow trainer bowkmeanstrainer bowtrainer(dictionarysize, tc, retries, flags); //cluster feature vectors mat dictionary = bowtrainer.cluster(featuresunclustered); //store vocabulary filestorage fs("d:/willowactions backup/willow 300_200/dense.xml", filestorage::write); fs << "vocabulary" << dictionary; fs.release();
the function dense_sift_bow(img_raw, featuresunclustered) defined as:
void dense_sift_bow(mat img_raw,mat &featuresunclustered) { mat descriptors; // store our dense sift descriptors. vector<keypoint> keypoints; densefeaturedetector detector(12.f, 1, 0.1f, 10); detector.detect(img_raw,keypoints); ptr<descriptorextractor> descriptorextractor = descriptorextractor::create("sift"); descriptorextractor->compute(img_raw,keypoints,descriptors); descriptors.setto(0, descriptors < 0); descriptors = descriptors.reshape(0,1); featuresunclustered.push_back(descriptors); }
what loop trough folder each image, extract dense sift features images , k-means clustering. builds visual dictionary.
this part of code works fine me. i.e. in folder 256 image files dictionary of size 256 x 76800 (rows columns: cluster centers). after creating dictionary.
now, having created dictionary. following bof paradigm, want again extract dense sift , matching against dictionary created. use nearest neighbors. looping folder images. have:
#else mat dictionary; filestorage fs("d:/willowactions backup/willow 300_200/dic_dense.xml", filestorage::read); fs["vocabulary"] >> dictionary; fs.release(); //create nearest neighbor matcher ptr<descriptormatcher> matcher(new flannbasedmatcher); //ptr<descriptorextractor> descriptorextractor = descriptorextractor::create("sift"); cout << dictionary.size() << endl; dir *pdir = nullptr; string imgdir("d:/willowactions backup/willow 300_200/sample/");// directory of images. (change according images are) string imgpath; pdir = opendir(imgdir.c_str()); string dirname; mat img_raw; // our loaded image. mat bowtry; struct dirent *pent = nullptr; // dirent structure directory manipulation. if(pdir == nullptr) { cout << "directory pointer not initialized correctly ! " << endl; // checking. return 1; } int count = 0; cout << "please wait..." << endl; while((pent = readdir(pdir)) != nullptr) { if(pent == nullptr) { cout << " dirent struct not initialized correctly !" << endl; return 1; } if(!strcmp(pent->d_name,".")||!strcmp(pent->d_name,"..")) { } else { // loop trough directory read image 1 one , extract dense sift dirname = pent->d_name; imgpath = imgdir+dirname; // overall image path... feed imread(); img_raw = imread(imgpath,1);// read image extract hog features sift_matcher(img_raw, dictionary, bowtry, matcher); cout << count << endl; count++; } } cout << "no of images: " << count << endl; //open file write resultant descriptor filestorage fs1("d:/willowactions backup/willow 300_200/features_train.xml", filestorage::write); //write new bof descriptor file fs1 << "vocabulary" << bowtry; fs1.release(); #endif waitkey(0); return 0; }
the function sift_matcher(img_raw, dictionary, bowtry, matcher) this:
void sift_matcher(mat img_raw, mat &dictionary, mat &bowtry, ptr<descriptormatcher> &matcher) { mat descriptors; vector<keypoint> keypoints; densefeaturedetector detector(12.f,1,0.1f,10); detector.detect(img_raw,keypoints); ptr<descriptorextractor> descriptorextractor = descriptorextractor::create("sift"); //create bof descriptor extractor bowimgdescriptorextractor bowde(descriptorextractor, matcher); bowde.setvocabulary(dictionary); //to store bof representation of image mat bowdescriptor; //extract bof descriptor given image bowde.compute(img_raw,keypoints,bowdescriptor); bowtry.push_back(bowdescriptor); bowdescriptor.release(); }
so, problem second part. whenever run second part program stops working "programx.exe has stopped working". no errors generated compiler , console. maybe have been overlooking something. suggestions, comments , helps welcomed. in advance.