Compiling hello world with opencv c++ on mac os x sierra - c++

First time I cannot compile hello world.
I've followed tons of tutorials how to install opencv.
I just have following example:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat imageMe = imread("test.jpg", 0);
return 0;
}
from opencv website.
Looks straightforward, but it won't compile.
g++ display_image.cpp `pkg-config --cflags --libs opencv` -l
Here's the error:
display_image.cpp:2:10: fatal error: 'opencv2/core/core.hpp' file not found
#include <opencv2/core/core.hpp>
^
1 error generated.
And running:
pkg-config --cflags --libs opencv
return this:
-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
these files are present.
Running ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp
Gives the result:
core.hpp
What's the problem?
edit:
I've been using following tutorials to install opencv on my machine:
http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/
https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/
https://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/
http://seeb0h.github.io/howto/howto-install-homebrew-python-opencv-osx-el-capitan/
edit:
Even after all these answers I cannot compile a single app. Even if it somehow can find .hpp file directly included in my file it fails finding includes inside.
Probably my configuration is wrong.
In /usr/local/Cellar/opencv3 I have 2 directories:
3.2.0 - probably installed binaries
HEAD-9053839 - compiled from source
Now I also have opencv.pc file located in /usr/local/lib/pkgconfig:
prefix=/usr/local/Cellar/opencv3/3.2.0
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
As a prefix I've also tried HEAD-9053839.
I've created even simplier example:
#include <opencv2/opencv.hpp>
int main(){return 0; }
It's still telling me that 'opencv2/opencv.hpp' file not found.
Then I printed what pkg-config says:
pkg-config --cflags --libs opencv
-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
And found out it'd rather should be directly include dir, not opencv/opencv2 subdirectories.
So I've ran g++ like this:
g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l display_image.cpp
Then it told me:
ld: library not found for -lopencv_contrib
After removing it it said:
ld: library not found for -lopencv_legacy
Then I had to remove -l before display_image.cpp, but then I've got another error:
Undefined symbols for architecture x86_64:
"cv::String::deallocate()", referenced from:
cv::String::~String() in display_image-9d8f86.o
cv::String::operator=(cv::String const&) in display_image-9d8f86.o
This is hell.

I don't have a compilation environment to regenerate your error but I think the problem is about these lines in your code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
since the opencv directory is being searched from
/usr/local/Cellar/opencv3/3.2.0/include/opencv2
as your "pkg-config --cflags --libs opencv" implies. So, I suppose, changing the include lines into
#include <core/core.hpp>
#include <highgui/highgui.hpp>
would help, or you can implicitly declare the directory you want to search like this:
g++ -I/usr/local/Cellar/opencv3/3.2.0/include display_image.cpp ...

In OpenCV 3.x, the layout of the header files has changed and now you pretty much just use:
#include <opencv2/opencv.hpp>
So, in concrete terms, I am suggesting you replace your 2nd and 3rd lines with the above incantation.
See the OpenCV Transition Guide.

Soner's answer should solve the OP's problem. I want to add some other solutions.
If you use CMake to install openCV from source, it will put OpenCV libraries in /usr/local/lib and OpenCV headers in /usr/local/include. Where the headers all go in the subfolder /usr/local/include/opencv. Since they are default library and header directories for most IDE's, all the examples you find will work with that config.
Now, brew does not install OpenCV to the above folders as you already knew. So you have three options:
Change compiler's search paths for OpenCV libraries and headers: g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib
Soft link /usr/local/Cellar/opencv3/3.2.0/include/opencv to /usr/local/include and /usr/local/Cellar/opencv3/3.2.0/lib to /usr/local/lib
Install OpenCV from source with CMake.

