OpenCV 3 SURF feature detection - c++

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;
}

Related

OpenCV identifier "Tracker" is undefined

i'm using OpenCV 3.2 and trying to compile the following code using Visual studio 2013:
#include <opencv2/core/utility.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap;
Mat frame;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 160);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 120);
cap.set(CV_CAP_PROP_FPS, 15);
cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('B', 'G', 'R', '3'));
cap = VideoCapture(0);
Ptr<Tracker> tracker = Tracker::create("KCF");
while (1){
cap.read(frame);
imshow("TEST", frame);
waitKey(1);
}
return 0;
}
But it tells me that tracker is undefined.i'm trying to use the example from Here.
the opencv2/tracking.hpp used there did not exist in my include directory so i added some files to fix including problems(required files were feature.hpp,onlineMIL.hpp,onlineBoosting.hpp,tracking.hpp. copied from opencv github) but still VS tells me that Tracker is undefined
Opencv had moved all these extension librarys to Opencv-contrib when the Version 3.0 published, you can install from the github website:
https://github.com/opencv/opencv_contrib
please be care for choose the version about this Repository .

Assertion failed with cv::merge

I am currently doing some C++ image processing with openCV. I developed the application on a Mac with Xcode 6.3.2 and it works perfectly in both debug and release. In order to have a Windows executable program, I am now working on Windows with Visual Studio Express 2013. The program is running well on debug mode but crashes in release mode on this part of the code :
#include "stdafx.h"
#include "math.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/photo/photo.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
vector<Mat> stacked_images;
Mat medianr_eq, mediang_eq, medianb_eq, objrgb;
medianr_eq = imread("C:\\Path\\medianr_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
mediang_eq = imread("C:\\Path\\mediang_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
medianb_eq = imread("C:\\Path\\medianb_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
objrgb = Mat(medianr_eq.size(), CV_16UC3);
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(stacked_images, objrgb);
}
The error I get is :
OpenCV Error : Assertion failed <mv && n > 0> in cv::merge, file C:\builds\master_PackSlave_Win64-vc12-shared\opencv\modules\core\src\convert.cpp, line 941
I can't see where I could have done something wrong... Indeed, it is pretty basic OpenCV !
The images I used are downloadable with this link : https://transfert.u-psud.fr/gs67
For astronomy lovers it is the Stephan's Quintet, taken with Calar Alto Observatory's 1.23m telescope where I currently am an intern.
Thank you in advance for your help,
Arnaud.
I recently had the exact same error, appearing with a valid openCV code running perfectly fine in debug mode but not in release mode.
Looking into the openCV source code, one can find that the function called has this code (modules/core/src/convert.cpp, line 341):
void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
{
CV_OCL_RUN(_mv.isUMatVector() && _dst.isUMat(),
ocl_merge(_mv, _dst))
std::vector<Mat> mv;
_mv.getMatVector(mv);
merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
}
the last line here calls the function 'void cv::merge(const Mat mv, size_t n, OutputArray _dst)*', which has the infamous CV_Assert( mv && n > 0 ); instruction in its first line, causing the crash at runtime.
This error tells us that the vector of Mat is either a null pointer/reference or empty, which it clearly isn't in your code. I strongly suspect that the error is in the getMatVector function call, not copying the contents of _mv into mv. This leaves an empty array that is passed to the merge function, causing the error raised by CV_Assert.
In my case, the fix was to use directly the prototype of the function defined at line 200 in convert.cpp. For you, that would mean (copying only the last few lines of your code):
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(&stacked_images[0], stacked_images.size(), objrgb);
This solution worked in my case, and my code is now happily running in debug and release mode !
PS: I know it has been a while, but thought I would post the answer anyway in case somebody running into the same problem would arrive on this SO question.
PS2: Here is a full example code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
namedWindow( "Display window", WINDOW_AUTOSIZE );
Mat result;
Mat R = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat G = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat B = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
// Changing channel values for final result that should look largely blue
R = 0.1*R;
G = 0.1*G;
B = 1.5*B;
std::vector<cv::Mat> array_to_merge ;
array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);
// This line triggers a runtime error in Release mode
//cv::merge(array_to_merge,result);
// This line works both in Release and Debug mode
cv::merge(&array_to_merge[0], array_to_merge.size(), result);
cv::imshow( "Display window", result );
cv::waitKey(0);
}
Although you could write it in a lot fewer lines , this code is valid.

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 crash after SURF Detection

I wrote a simple program in OpenCV that detects SURF feature in a given image and diplays the detected features in a namedWindow.
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
using namespace cv;
int main(int argc,char** argv)
{
if(argc!=3)//Check cmd number of argumets
{
std::cout<<"Usage: "<<argv[0]<<" <image-file> <method>"<<std::endl;
return -1;
}
//LOAD THE SOURCE IMAGE
Mat Img = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
if(!Img.data)//Check correct image load
{
std::cout<<"Cannot read image file. Check file path!"<<std::endl;
return -1;
}
//COMPUTE FEATURES
SurfFeatureDetector detector;
std::vector<KeyPoint> features;
detector.detect(Img,features);
//SHOW RESULT
Mat ImgF;
drawKeypoints(Img,features,ImgF);
namedWindow("Features", CV_GUI_NORMAL);
imshow("Features",ImgF);
waitKey();
return 0;
}
Everything is OK, the programs do what it have to do. The problem is when pressing a key to terminate the program a crash error occurs.
It doesn't crash for me... but in order for me to compile your code, I had to add
#include <opencv2/nonfree/features2d.hpp>
because SURF was moved to the nonfree module at some point.
So, I would have to recommend trying the newest version (2.4.6 as of today).

OpenCV SURF extractor.compute error

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();