OpenCV SURF extractor.compute error - c++

I use OpenCV 2.44 and Visual Studio C++ 2010
When I compile this
#include <opencv2/imgproc/imgproc_c.h>
#include <stdio.h>
#include <math.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace cv;
void main()
{
Mat img1 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
Mat img2 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
// detecting keypoints
FastFeatureDetector detector(15);
vector<KeyPoint> keypoints1;
detector.detect(img1, keypoints1);
// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1;
extractor.compute(img1, keypoints1, descriptors1);
when I run the code I get Unhandled exception at 0x580f375b in prj.exe: 0xC0000005: Access violation reading location 0x001f7014.
the error is at extractor
I'm using this tutorial link

It's looks like that you forget to init non-free module. Try to call appropriate function before using SurfDescriptorExtractor:
#include <opencv2/nonfree/nonfree.hpp>
...
cv::initModule_nonfree();

Related

OpenCV 3 SURF feature detection

I am trying to do a simple surf feature detection.
The problem is that during the execution I have two Microsoft Visual C++ Runtime Library Errors with only the use of the detect method :
Debug Assertion Failed ! Program:
C:\Windows\system32\MSVCP120D.dll File : C:\Program Files
(x86\Microsoft Visual Studio 12.0\VC\include\vector Line: 240
Expression : vector iterators incompatible
And
Debug Assertion Failed ! Program:
C:\opencv\build\x64\vc12\bin\Debug\opencv_xfeatures2d300d.dll File : C:\Program Files
(x86\Microsoft Visual Studio 12.0\VC\include\vector Line: 241
Expression : "Standard C++ Libraries Invalid Argument" && 0
This is the code :
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv/cv.h"
#include "opencv/highgui.h"
using namespace cv;
using namespace std;
using namespace xfeatures2d;
int main(int argc, const char** argv)
{
// Read image
Mat img1 = imread("box.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread("box_in_scene.png", CV_LOAD_IMAGE_GRAYSCALE);
if (img1.empty() || img2.empty())
{
printf("Images non lisibles \n");
return -1;
}
// detecting keypoints
int hessian = 800;
Ptr<SURF> surf = SURF::create(hessian);
vector<KeyPoint> keypoints1, keypoints2;
surf->detect(img1, keypoints1);
surf->detect(img2, keypoints2);
waitKey(0);
return 0;
}

OpenCV Bag of Words: Assertion failed (!_descriptors.empty())

I'm trying to create Bag-Of-Words so I can train the SVM later. I'm new with OpenCV so I used a code that I found on the Internet. The problem is that I have the following error:
OpenCV Error: Assertion failed (!_descriptors.empty()) in add, file /build/buildd/opencv-2.4.8+dfsg1/modules/features2d/src/bagofwords.cpp, line 57
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.4.8+dfsg1/modules/features2d/src/bagofwords.cpp:57: error: (-215) !_descriptors.empty() in function add
Aborted (core dumped)
And here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Ptr<FeatureDetector> features = FeatureDetector::create("FAST");
Ptr<DescriptorExtractor> descriptors = DescriptorExtractor::create("FAST");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
//defining terms for bowkmeans trainer
TermCriteria tc(TermCriteria::MAX_ITER + TermCriteria::EPS, 10, 0.001);
int dictionarySize = 1000;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bow_trainer(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor bowDE(descriptors, matcher);
//training data now
Mat mat_features1, mat_features2;
Mat img = imread("../positive_images/2014-12-07 19-55-07 804ea51d (1).JPG", 0);
Mat img2 = imread("../positive_images/Pictures23.bmp_0000_0342_0104_0564_0543.png", 0);
vector<KeyPoint> keypoints, keypoints2;
features->detect(img, keypoints);
features->detect(img2,keypoints2);
descriptors->compute(img, keypoints, mat_features1);
descriptors->compute(img2, keypoints2, mat_features2);
bow_trainer.add(mat_features1);
bow_trainer.add(mat_features2);
Mat dictionary = bow_trainer.cluster();
bowDE.setVocabulary(dictionary);
return 0;
}
And here is how the code is compiled:
g++ BOW_creator.cpp -o BOW_creator `pkg-config --cflags --libs opencv`
Do you know what the problem could be? Can you tell me how to fix it?
Let me know if you need me to provide more information.

OpenCV C++ 'Access Violation' with SURF

I'm trying using SURF feature detector but I'm always getting this error-
The program '[1120] Corner Detection.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
Here is the code
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2\nonfree\nonfree.hpp"
using namespace cv;
using namespace std;
void foo(Mat &image1, Mat &image2)
{
int minHeassian = 400;
SurfFeatureDetector detector(minHeassian);
std::vector< KeyPoint > keypoints1, keypoints2;
keypoints1.resize(1000);
keypoints2.resize(1000);
detector.detect(image1, keypoints1); // <--- crashes at this line
detector.detect(image2, keypoints2); // <--- probably will crash here too
SurfDescriptorExtractor extractor;
Mat discriptors1, discriptors2;
extractor.compute(image1, keypoints1, discriptors1);
extractor.compute(image2, keypoints2, discriptors2);
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match(discriptors1, discriptors2, matches);
double minDist = 100;
for (int i = 0; i < matches.size(); ++i)
if (matches[i].distance < minDist)
minDist = matches[i].distance;
std::vector< DMatch > goodMatches;
for (int i = 0; i < matches.size(); ++i)
if (matches[i].distance <= max(0.02, (double)matches[i].distance))
goodMatches.push_back(matches[i]);
Mat matchImage;
drawMatches(image1, keypoints1, image2, keypoints2,
goodMatches, matchImage,
Scalar::all(-1), Scalar::all(-1), vector<char>(),
DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
namedWindow("Matches", WINDOW_AUTOSIZE);
imshow("Matches", matchImage);
waitKey(0);
}
int _tmain(int argc, _TCHAR* argv[])
{
cv::initModule_nonfree();
Mat left, right;
right = imread("D:\\right.jpg", IMREAD_COLOR);
left = imread("D:\\left.jpg", IMREAD_COLOR);
foo(left, right);
return 0;
}
I get the error at the line
detector.detect(image1, keypoints1);
I have following lib files mentioned to linker-
opencv_core249d.lib
opencv_imgproc249d.lib
opencv_highgui249d.lib
opencv_ml249d.lib
opencv_video249d.lib
opencv_features2d249d.lib
opencv_calib3d249d.lib
opencv_objdetect249d.lib
opencv_contrib249d.lib
opencv_legacy249d.lib
opencv_flann249d.lib
opencv_features2d249.lib
opencv_nonfree249.lib
I have tried everything I found on the Internet but nothing worked. What is wrong with this code?
I'm running VS 2013 on Windows 8.1 and I'm using OpenCV version 2.4.9.
Solved
It was a silly mistake. I used the library opencv_nonfree249.lib whereas I should be using opencv_nonfree249**d**.lib as I was working in debug mode.
You are using the library opencv_nonfree249.lib in a debug environment which is meant for release environment. Add a d to the lib name to make it opencv_nonfree249d.lib which will work for debug environment.

Feature point Tracking

I am trying to track set of feature points in a sequence of grayscale images using OpenCV 2.4.0.
I already know how to implement SIFT or SURF for detecting feature points and initially computing the descriptors. However, I need help in computing the SIFT descriptor of a feature point whose location (u,v) is only known to me. A working example code for SIFT is shown below.
For example, if I use Haris corner detector to detect features at dv_scenePoints_t like:
cvGoodFeaturesToTrack (source2, eig_img, temp_img, dv_scenePoints_t, &corner_count, 0.3, 3.0, mask, 7, 1);
Then in such a case, how would I compute the SIFT descriptor of points at dv_scenePoints_t.
Also, if I have to track feature points by a particle filter. Then, how would I use the SIFT descriptor for computing the weight of each particle (feature point hypothesis).
Thanks.
#include "stdafx.h"
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <opencv2/nonfree/features2d.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/legacy/legacy.hpp"
#include "opencv2/legacy/compat.hpp"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <string.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat source1 = imread("KITTI_train.png",CV_LOAD_IMAGE_GRAYSCALE);
Mat source2 = imread("KITTI_trainRotate90.png",CV_LOAD_IMAGE_GRAYSCALE);
vector<KeyPoint> dv_sceneKeypoints_t, dv_objectKeypoints_t;
vector< DMatch > matches;
SiftFeatureDetector detector(400,5,0.03);
detector.detect(source1, dv_objectKeypoints_t);
detector.detect(source2, dv_sceneKeypoints_t);
SiftDescriptorExtractor extractor;
Mat descriptors1,descriptors2;
extractor.compute(source1,dv_objectKeypoints_t,descriptors1);
extractor.compute(source2,dv_sceneKeypoints_t,descriptors2);
FlannBasedMatcher matcher;
matcher.match(descriptors1,descriptors2, matches);
Mat target;
drawMatches(source1,dv_objectKeypoints_t,source2,dv_sceneKeypoints_t,matches,target);
imshow("Matches", target);
waitKey(0);
return 0;
}
Keypoint structure contains some members such as size and response:
http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html?#KeyPoint
You can use these features to determine the relative weights of particles.

OpenCV 2.42 FeatureDetector FREAK

I want to try the new class FREAK in OpenCV 2.4.2.
I tried to use common interface of feature detector to construct FREAK, but,of course, it doesn't work. How should I revise my code to get result?
#include <stdio.h>
#include <iostream>
#include <opencv\cxcore.h>
#include <opencv2\nonfree\features2d.hpp>
#include <opencv\highgui.h>
#include <opencv2\features2d\features2d.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main(){
Mat mat1;
mat1 = imread("Testimg06.jpg",0);
vector<KeyPoint> P1;
Ptr<FeatureDetector> freakdes;
Ptr<DescriptorExtractor> descriptorExtractor;
freakdes = FeatureDetector::create("FREAK");
freakdes->detect(mat1,P1);
Mat keypoint_img;
drawKeypoints( mat1, P1, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("Keypoints 1", keypoint_img );
cvWaitKey(0);
}
FREAK is descriptor only. There is no corresponding feature detector.
So you need to combine it with one of the available detectors: FAST, ORB, SIFT, SURF, MSER or use goodFeaturesToTrack function.
There is an OpenCV example that shows how to use FREAK combined with FAST.
The basic instructions are:
FREAK extractor;
BruteForceMatcher<Hamming> matcher;
std::vector<KeyPoint> keypointsA, keypointsB;
Mat descriptorsA, descriptorsB;
std::vector<DMatch> matches;
FAST(imgA,keypointsA,10);
FAST(imgB,keypointsB,10);
extractor.compute( imgA, keypointsA, descriptorsA );
extractor.compute( imgB, keypointsB, descriptorsB );
matcher.match(descriptorsA, descriptorsB, matches);