Trying to compile character_recognition.cpp from opencv_contrib samples - opencv

I've just installed opencv 3.2 and tesseract module. Now I'm tryng to compile the character_recognition.cpp sample in the opencv_contrib folder. If I use the command pkg-config --cflags --libs opencv it displays
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_freetype -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
But if I try to compile che cpp file
g++ -ggdb `pkg-config --cflags --libs opencv` character_recognition.cpp -o character_recognition
it says me that cv::imread(cv::String const&, int) it's not defined and this kind of error for every function of the opencv libraries that is used in the code.
Seems thats I can't link the libraries. Can you help me?

Related

Why do I get an error when compiling opencv code with make even if the particular missing files exist?

I've got the following makefile:
OBJS:= main.o
CV_LIBS:= -I/usr/local/include/opencv4 -I/usr/local/include -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
main: $(OBJS)
g++ $(OBJS) $(CV_LIBS) -O3 -ffast-math -o main -Wall -g
main.o: main.h
g++ -c main.cpp -Wall -g
My main.h file has the following line:
#include <opencv4/opencv2/objdetect.hpp>
I get the following error:
/usr/local/include/opencv4/opencv2/objdetect.hpp:47:10: fatal error: opencv2/core.hpp: No such file or directory
47 | #include "opencv2/core.hpp"
I have tried the following as well with no luck:
#include <opencv2/objdetect.hpp>
Error:
main.h:4:10: fatal error: opencv2/objdetect.hpp: No such file or directory
I can confirm there is an objdetect.hpp in both:
/usr/local/include/opencv4/opencv2/ and
/usr/local/include/opencv4/opencv2/objdetect.hpp
I have looked at this question, but unfortunately I'm not using cmake. What am I doing wrong? Many thanks.
If you look at the compile line that make prints out (which you didn't include in your question), it's pretty straightforward why you get this error.
The compile line, that compiles main.c into main.o, looks like this:
g++ -c main.cpp -Wall -g
You can clearly see that there is no reference to the directories you need to include header files here.
Your makefile has this:
CV_LIBS:= -I/usr/local/include/opencv4 -I/usr/local/include -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
where you are combining compiler flags, such as -I, with linker flags, such as -L and -l in the same variable, calling them all "LIBS", and adding them only to the link line.
You need to put the compiler flags into the compile command, and the linker flags into the link command:
CPPFLAGS = -I/usr/local/include/opencv4 -I/usr/local/include
CV_LIBS = -L/usr/local/lib/ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
main: $(OBJS)
g++ $(OBJS) $(CV_LIBS) -O3 -ffast-math -o main -Wall -g
main.o: main.h
g++ $(CPPFLAGS) -c main.cpp -Wall -g
FYI, it's more correct for you to use #include <opencv2/objdetect.hpp> in your source code, not #include <opencv4/opencv2/objdetect.hpp>

How to write compile.sh file for facerec_fisherfaces?

On Ubuntu 14.4 with OpenCV 3.1.0 :
I'm trying to compile and run this file http://docs.opencv.org/3.0-last-rst/_downloads/facerec_fisherfaces.cpp
but I don't know what to write in the compile.sh file to make it executable
I found this compile.sh but it doesn't work :
LIBS="-lopencv_imgproc -lopencv_highgui -lopencv_core -lopencv_objdetect"
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/lib -L/usr/local/lib -fpic -Wall -c "untitled.cpp" $LIBS
g++ -shared -I/usr/local/include/opencv -I/usr/local/include/opencv2 -o libuntitled.so untitled.o -L/usr/local/lib $LIBS
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -o untitled untitled.o -L/usr/local/lib $LIBS
exit 0
A good answer for my own question :)
you'll need to rebuild opencv with opencv_contrib (please see readme there for build instructions).
please see 3.1 docs http://docs.opencv.org/ref/master/tutorial_face_main.html#gsc.tab=0, not the old 3.0 ones
LIBS="-lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lopencv_core -lopencv_objdetect -lopencv_face"
the old 2.4 samples won't run as-is, please have a look at the current https://github.com/Itseez/opencv_contrib/tree/master/modules/face/samples

No such file or directory #include "opencv2/face.hpp" while compiling facerec_fisherfaces with c++ in ubuntu?

I'm new to Opencv version 3.1.0 and trying to compile the code of facerec_fisherfaces.cpp http://docs.opencv.org/3.0-last-rst/_downloads/facerec_fisherfaces.cpp
using this compile.sh file
LIBS="-lopencv_imgproc -lopencv_highgui -lopencv_core -lopencv_objdetect -lopencv_contrib"
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/lib -L/usr/local/lib -fpic -Wall -c "untitled.cpp" $LIBS
g++ -shared -I/usr/local/include/opencv -I/usr/local/include/opencv2 -o libuntitled.so untitled.o -L/usr/local/lib $LIBS
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -o untitled untitled.o -L/usr/local/lib $LIBS
exit 0
the error in the terminal is :
No such file or directory
#include "opencv2/face.hpp"
A proper answer to my question :)
you need to download so extra modules from :
https://github.com/Itseez/opencv_contrib

DSO missing from command line opencv

I ran this command
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary DisplayImage.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching
I am getting this error
/usr/bin/ld: /tmp/ccB73ml3.o: undefined reference to symbol
'_ZN2cv6imreadERKNS_6StringEi'
//usr/local/lib/libopencv_imgcodecs.so.3.0: error adding symbols: DSO
missing from command line
I dont know what order it should be the linking

opencv 2.4 SIFT compile error

openCV has changed feature detectors/descriptors (as usually) with new version. I've already found, that I need to add
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/nonfree/features2d.hpp"
however, it still doesn`t work. The error I get is:
error: ‘cv::SIFT::DetectorParams’ has not been declare
I use standard command for building
gcc `pkg-config --cflags --libs opencv` -o descriptorExtractor main.cpp
and all the libraries, etc. should be linked correctly
pkg-config --cflags --libs opencv
-I/opt/ros/fuerte/include/opencv -I/opt/ros/fuerte/include -L/opt/ros/fuerte/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_ts -lopencv_video -lopencv_videostab
What's wrong with openCV again? Please, don't ask me why do I need SIFT from openCV and not from some other software or binary, I just need it...
I do not remember it well... but I think the issue was that I didn't use
cv::initModule_nonfree();

Resources