OpenCV FeatureDetector is not initialized - c++

I am trying to create a class where I shall find KeyPoints in an image. However, I get a ridiculous error I cannot figure out how to solve.
The problem is now that
this->detector
is zero, so it is for some reason not initialized properly. I have searched on google but I haven't found anything.
My headerfile looks as follows
/*
* FindKeyPoints.h
*
* Created on: Jan 21, 2015
* Author: erikbylow
*/
#ifndef FINDKEYPOINTS_H_
#define FINDKEYPOINTS_H_
#include <vector>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
using namespace std;
class FindKeyPoints {
private:
// Create vector of KeyPoints to store the detected keypoint in
vector<cv::KeyPoint> keyPointsOld;
vector<cv::KeyPoint> keyPointsNew;
// Threshold for similarity
const int THRESHOLD = 10;
// Instance(?) DescriptionExtractor and descriptor
cv::Ptr<cv::DescriptorExtractor> descriptionExtractor;
cv::Ptr<cv::FeatureDetector> detector;
public:
FindKeyPoints(const string &METHOD);
virtual ~FindKeyPoints();
void detectKeypoints(cv::Mat& imgNew, cv::Mat& imgOld);
};
#endif /* FINDKEYPOINTS_H_ */
And constructor looks as
FindKeyPoints::FindKeyPoints(const string &METHOD) {
cout<<METHOD<<endl;
cv::initModule_features2d();
this->detector = cv::FeatureDetector::create("SURF");
//this->descriptionExtractor = cv::DescriptorExtractor::create(METHOD);
}
and a function I will use looks as:
// Input: The new image and the old image. Find Keypoints and extract the descriptors.
void FindKeyPoints::detectKeypoints(cv::Mat& imgNew,
cv::Mat& imgOld) {
if (this->detector == 0){
cout<<"Hej"<<endl;
}
// this->detector->detect(imgNew, keyPointsNew);
// this->detector->detect(imgOld, keyPointsOld);
}
I use cmake and (part of) my CMakeLists.txt looks like:
TARGET_LINK_LIBRARIES(${PROJECT_NAME} groundtruth ${OpenCV_LIBS} ${QT_LIBRARIES}
${QGLViewer_LIBRARIES} ${OPENGL_gl_LIBRARY} GL
glut)
Can it be that
${OpenCV_LIBS}
does not include libopencv_nonfree.so ?
Regards

I suspect the error is that I had only included
opencv2/nonfree/features2d.hpp
and not also
opencv2/nonfree/nonfree.hpp.

Related

error: no matching function for call to 'FaceDetector::FaceDetector(std::__cxx11::string)'

I am new to C++ and i am getting error like
error: no matching function for call to 'FaceDetector::FaceDetector(std::__cxx11::string)'
FaceDetector fd(string(DEFAULT_CASCADE_PATH));
and i am attaching my code and error log how to fix this please guide me
#define DEFAULT_CASCADE_PATH "cascades/haarcascade_frontalface_default.xml"
#define ORIGINALS_LIST "obama_raw/list"
#define OUTPUT_DIR "obama_faces"
#define OUTPUT_LIST "list"
#define FACE_SIZE Size(150,150)
#include <cstdlib>
#include <fstream>
#include "cv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "FaceDetector.h"
using namespace std;
using namespace cv;
void read_input_list(const string &list_path, vector<Mat> &images) {
ifstream file(list_path.c_str());
string path;
while (getline(file, path)) {
images.push_back(imread(path));
}
}
int main(int argc, char** argv) {
FaceDetector fd(string(DEFAULT_CASCADE_PATH));
vector<Mat> raw_faces;
ofstream out_list(format("%s/%s", OUTPUT_DIR, OUTPUT_LIST).c_str());
read_input_list(string(ORIGINALS_LIST), raw_faces);
int img_c = 0; //images counter
//now detect the faces in each of the raw images:
for (vector<Mat>::const_iterator raw_img = raw_faces.begin() ; raw_img != raw_faces.end() ; raw_img++){
vector<Rect> faces;
//detect faces in the image (there should be only one):
fd.findFacesInImage(*raw_img, faces);
//cut each face and write to disk:
for (vector<Rect>::const_iterator face = faces.begin() ; face != faces.end() ; face++){
int edge_size = max(face->width, face->height);
Rect square(face->x, face->y, edge_size, edge_size);
Mat face_img = (*raw_img)(square);
//resize:
resize(face_img, face_img, FACE_SIZE);
//write to disk:
string face_path = format("%s/%d.jpg", OUTPUT_DIR, img_c++);
imwrite(face_path,face_img);
out_list << face_path << endl;
}
}
out_list.close();
return 0;
}
and i am attaching my error log.Please can any one help.
Thanks in advance
Error : https://i.stack.imgur.com/RZXXK.jpg
From GCC 5, A new ABI is enabled by default. In that new ABI, std::__cxx11 namesapce was introduced.
According to your error message, It seems that your program and OpenCV library you want to link with were build with different GCC version, which made incompatible binary.
For more information, you can read the following page:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

