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.
Related
I am trying to set up a bag of visual words using openCV 3.0. I have looked a bit everywhere and all I seem to be able to find is code that is only compatible with versions in the 2.x domain. As of now this is what I have:
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
Ptr<FeatureDetector> features;
Ptr<DescriptorExtractor> descriptors;
Ptr<DescriptorMatcher> matcher;
int MAX_ITER = 100;
int EPS = 2;
TermCriteria tc(MAX_ITER + EPS,1,0.001);
int dictSize = 1000;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictSize,tc,retries,flags);
BOWImgDescriptorExtractor bowDE(descriptors,matcher);
Mat img1 = imread("/Users/Lucas/Desktop/pic2.jpg");
Mat img2 = imread("/Users/Lucas/Desktop/2.jpg");
vector<KeyPoint> keypoints,keypoints2;
features->detect(img1, keypoints);
features->detect(img2, keypoints2);
Mat myFeatures;
Mat myFeatures2;
descriptors->compute(img1, keypoints, myFeatures);
descriptors->compute(img2, keypoints2, myFeatures2);
bowTrainer.add(myFeatures);
bowTrainer.add(myFeatures2);
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
cout << dictionary << endl;
return 0;
}
I have put this together by using a few tutorials and snippets, but I am running into an issue. When the program gets to
features->detect(img1, keypoints);
it exits with a segmentation fault 11, whatever that means. Could someone help me and point out what it is I am doing wrong?
you have to create your FeatureDetector, DescriptorExtractor first. atm, you got null-pointer instances (that's your segfault).
#include <opencv2/xfeatures2d.hpp>
...
Ptr<FeatureDetector> features = xfeatures2d::SIFT::create();
Ptr<DescriptorExtractor> descriptors = xfeatures2d::SIFT::create();
Ptr<DescriptorMatcher> matcher = makePtr<BFMatcher>(NORM_L2);
note, that since you have to use SIFT or SURF, you will need the opencv_contrib repo installed for this
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;
}
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.
I'm trying to use the inpaint function in opencv, but I'm getting this error
loadimg.cpp: In function 'int main(int, char**)':
loadimg.cpp:19:28: error: 'INPAINT_TELEA' is not a member of 'cv'
loadimg.cpp:19:45: error: 'inpaint' was not declared in this scope
From typing this:
C:\Users\Francesco\Desktop\prova>g++ -I"C:\opencv\build\include"
-L"C:\opencv\build\x86\mingw\lib" loadimg.cpp -lopencv_core245 -lopencv_highgui245
-lopencv_img proc245 -o loadimg
This is my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cv.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = cv::imread("prova.jpg");
Mat mask;
cvtColor(src, mask, CV_RGB2GRAY);
threshold(mask, mask, 220, 255, CV_THRESH_BINARY);
Mat dst;
inpaint(src, mask, dst, 1, cv::INPAINT_TELEA);
imshow("image", dst);
waitKey(0);
return 0;
}
Can anyone help me? Thank you very much.
cv::inpaint() is declared in the photo module. You need to #include <opencv2/photo/photo.hpp> . Alternatively, you could #include <opencv2/opencv.hpp>, which includes all of OpenCV's functionality.
try:
inpaint(src, mask, dst, 1, INPAINT_TELEA);
Also, include :opencv2/photo/photo.hpp
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();