Compiling simple openCV project - c++

Attempting to compile a very simple program to test out my recent installation of OpenCV 3.2.0. My program opencvtest.cpp is in a folder called cpp, which also holds the opencv-3.2.0 installation directory.
Here's my program.
//Include file for every supported OpenCV function
#include <opencv2/opencv.hpp>
int main( int argc, char** argv ) {
cv::Mat img = cv::imread( argv[1], -1 );
if( img.empty() ) return -1;
cv::namedWindow( "Example 2-1", cv::WINDOW_AUTOSIZE );
cv::imshow( "Example 2-1", img );
cv::waitKey( 0 );
cv::destroyWindow( "Example 2-1" );
return 0;
}
Below I've included the call to pkg-config to show what parameters I'm using, and my actual g++ command to try and compile my file.
asif.ahmed:~/cpp $ pkg-config opencv --cflags --libs
-I/usr/include/opencv -I/usr/include/opencv2 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
asif.ahmed:~/cpp $ g++ opencvtest.cpp `pkg-config opencv --cflags --libs` -o main
opencvtest.cpp:2:10: fatal error: 'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
^
1 error generated.
Can anyone else suggest anything to get this to compile? I don't have a great grasp of command line compiling C++.

Check path to opencv.hpp (is it really /usr/include/opencv2/opencv2/opencv.hpp?) - If you use -I/usr/include/opencv2 perhaps you need #include "opencv.hpp" instead of #include <opencv2/opencv.hpp>, or better use -I/usr/include/ and #include "opencv2/opencv.hpp"

Related

program comiled ok using pkg-config --cflgas opencv and pkg-config --ldflag but not executable (ubuntu 16.04)