error C3861: 'getGaborKernel': identifier not found

I am trying to build the following code, but It failed because of the following error
error C3861: 'getGaborKernel': identifier not found
I have included the required header files and lib files. What could be the problem?
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
Mat in = imread("1.jpg",0); // load grayscale
Mat dest;
Mat src_f;
in.convertTo(src_f,CV_32F);
extern ostream cerr;
int kernel_size = 31;
double sig = 1, th = 0, lm = 1.0, gm = 0.02, ps = 0;
cv::Mat kernel =
getGaborKernel(cv::Size(kernel_size,kernel_size),sig, th, lm, gm,
ps);
cv::filter2D(src_f, dest, CV_32F, kernel);
cerr << dest(Rect(30,30,10,10)) << endl; // peek into the data
Mat viz;
dest.convertTo(viz,CV_8U,1.0/255.0);
imshow("k",kernel);
imshow("d",viz);
waitKey();
return 0;
}
When C++ misses a prototype it issues an error. Your source obviously didn't include the header file with the appropriate prototype or the prototype is hidden by #ifdefs.
Lookup the Header file that you expect that it contains the prototy of getGaborKernel.
Check the option Show includes to see what header files are actually included. Verify the header found in the first step is in the list.
Check the option Preprocess to a file to see what the preprocessor outputs after processing all #ifdefs in the generated .i file. Check why the prototype isn't in the output.
Uncheck both options after you have found the problem. That's important for the Preprocess to a file option to get a .obj file instead of the .i.

undefined reference BUT header is there

