In order to install OpenCV on my Mac, I opted to use brew install opencv instead of building from source as is specified in the Mac installation tutorials. Homebrew's installation path appears to be /opt/homebrew, and all of the included packages that I have downloaded appear to be contained in the include in that directory.
I wrote a sample program program in order to test the installation:
#include <opencv4/opencv2/imgcodecs.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
VideoCapture cap(0);
Mat img;
while (1) {
cap.read(img);
imshow("Image", img);
if (waitKey(1) == 27) {
break;
}
}
return 0;
}
And, to compile it, I ran:
g++ video.cpp `pkg-config --cflags --libs opencv4`
which returned:
fatal error: 'opencv4/opencv2/imgcodecs.hpp' file not found
I ran pkg-config --cflags --libs opencv4 to check whether it was set correctly, and the following was returned:
-I/opt/homebrew/Cellar/opencv/4.5.2_4/include/opencv4 -L/opt/homebrew/Cellar/opencv/4.5.2_4/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
I think that this implies that the path to OpenCV is set for linking the package during compilation, so I was wondering about why the linker couldn't find it. Also, my end-goal is to write a program involving pcl, and I also installed pcl via brew. Since it is also contained in opt/homebrew/, do I have to create a specific reference to its path outside of /usr/ when making the cake file? Thanks!
Thank you #Alan Birtles. It was just a problem with my includes. For those reading, #include <opencv4/opencv2/imgcodecs.hpp>, as well as all of the other OpenCV packages, had to refer directly to OpenCV2. So, for example, it had to be #include <opencv2/imgcodecs.hpp>
Related
I am trying to compile a program using this command:
g++ -std=c++11 -I"/usr/local/Cellar/opencv/4.5.0_5/include/opencv4/opencv2/" -I"/usr/include/python2.7" stl.cpp -o stl -ldl -lpthread -lrt -lopencv_core -lpython2.7
However, I keep getting this error:
fatal error: 'opencv2/core.hpp' file not found
I changed my -I include path to point to my version of OpenCV that has opencv2/core.hpp properly installed. Am I missing something? This approach has worked for other compilations...
EDIT
After trying the suggestions below, I'm still having no luck.
I added the pkg-config suggestion like below:
g++ -std=c++11 -I"/usr/include/python2.7" stl.cpp -o stl -ldl -lpthread -lrt -lopencv_core -lpython2.7 `pkg-config --cflags --libs opencv4`
but I still get "opencv2/core.hpp" not found. This is weird because when I run:
pkg-config --cflags --libs opencv4
I get:
-I/usr/local/Cellar/opencv/4.5.0_5/include/opencv4 -L/usr/local/Cellar/opencv/4.5.0_5/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
which makes me think it should work.
I also tried changing #include opencv2/core.hpp to #include core.hpp. This gets me closer, but then I get an error in my actual opencv2/core.hpp file saying it can't find the includes anymore. I don't think I want to start editing include paths in the original opencv library.
It looks like your include path is a bit wrong.
With your -I path you are looking at ...opencv4/opencv2/. And when you include core.hpp you are including it with (presumably):
#include "opencv2/core.hpp"
So the compiler is basically looking for core.hpp at: ...opencv4/opencv2/opencv2/core.hpp.
Try using ...include/opencv4/ instead. Or include like so:
#include "core.hpp"
I'm trying to compile and run OpenCV through the command line. I can get the main Open CV libraries to work however I think my problem is with using the opencv_contrib libraries.
I have installed Open CV with home brew.
I can compile and run the below code fine from another SO question:
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat src = Mat(Size(320,240),CV_64F);;
namedWindow("test");
cout << "press any key to close" << endl;
while(true){
randn(src,0,1.0);
imshow("test",src);
if(waitKey() > 0) break;
}
}
This is compiled like this:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -lopencv_core -lopencv_highgui -o cv
Then ran ./main
However when I try and run anything with the opencv_contrib libraries I get this error when compiled like this:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv
error:
Undefined symbols for architecture x86_64:
"cv::xfeatures2d::SURF::create(double, int, int, bool, bool)", referenced from:
_main in cv-f48298.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I believe this is likely due to not specifying the opencv_contrib libraries. I understand they come installed with homebrew. In my Cellar directory I have a directory for opencv and opencv_contrib. I'm not sure if the opencv_contrib directory needs to be located within the opencv directory.
But I believe everything is there as with this:
pkg-config --libs --cflags opencv
It outputs:
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
But then if I add that to my command like:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" pkg-config --libs --cflags opencv -o cv
I get this:
clang: error: unsupported option '--libs'
clang: error: unsupported option '--cflags'
clang: error: no such file or directory: 'pkg-config'
clang: error: no such file or directory: 'opencv'
I'm able to compile the same code in Xcode and I only needed to add the search paths for the openCV directory and add the libs within there. I can run the code by opening the products folder in finder and running ./cv with images passed in. But I'm struggling with doing this all through the command line.
Any help would be great!
You want to add the output of the pkgconfig tool (you listed it above: -I/usr/local/include/opencv ...) to your compilation command line to compile correctly.
You could just manually copy this output text into your compiler invocation command:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv
This is usually automated by calling pkgconfig inline:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" `pkg-config --libs --cflags opencv` -o cv
Notice the backticks in this though - they are missing in your command line. With the backticks pkgconfig will be executed and the output (which you know what it looks like) will be inserted into the command line.
I would like to compile a C++ program that uses openCV statically, so that I can use it on other computers without having to install openCV.
I compiled openCV 2.4.5 with following flags in order to get static libraries and to install it besides my main installation which is version 3.1.
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/opt/openCV_2_4_5 -D WITH_FFMPEG=OFF -DBUILD_SHARED_LIBS=NO ..
ls /opt/openCV_2_4_5/lib
libopencv_calib3d.a libopencv_flann.a libopencv_legacy.a libopencv_photo.a libopencv_video.a
libopencv_contrib.a libopencv_gpu.a libopencv_ml.a libopencv_stitching.a libopencv_videostab.a
libopencv_core.a libopencv_highgui.a libopencv_nonfree.a libopencv_superres.a pkgconfig
libopencv_features2d.a libopencv_imgproc.a libopencv_objdetect.a libopencv_ts.a python2.7
After installing, I tried to compile a c++ program for face detection like this:
g++ -std=c++11 -L/opt/openCV_2_4_5/lib -I/opt/openCV_2_4_5/include -o ex2 ex2.cpp -L. -lpthread -lz -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
From that I got the error:
/tmp/cct4prBn.o: In function `main':
ex2.cpp:(.text+0x141): undefined reference to `cv::imread(std::string const&, int)'
ex2.cpp:(.text+0x2bc): undefined reference to `cv::imread(std::string const&, int)'
ex2.cpp:(.text+0x4f3): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
make_2_4_5:9: recipe for target 'ex2' failed
make: *** [ex2] Error 1
I used the same library flags as my installation of OpenCV 3.1 has. I got them from:
pkg-config --cflags --libs opencv
The -L and -I flags point to the openCV 2.4.5 installation in the /opt directory.
Why doesn't it compile? All OpenCV libraries are listed in the section where the static libraries should be.
In OpenCV 2 I believe cv::imread() and cv::imwrite() are in the highgui library. In OpenCV 3 they were moved to the imgcodecs library. Try adding:
-lopencv_highgui
to your command line.
I am using xcode to compile my opencv project, and I have some settings as below:
HEADER_SEARCH_PATHS = /usr/local/include
LIBRARY_SEARCH_PATHS = /usr/local/lib
OTHER_LDFLAGS = -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab
I want to know what shall I write by terminal command rather than setting of xcode.
If you installed OpenCV with homebrew and you also installed the pkgconfig package with homebrew, the package can tell you the settings you need itself - far more accurately than you can guess them.
The easy way is to ask pkgconfig to list all the packages it knows about:
pkg-config --list-all | grep -i opencv
opencv OpenCV - Open Source Computer Vision Library
So, now you know the package name is plain and simple opencv, and you can find the flags you need like this:
pkg-config --cflags --libs opencv
-I/usr/local/Cellar/opencv/2.4.12_2/include/opencv \
-I/usr/local/Cellar/opencv/2.4.12_2/include \
-L/usr/local/Cellar/opencv/2.4.12_2/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_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
Which means your compilation and linking becomes simply:
g++ $(pkg-config --cflags --libs opencv) program.cpp -o program
If you do that in a Makefile, you will need to double up the $ signs.
If your system is not so well installed, you may need to find the pkgconfig file yourself. So you would do:
find /usr/local -name "opencv*pc"
/usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
Then you can access that file specifically like this:
pkg-config --cflags --libs /usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
You can use:
g++ -I ${HEADER_SEARCH_PATHS} -L ${LIBRARY_SEARCH_PATHS} ${OTHER_LDFLAGS} ${SOURCE_FILES}
I has compiled and linked OpenCV project with Visual C++ Express under Windows.
With GCC under Ubuntu is compiled but is link error:
finder.cpp|219|undefined reference to `cv::imread(cv::String const&, int)'|
I have added libraries:
-lopencv_calib3d
-lopencv_core
-lopencv_features2d
-lopencv_flann
-lopencv_highgui
-lopencv_imgproc
-lopencv_ml
-lopencv_nonfree
-lopencv_objdetect
-lopencv_photo
-lopencv_stitching
-lopencv_superres
-lopencv_ts
-lopencv_video
-lopencv_videostab
and for example cv:line proceure is OK, but only one error (so far) imread