I did it!
It was a problem with pkg-config.
In /usr/local/Cellar/opencv3/3.2.0/lib/pkgconfig I've found opencv.pc, which looks like this:
# Package Information for pkg-config
prefix=/usr/local/Cellar/opencv3/3.2.0
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -framework OpenCL -L/usr/local/opt/jpeg/lib -ljpeg -L/usr/local/lib -lpng -ltiff -lImath -lIlmImf -lIex -lHalf -lIlmThread -L/usr/lib -lz -framework Cocoa -framework AVFoundation -framework CoreGraphics -framework CoreMedia -framework CoreVideo -framework QuartzCore -framework Accelerate
Cflags: -I${includedir_old} -I${includedir_new}
I've replaced old opencv.pc located in: /usr/local/lib/pkgconfig
Now I'm not getting any errors.
I also had to use different include file:
#include <opencv2/opencv.hpp>

Related

G++ returning errors after attempted compilation with openCV using G++

I'm trying to run a simple c++ file that includes openCV libraries and creates a simple Mat. Unfortunately, when I try and compile the file using g++, it returns a number of errors.
So far, I've tried removing the Mat definition all together and just included the openCV files, which worked! I'm new to the command line, so I'm not sure if it's an issue with the way I'm using g++. However, I have worked quite a bit on openCV in Xcode (though with Xcode, there's no need to work in the command line, as everything is neatly built for you at run time).
Also, this is my first time posting to Stack Overflow, so I may have made a mistake formatting.
This is my main.cpp file which I'm attempting to compile. As I mentioned before, if I remove the line "Mat test;", it compiles successfully and writes to console as it should.
#include <iostream>
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
int main() {
Mat test;
cout << "Mat Defined!" << endl;
return(0);
}
This is the g++ command I am using:
g++ -o main.out main.cpp
and this is the error I get when I try and run it:
/usr/bin/ld: /tmp/ccbbhoNd.o: in function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/ccbbhoNd.o: in function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
Any help is greatly appreciated. I also opted out of the verbose flag for g++, as it returned a ton of lines and I wasn't sure how people feel about 50+ lines of code. Thanks so much!
You have problem with linking to OpenCV library. You need to pass to ld program information where to find all necessary lib's. In your case it will be:
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -o main.out main.cpp -lopencv_core
All OpenCV linker flags are:
-lopencv_calib3d
-lopencv_contrib
-lopencv_core
-lopencv_features2d
-lopencv_flann
-lopencv_highgui
-lopencv_imgproc
-lopencv_legacy
-lopencv_ml
-lopencv_nonfree
-lopencv_objdetect
-lopencv_photo
-lopencv_stitching
-lopencv_superres
-lopencv_ts
-lopencv_video
-lopencv_videostab
After all I suggest to jump in CMake build system for your C++ project's. With OpenCV it's much easier to compile program. Checkout doc's. CMake will prepare for you makefile with all needed dependencies.
Best Regards!
It fails because you didn't link the libraries in your command. During compilation, your operating system can't reach opencv libraries because you didn't address them. You need to compile your cpp file using the code below:
g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -o output

Running OpenCV in Mac OS terminal with opencv_contrib

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.

OpenCV "Undefined reference to 'cv::imread' etc C++

