Detection of planar objects
I am trying to do feature extraction using C++ API. For this, I am using http://docs.opencv.org/ref/master/dd/... as reference.
The code I have written is :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/calib3d.hpp>
using namespace cv;
#include <iostream>
#include <stdio.h>
using namespace std;
Mat img; Mat templ;
int main(int, char** argv)
{
templ = cv::imread("C:/Users/.../workspace/FeatureDetectionCPP/src/temp.jpg", IMREAD_GRAYSCALE);
img = cv::imread("C:/Users/.../workspace/FeatureDetectionCPP/src/image.jpg", IMREAD_GRAYSCALE);
if(templ.empty())
{
cout<<"Empty template";
}
if(img.empty())
{
cout<<"Empty image";
}
Ptr<Feature2D> surf = SURF::create();
vector<KeyPoint> keypoints1;
Mat descriptors1;
surf->detectAndCompute(img, Mat(), keypoints1, descriptors1);
Ptr<Feature2D> surf1 = SURF::create();
vector<KeyPoint> keypoints2;
Mat descriptors2;
surf1->detectAndCompute(templ, Mat(), keypoints2, descriptors2);
// matching descriptors
BruteForceMatcher<L2<float> > matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(templ, keypoints1, img, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);
return 0;
}
During compilation, I got this:
error: 'SURF' has not been declared Ptr<feature2d> surf1 = SURF::create();
error: 'BruteForceMatcher' was not declared in this scope BruteForceMatcher<l2<float> > matcher;
error: expected primary-expression before '>' token BruteForceMatcher<l2<float> > matcher;
error: 'matcher' was not declared in this scope BruteForceMatcher<l2<float> > matcher;
On searching, I found that #include "opencv2/nonfree/features2d.hpp" should be added. However, I don't have any folder nonfree or misc in the opencv2 folder. I am using opencv 3.0.0. And, have compiled it using mingw.
Can anyone help in telling as if why this error is appearing. And, how can I resolve it.
I have looked at http://answers.opencv.org/question/52...
but don't understand that on donloading zip file, where shall I place it. How shall it be compiled to add to the existing build of opencv.
@berak , can you help me wih this ?
to use SURF or SIFT in 3.0, you need the opencv_contrib repo, also they are in another namespace, try to add:
there's also plan B: use another keypointdetector/featureextractor pair, e.g. AKAZE instead of SURF should do pretty well.
cmake -DOPENCV_EXTRA_MODULES_PATH=/your/opencv_contrib/modules
. again, please see the readme here.I am using cmake-gui. I just entered path for contrib/modules. On executing mingw32-make install, the issues coming are :
warning: ignoring #pragma warning [-Wunknown-pragmas]
pragma warning( disable : 4267 )
In file included from C:/opencv1/opencv_contrib-master/opencv_contrib-master/mod ules/line_descriptor/src/precomp.hpp:72:0, from C:\opencv1\build\x86\mingw\modules\line_descriptor\opencv_ line_descriptor_pch_dephelp.cxx:1: C:/opencv1/opencv_contrib-master/opencv_contrib-master/modules/line_descriptor/s rc/bitops.hpp:49:21: fatal error: intrin.h: No such file or directory
include <intrin.h>
How to get rid of them ?
oh, the line_descriptor again. did not compile for me, either ;(
you might disable it by ticking BUILD_opencv_line_descriptor=OFF in cmake-gui.
there might be more modules, which do not compile (saliency ?), just disable them in the same way.
Okay, so now on compilation, 3 errors are still left : error: 'BruteForceMatcher' was not declared in this scope BruteForceMatcher<l2<float> > matcher;
error: expected primary-expression before '>' token BruteForceMatcher<l2<float> > matcher;
error: 'matcher' was not declared in this scope BruteForceMatcher<l2<float> > matcher;
What shall I do for BruteForceMatcher ?
BFMatcher
your original code seems rather outdated. see e.g. here for a recent demo using AKAZE
@berak, And by the way...saliency compiled w/o giving errors :) However, datasets threw an error.
This time, when I am using AKAZE, issues I am facing are : 1. It's dealing with images of 32-bit depth & for others, its giving error : Assertion failed (type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in gemm, file C:\opencv\opencv-master\opencv-master\modules\core\src\matmul.cpp, line 893 2. Even when I crop a part of image, it isn't finding any matches :/
I am using the image - http://nullcandy.com/wp-content/uploa...
And BFMatcher gives the error : Assertion failed ((type == CV_8U && dtype == CV_32S) || dtype == CV_32F) in batchDistance, file C:\opencv\opencv-master\opencv-master\modules\core\src\stat.cpp, line 3662 for images not having 32-bit depth.
Even for some images, it works. And for some, it gives this error.
Does it require any special kind of image ? Or there is something else...
I have also tried with images graf1.png and graf3.png. But, the error is same...
Can you please help me with this @berak ?
Since I have faced a lot of problems in setting up OpenCV for Windows 7, I ended up writing a blog describing the process. You may find it at http://techieshruti.blogspot.in/2015/... and help others too. Thanks, Shruti