c++ - BRIEF implementation with OpenCV 2.4.10 -
does know of link brief implementation opencv 2.4? regards.
ps: know such questions not welcome on so, primary focus work have done. there similar question quite received.
one of answers questions suggests generic manner sift, extended brief. here modified code.
#include <opencv2/nonfree/nonfree.hpp> #include <opencv2/highgui/highgui.hpp> //using namespace std; using namespace cv; int main(int argc, char *argv[]) { mat image = imread("load02.jpg", cv_load_image_grayscale); cv::initmodule_nonfree(); // create smart pointer sift feature detector. ptr<featuredetector> featuredetector = featuredetector::create("harris"); // "brief written. changed after answer." vector<keypoint> keypoints; // detect keypoints featuredetector->detect(image, keypoints); // note: featuredetector pointer hence '->'. //similarly, create smart pointer sift extractor. ptr<descriptorextractor> featureextractor = descriptorextractor::create("brief"); // compute 128 dimension sift descriptor @ each keypoint. // each row in "descriptors" correspond sift descriptor each keypoint mat descriptors; featureextractor->compute(image, keypoints, descriptors); // if draw detected keypoint check mat outputimage; scalar keypointcolor = scalar(255, 0, 0); // blue keypoints. drawkeypoints(image, keypoints, outputimage, keypointcolor, drawmatchesflags::default); namedwindow("output"); imshow("output", outputimage); char c = ' '; while ((c = waitkey(0)) != 'q'); // keep window there until user presses 'q' quit. return 0; }
the issue code gives error: first-chance exception @ 0x00007ffb84698b9c in project2.exe: microsoft c++ exception: cv::exception @ memory location 0x00000071f4fbf8e0.
the error results in function execution breaking. tag says execution resume @ namedwindow("output");
line.
could please fix issue, or suggest new code altogether? thanks.
edit: terminal shows error: assertion failed (!outimage.empty()) in cv::drawkeypoints, file ..\..\..\..opencv\modules\features2d\src\draw.cpp, line 115
. next statement code resume remains same, drawkepoints
called before it.
in opencv, brief descriptorextractor, not featuredetector. according featuredetector::create, factory method not support "brief"
algorithm. in other words, featuredetector::create("brief")
returns null pointer , program crashes.
the general steps in feature matching are:
- find interesting (feature) points in image:
featuredetector
- find way describe points:
descriptorextractor
- try match descriptors (feature vectors) in 2 images:
descriptormatcher
brief algorithm step 2. can use other methods, harris, orb, ..., in step 1 , pass result step 2 using brief. besides, sift can used in both step 1 , 2 because algorithm provides methods both steps.
here's simple example use brief in opencv. first step, find points looks interesting (key points) in image:
vector<keypoint> detectkeypoints(const mat &image) { auto featuredetector = featuredetector::create("harris"); vector<keypoint> keypoints; featuredetector->detect(image, keypoints); return keypoints; }
you can try featuredetector algorithm instead of "harris"
. next step, compute descriptors key points:
mat computedescriptors(const mat &image, vector<keypoint> &keypoints) { auto featureextractor = descriptorextractor::create("brief"); mat descriptors; featureextractor->compute(image, keypoints, descriptors); return descriptors; }
you can use algorithm different "brief"
, too. , can see algorithms in descriptorextractor not same algorithms in featuredetector. last step, match 2 descriptors:
vector<dmatch> matchtwoimage(const mat &descriptor1, const mat &descriptor2) { auto matcher = descriptormatcher::create("bruteforce"); vector<dmatch> matches; matcher->match(descriptor1, descriptor2, matches); return matches; }
similarly, can try different matching algorithm other "bruteforce"
. main program, can build application functions:
auto img1 = cv::imread("image1.jpg"); auto img2 = cv::imread("image2.jpg"); auto keypoints1 = detectkeypoints(img1); auto keypoints2 = detectkeypoints(img2); auto descriptor1 = computedescriptors(img1, keypoints1); auto descriptor2 = computedescriptors(img2, keypoints2); auto matches = matchtwoimage(descriptor1, descriptor2);
and use matches
vector complete application. if want check results, opencv provides functions draw results of step 1 & 3 in image. example, draw matches in final step:
mat result; drawmatches(img1, keypoints1, img2, keypoints2, matches, result); imshow("result", result); waitkey(0);
Comments
Post a Comment