So I've written some very basic OpenCV C++ code to test it's functionality. I am using CodeBlocks on Linux Ubuntu 16.04. I have installed OpenCV correctly, I think, Codeblocks offers 'OpenCV Project' as an option when making a project and whenever I start typing in an OpenCV keyword, it'll expand and suggest keywords.
My problem is that when I attempt to build it, I get
undefined reference to `cv::imread(std::__cxxll::basic_string<char, std::char_traits<ch..
And it repeats this for every opencv function.
I read somewhere that this may have something to do with the archive files and so I'll provide the results when I enter:
pkg-config opencv --libs
into the terminal. The output is:
-L/usr/local/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 -latomic
-ltbb -lGL -lGLU -lrt -lpthread -lm -ldl
So how can I solve this issue? Should I edit my files in some way?
Thanks very much in advance
Your code has been compiled with a standard library using the std::__cxx11 namespace. Ie, you're probably using g++ 5+ which has a different ABI to previous versions. The opencv libraries you've installed are probably an older ABI, and therefore not compatible.
You can either compile the opencv libraries yourself, use an earlier compiler that matches the ABI of the libries you have, or configure your compiler to use the old ABI.
You can use the macro _GLIBCXX_USE_CXX11_ABI if you're using g++ 5. Before you include and std libs:
#define _GLIBCXX_USE_CXX11_ABI 0

opencv creates faulty pkg-config files

If I run the straight installation of opencv like this:
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX="/some/path" ..
make install
opencv creates a faulty pkg-config file like this:
# Package Information for pkg-config
prefix=/some/path
exec_prefix=${prefix}
libdir=
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.4.10
Libs: ${exec_prefix}/lib/libopencv_calib3d.so ${exec_prefix}/lib/libopencv_contrib.so ${exec_prefix}/lib/libopencv_core.so ${exec_prefix}/lib/libopencv_features2d.so ${exec_prefix}/lib/libopencv_flann.so ${exec_prefix}/lib/libopencv_gpu.so ${exec_prefix}/lib/libopencv_highgui.so ${exec_prefix}/lib/libopencv_imgproc.so ${exec_prefix}/lib/libopencv_legacy.so ${exec_prefix}/lib/libopencv_ml.so ${exec_prefix}/lib/libopencv_nonfree.so ${exec_prefix}/lib/libopencv_objdetect.so ${exec_prefix}/lib/libopencv_ocl.so ${exec_prefix}/lib/libopencv_photo.so ${exec_prefix}/lib/libopencv_stitching.so ${exec_prefix}/lib/libopencv_superres.so ${exec_prefix}/lib/libopencv_ts.a ${exec_prefix}/lib/libopencv_video.so ${exec_prefix}/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
Which produces outputs, which can't be used directly for the compiler, or by cmake:
$ pkg-config opencv --libs
/sct/homes3/opencv/opencv_2_4_10/lib/libopencv_calib3d.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_contrib.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_core.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_features2d.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_flann.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_gpu.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_highgui.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_imgproc.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_legacy.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_ml.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_nonfree.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_objdetect.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_ocl.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_photo.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_stitching.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_superres.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_ts.a /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_video.so /sct/homes3/opencv/opencv_2_4_10/lib/libopencv_videostab.so -lrt -lpthread -lm -ldl
You can see that the libraries of opencv are given by their full path, and that no compiler flags are given.
A proper version of the opencv.pc should look like this:
# Package Information for pkg-config
prefix=/some/path
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.4.10
Libs: -L${libdir} -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 -lrt -lpthread -lm -ldl
Cflags: -I${includedir_old} -I${includedir_new}
So why is opencv/cmake producing such a pkg-config file?
Or am I doing something wrong with the make install?

unable to compile ffmpeg bohr on ubuntu 14.04 32bit

I'm developing an application to capture video from webcam and stream it to Android. I'm using ffmpeg latest release - 2.5.2 "Bohr" on Ubuntu 14.04 32bit and using Eclipse as IDE.
I'm receiving this error when compiling:
g++ -L/usr/local/lib -L/home/idanhahn/ffmpeg/ffmpeg_build/lib -o "camera" ./src/.metadata/.plugins/org.eclipse.cdt.make.core/specs.o ./src/CameraSec.o ./src/camera.o ./.metadata/.plugins/org.eclipse.cdt.make.core/specs.o -lz -lswscale -lopencv_core -lavcodec -lavutil -lpthread -lboost_thread -lboost_system -lboost_date_time -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -lavformat
/usr/bin/ld: /home/idanhahn/ffmpeg/ffmpeg_build/lib/libavformat.a(http.o): undefined reference to symbol 'inflateInit2_'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libz.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I've linked avformat (and other ffmpeg related libs).
I've tried the following:
Linked libz.
Tried to recompile using instructions from here: http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
What could be the problem?
Why linker points to i686 and then go back to i386?
You are really only missing an additional library here. Just add -llzma to the end of your compilation line.
I additionally had to add other missing libraries. Just append in case you are facing the same problem:
-lswresample -lm -lz
It is because libavcodec includes some math and zlib headers, so you must link to the respective libraries as well. This is also the case for lzma.
then you need to put -llzma with the compilation line of ffmpeg.
or i have an alternative to do it via simpler method.
Try this: http://ubuntuforums.org/showthread.php?t=2219550&p=13101922#post13101922
it will be simple..