I am trying to run the ezsift library example. The example has name "image_match.cpp".
Here is the code
image_match.cpp
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "../ezsift.h"
using namespace std;
#define USE_FIX_FILENAME 0
int main(int argc, char ** argv)
{
#if USE_FIX_FILENAME
char * file1 = "img1.pgm";
char * file2 = "img2.pgm";
#else
if (argc != 3)
{
printf("Please input two image filenames.\n");
printf("usage: image_match img1 img2\n");
return -1;
}
char file1[255];
char file2[255];
memcpy(file1, argv[1], sizeof(char) * strlen(argv[1]));
file1[strlen(argv[1])] = 0;
memcpy(file2, argv[2], sizeof(char) * strlen(argv[2]));
file2[strlen(argv[2])] = 0;
#endif
// Read two input images
ImageObj<uchar> image1, image2;
if(image1.read_pgm(file1) != 0)
{
printf("Failed to open input image1!\n");
return -1;
}
if(image2.read_pgm(file2) != 0)
{
printf("Failed to open input image2!\n");
return -1;
}
printf("Image 1 loaded. Image size: %d x %d\n", image1.w, image1.h);
printf("Image 2 loaded. Image size: %d x %d\n", image2.w, image2.h);
// Double the original image as the first octive.
double_original_image(true);
// Detect keypoints
list<SiftKeypoint> kpt_list1, kpt_list2;
printf("\nSIFT detection on image 1 ...\n");
sift_cpu(image1, kpt_list1, true);
printf("# keypoints in image1: %d\n", kpt_list1.size());
printf("\nSIFT detection on image 2 ...\n");
sift_cpu(image2, kpt_list2, true);
printf("# keypoints in image2: %d\n", kpt_list2.size());
// Save keypoint list, and draw keypoints on images.
char filename[255];
sprintf(filename, "s_A_keypoints.ppm");
draw_keypoints_to_ppm_file(filename, image1, kpt_list1);
export_kpt_list_to_file("s_A_keypoints.key", kpt_list1, true);
sprintf(filename, "s_B_keypoints.ppm");
draw_keypoints_to_ppm_file(filename, image2, kpt_list2);
export_kpt_list_to_file("s_B_keypoints.key", kpt_list2, true);
// Match keypoints.
list<MatchPair> match_list;
match_keypoints(kpt_list1, kpt_list2, match_list);
// Draw result image.
sprintf(filename, "s_A_B_matching.ppm");
draw_match_lines_to_ppm_file(filename, image1, image2, match_list);
printf("# of matched keypoints: %d\n", match_list.size());
return 0;
}
ezsift.h
#ifndef EZSIFT_H
#define EZSIFT_H
#include "util/image.h"
#include "util/img_io.h"
#include <vector>
#include <list>
.
.
.
#endif
#define DEGREE_OF_DESCRIPTORS (128)
// Enable doubling of original image.
void double_original_image(bool doubleFirstOctave);
// Detect keypoints and extract descriptor.
int sift_cpu(
const ImageObj<uchar> &image,
std::list<SiftKeypoint> & kpt_list,
bool bExtractDescriptors);
// Match keypoints from two keypoint lists.
int match_keypoints(
std::list<SiftKeypoint> & kpt_list1,
std::list<SiftKeypoint> & kpt_list2,
std::list<MatchPair> & match_list);
.
.
.
#endif
(I will not include all the code here, I just wanted to give an idea. It is ease to download the source and see the files that I am talking about)
Also there is a "ezsift.cpp" file with more files in the "util" folder
I'm trying to run it from the terminal. I go to the directory and type "gcc image_match.cpp" but is says that double_original_image, sift_cpu (and all other functions in the header file) are not defined.
more specifically is gives this error:
image_match.cpp:(.text+0x1c7): undefined reference to `double_original_image(bool)'
image_match.cpp:(.text+0x20d): undefined reference to `sift_cpu(ImageObj<unsigned char> const&, std::list<_SiftKeypoint, std::allocator<_SiftKeypoint> >&, bool)'
(these are not the only error of course. It gives same error for all the functions)
I then tried "gcc ../ezsift.cpp image_match.cpp" but the same error.
What am I doing wrong?? I am running ubuntu 14.04.
(expending Cameron's comment into a tested answer)
You need to compile both ezsift.cpp and img_io.cpp with your program (image_match.cpp), while providing the location of the header files (-I\path\to\directory). Here is a working snippet, assuming you are in the examples directory:
g++ image_match.cpp ../ezsift.cpp ../util/img_io.cpp -I.. -o image_match

Linking error in OpenCV programs when using third party libraries

I am trying to use graph library using opencv.
This is the code that I have written , and am building this on Visual studio 2010.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "GraphUtils.h"
using namespace cv;
using namespace std;
int main()
{
float floatArray[4]= {1,1,2,2};
showFloatGraph("Rotation Angle", floatArray, 4 );
return 0;
}
There is no compilation error.
However, I am getting following linking error:
Error 1 error LNK2019: unresolved external symbol _showFloatGraph
referenced in function _main C:\Users\Yam\Documents\Visual Studio
2010\Projects\plot\opencv1\helloworld.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\Yam\Documents\Visual Studio
2010\Projects\plot\Debug\opencv1.exe
I have put the two files (GraphUtils.h and GraphUtils.c) with my .cpp file.
Why I am getting these compilation errors whenever I am using third party libraries (.c and .h) files, since I get this linking error also when using other library. I asked the question here
In Properties=>Linker=>input=>Additional dependencies , I have added :
opencv_core231d.lib;opencv_highgui231d.lib;opencv_imgproc231d.lib;opencv_features2d231d.lib;opencv_calib3d231d.lib;%(AdditionalDependencies)
And in fact my basic opencv program for image display works fine.
Do I need to do something special when using these libraries. As of now I just copy paste the .c and .h files in the folder where my .cpp file is present. Do I need to do something more ?
Update:
I found that the the file GraphUtils.c has the following code:
// OpenCV
#include <cv.h>
#include <cxcore.h>
#ifdef USE_HIGHGUI
#include <highgui.h>
#endif
I found that the cv.h, cxcore.h and highgui.h are actually present in opencv 2.3.1/opencv folder and so I changed it like this:
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#ifdef USE_HIGHGUI
#include <opencv/highgui.h>
#endif
Unfortunately this gave rise to more linking errors.
Update
The following program works fine.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include<conio.h>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("xyz.bmp", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
getch();
return 0;
}
But this program always gives error; no header file recognized. so no function would work:
#include "cv.h" //main OpenCV header
#include "highgui.h" //GUI header
int main() {
// declare a new IplImage pointer
IplImage* myimage;
// load an image
myimage = cvLoadImage("sayyidsmile.jpg",1); //change the file name with your own image
//create a new window & display the image
cvNamedWindow("Smile", 1);
cvMoveWindow("Smile", 100, 100);
cvShowImage("Smile", myimage);
//wait for key to close the window
cvWaitKey(0);
cvDestroyWindow( "Smile" );
cvReleaseImage( &myimage );
return 0;
}
Details on Env. Variables
I followed this article to set EVs.
Currently my variables are like this:
Name Value
OpenCV: C:\Users\Yuvue\Desktop\OpenCV2.3.1
INCLUDE: %OPENCV%\build\include
LIB: %OPENCV%\build\x86\vc10\lib
Path: %OPENCV%\build\x86\vc10\bin
Under Properties => VC++ Directories=> Library Directories, I have added the following:
$(OPENCV)\build\x86\vc10\lib
The second program compiles when I explicitly gives path of cv.h in the include. But if I do so with any of the third party libraries when they have used #include, it starts giving linking error (my original question).
Please help me sort this!!

How to set 'nmixures' and 'bShadowDetection' values in 'BackgroundSubtractorMOG2'?

Please have a look at the following code
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
using namespace cv;
using namespace std;
Mat frame,back,fore;
int main()
{
VideoCapture cam;
BackgroundSubtractorMOG2 bgs(0,0,false);
vector<vector<Point>>contours;
bgs.setInt("nmixtures",3);
cam.open(0);
if(!cam.isOpened())
{
cout << "Cam not Found";
return -1;
}
namedWindow("Frame");
while(true)
{
cam>>frame;
imshow("Frame",frame);
if(waitKey(30)>=0)
{
break;
}
}
}
I am trying to set the value of nmixures of BackgroundSubtractorMOG2 into 3 and bShadowDetection of BackgroundSubtractorMOG2 into false.
However, as with OpenCV 2.4.5, these values are set to private, so I can't acess them directly. I managed to set the value of bShadowDetection via the constructor (Eventhough I dnt know what other 2 params are), and I couldn't find a way to set the nmixers. I don't know whether The way I set the nmixures is correct or not,because in the article I read, the writer says "Set them via constructors in case of opencv 2.4"
Can you please tell me how to set those 2 values?
In OpenCV 2.4.8.:
bgs.set("nmixtures", 3);
bgs.set("detectShadows", false);
In your case, you have to write:
BackgroundSubtractorMOG2 bgs;
bgs.setInt("nmixtures", 3);
bgs.setBool("detectShadows", false);