Recently I installed opencv on my ubuntu 16.04 machine(I've done this many times before and used it ok). I tried compiling yolo program, it compiled ok and ran ok. When I set OPENCV=1 in the Makefile, it compiles the program with opencv. To set the compile and link flags it has these two lines.
LDFLAGS+= `pkg-config --libs opencv` -lstdc++
COMMON+= `pkg-config --cflags opencv`
My pkg-config commands work correctly. See the output below.
ckim#chan-ubuntu:~/YOLOV2/darknet$ pkg-config --cflags opencv
-I/home/ckim/opencv_install/installation/OpenCV-3.4/include/opencv -I/home/ckim/opencv_install/installation/OpenCV-3.4/include
ckim#chan-ubuntu:~/YOLOV2/darknet$ pkg-config --libs opencv
-L/home/ckim/opencv_install/installation/OpenCV-3.4/lib -lopencv_stitching -lopencv_videostab -lopencv_superres -lopencv_surface_matching -lopencv_dnn_objdetect -lopencv_line_descriptor -lopencv_aruco -lopencv_img_hash -lopencv_xobjdetect -lopencv_dpm -lopencv_freetype -lopencv_stereo -lopencv_face -lopencv_objdetect -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_saliency -lopencv_hfs -lopencv_ccalib -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_optflow -lopencv_ximgproc -lopencv_fuzzy -lopencv_bioinspired -lopencv_highgui -lopencv_videoio -lopencv_text -lopencv_dnn -lopencv_reg -lopencv_hdf -lopencv_bgsegm -lopencv_rgbd -lopencv_sfm -lopencv_xfeatures2d -lopencv_calib3d -lopencv_shape -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_ml -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
A couple of days ago, I tried compiling a simple opencv pgrogram that I find on a web page and tried compiling it. (Iwanted to test a basic display window's behavior). The program was like this. very typical)
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(1); // Wait for a keystroke in the window
return 0;
}
When I compile and link it with command
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
it gives me no error and I see the file test but it's not executable. When I run it after setting chmod +x test, I get
ckim#chan-ubuntu:~/opencvtest$ ./test
bash: ./test: cannot execute binary file: Exec format error
ckim#chan-ubuntu:~/opencvtest$ file test
test: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
What could be wrong?
As per the comment you're command line...
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
includes the -c flag which tells the compiler to compile only -- don't link. Also. as you're code is c++ rather than c you need to link with the correct runtime libraries. That means using g++ rather than gcc. So the command should be...
g++ test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test

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.

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

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>

Gtkmm-3.0 compiled code throwing Gtk+2 symbol error

I'm on Ubuntu 13.10 using gtkmm 3.0 dev package, compiling the following code:
#include <gtkmm.h>
#include <iostream>
#include "opencv2/opencv.hpp"
#include <chrono>
#include <thread>
#include <mutex>
//GASP! global vars!
std::mutex FRAME_MUTEX;
std::thread CV_THREAD;
cv::Mat FRAME, CLEAN;
Glib::Dispatcher dispatcher;
volatile bool THREADRUN;
void cvThread() {
THREADRUN = true;
cv::VideoCapture capture(0);
while(THREADRUN) {
FRAME_MUTEX.lock();
capture >> FRAME;
FRAME_MUTEX.unlock();
dispatcher.emit();
}
}
int main(int argc, char** argv)
{
Gtk::Main gtkMain(argc, argv);
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("main_window.glade");
Gtk::Window *mainWindow;
Gtk::Image *cleanDisplay;
Gtk::Image *evmDisplay;
builder->get_widget("main_window", mainWindow);
builder->get_widget("clean_display", cleanDisplay);
builder->get_widget("evm_display", evmDisplay);
dispatcher.connect([&]() {
FRAME_MUTEX.lock();
cv::cvtColor(FRAME, CLEAN, CV_BGR2RGB);
cleanDisplay->set(Gdk::Pixbuf::create_from_data(CLEAN.data, Gdk::COLORSPACE_RGB, false, 8, CLEAN.cols, CLEAN.rows, CLEAN.step));
cleanDisplay->queue_draw();
FRAME_MUTEX.unlock();
});
CV_THREAD = std::thread(&cvThread);
gtkMain.run(*mainWindow);
return 0;
}
Using the command:
$ g++ test.cpp -o uitest `pkg-config --cflags --libs gtkmm-3.0 opencv` -std=c++11
Compilation works fine, but when I run the executable, it fails with the following error:
(uitest:20300): Gtk-ERROR **: GTK+ 2.x symbols detected.
Using GTK+ 2.x and GTK+ 3 in the same process is not supported
Trace/breakpoint trap (core dumped)
This is my first attempt to delve into using gtkmm, and I don't have the foggiest idea of where to go to start debugging. I checked the glade file, and it has an xml comment at the top indicating that gtk+3 is required by the contents of that file. The answer here didn't help me much; I got the following output from following those instructions:
-lgtkmm-3.0 -latkmm-1.6 -lgdkmm-3.0 -lgiomm-2.4 -lpangomm-1.4 -lgtk-3 -lglibmm-2.4
-lcairomm-1.0 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0
-lcairo-gobject -lpango-1.0 -lcairo -lsigc-2.0 -lgobject-2.0 -lglib-2.0
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann
-lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml
-lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres
-lopencv_ts -lopencv_video -lopencv_videostab
Any suggestions on how to fix this issue?
Till the OpenCV 3 gets released you can use Gtk::Image or Gdk::pixbuf to read/write images and skip linking highgui wich is the cause of the problem.
Here is how you can convert Mat & pixbuf:
cv::Mat mat(cv::Size(pixbuf->get_width(),pixbuf->get_height()),
CV_8UC3, (uchar*)pixbuf->get_pixels(), pixbuf->get_rowstride());
RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data((guint8*)mat.data,
Gdk::COLORSPACE_RGB,false,8,mat.cols,mat.rows,mat.step);
The "false" parameter is for transparency.
It seems that OpenCV uses gtk+2. While there has been some effort to extend support to version 3, the latest on that is that the pull request has not been fully approved. Until then, one can only use gtk+2 when using OpenCV.
For those interested in supporting the effort, visit the pull request page on github.

compiling opencv in c++

i have a file with only import:
#include <iostream>
#include <stdio.h>
#include "cxcore.hpp"
#include "highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
}
and i try to compile with g++ -I/usr/include/opencv -lopencv -lm m.cpp
but get whit error:
In file included from /usr/include/opencv/cxcore.hpp:46,
from m.cpp:5:
/usr/include/opencv/cxmisc.h:214: error: expected constructor, destructor, or type conversion before ‘void’
/usr/include/opencv/cxmisc.h:220: error: expected constructor, destructor, or type conversion before ‘int’
/usr/include/opencv/cxmisc.h:226: error: ‘CV_INLINE’ does not name a type
/usr/include/opencv/cxmisc.h:516: error: ‘CV_DEPTH_MAX’ was not declared in this scope
/usr/include/opencv/cxmisc.h:522: error: ‘CV_DEPTH_MAX’ was not declared in this scope
/usr/include/opencv/cxmisc.h:522: error: ‘CV_CN_MAX’ was not declared in this scope
In file included from m.cpp:5:
/usr/include/opencv/cxcore.hpp:70: error: template declaration of ‘cv::CV_EXPORTS cv::Size_’
/usr/include/opencv/cxcore.hpp:71: error: template declaration of ‘cv::CV_EXPORTS cv::Point_’
/usr/include/opencv/cxcore.hpp:72: error: template declaration of ‘cv::CV_EXPORTS cv::Rect_’
/usr/include/opencv/cxcore.hpp:77: error: expected initializer before ‘fromUtf16’
/usr/include/opencv/cxcore.hpp:78: error: expected initializer before ‘toUtf16’
/usr/include/opencv/cxcore.hpp:80: error: expected initializer before ‘format’
/usr/include/opencv/cxcore.hpp:82: error: expected initializer before ‘:’ token
m.cpp:38: error: expected ‘}’ at end of input
this is my copencv lib content:
alberto#zefiro:~$ ls /usr/include/opencv/
cvaux.h cvcompat.h cv.hpp cvtypes.h cvvidsurv.hpp cxcore.h cxerror.h cxmat.hpp cxoperations.hpp highgui.h ml.h
cvaux.hpp cv.h cvinternal.h cvver.h cvwimage.h cxcore.hpp cxflann.h cxmisc.h cxtypes.h highgui.hpp
i'm on ubuntu 10.10
You need to properly include the headers -I (capital i) and libraries -l (lowercase L).
On the newest OpenCV versions you should do:
#include <cv.h>
#include <highgui.h>
And then try to compile it with:
g++ m.cpp -o app `pkg-config --cflags --libs opencv`
Note: if you execute only pkg-config --cflags --libs opencv in the command line you will see the paths and libraries you need to include in the g++ command line.
if your development environment does not have pkg-config and because of this the accepted answer by karlphilip is not practical, or, you need to know the minimal set of libraries required to link your application, then assuming code such as
#include <cv.h>
#include <highgui.h>
int main()
{
return 0;
}
you can add library arguments from the following list sequentially from the top until you find the minimal set of arguments that you need:
-lopencv_core
-lopencv_imgproc
-lopencv_highgui
-lopencv_ml
-lopencv_video
-lopencv_features2d
-lopencv_calib3d
-lopencv_objdetect
-lopencv_contrib
-lopencv_legacy
-lopencv_flann
For example, the C source code listed at the top of this post compiles and links cleanly with only
gcc hello.c -o hello \
-I /usr/include/opencv \
-L /usr/lib \
-lopencv_core \
-lopencv_imgproc
on my old x86_64 Ubuntu 12.04 box.
Assuming C++ code such as
#include "core/core.hpp"
#include "highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
return 0;
}
then you would compile and link with
g++ hello.cpp -o hello \
-I /usr/include/opencv2 \
-L /usr/lib \
-lopencv_core \
-lopencv_imgproc
I suggest you use CMake to compile OpenCV with G++, this way is more suitable, I think.
cmake_minimum_required(VERSION 3.1)
project(YOUR_PROJECT_NAME)
set(CMAKE_GXX_FLAGS "-Wall -Wextra -Wconversion -pedantic -std=gnu11")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(YOUR_EXCUTABLE YOUR_CODE_SOURCE_FILES)
target_link_libraries(YOUR_EXCUTABLE ${OpenCV_LIBS})
Download source files in OpenCV folder and
install-opencv.sh script.
By running script file you
automatically install needed files for opencv. Run the following
code:
chmod +x install-opencv.sh
./install-opencv.sh
In case if you install different version of the library please update the first line of version inside the installation script.
For more information use this tutorial. Compile it with the next line:
g++ `pkg-config --cflags opencv` example.cpp `pkg-config --libs opencv`
I think that the accepted answer is a bit old.
At least for OpenCV 4.X, the most recently release today, you can use:
#include <opencv2/opencv.hpp>
and compile your program by something like:
g++ your_program_file.cpp -o your_program `pkg-config --cflags --libs opencv4`
Note opencv4 and not only opencv.
For example, using the command above to compile the following your_program_file.cpp file:
#include <opencv2/opencv.hpp>
int main() {
std::cout << "The current OpenCV version is " << CV_VERSION << "\n";
return 0;
}
And running:
./your_program
Outputs:
The current OpenCV version is 4.4.0
Which indeed matches with my current OpenCV settings.