Installing OpenCV on Ubuntu was painful for me in the past. There are lots of dependencies to be sorted out first and there’s no comprehensive installation tutorial shipped with the OpenCV distribution.
This script has done a great job assembling all necessary “apt-get-install’s” into an executable script. Some personal notes about this:
- It seems to me ” sudo apt-get install libopencv-dev” for dependencies is not necessary – this will be superseded by the subsequent installation of opencv from source. At the end of the scripting installation, the package seems to be in some cache place and be not in effect:

- It seems ” sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev” is also unnecessary. These libraries are to be provided by the next fresh installation of ffmpeg follows. Checking their location seems to also confirm my guess:

- About version of ffmpeg: ffmepg is under active development as always. The choice of its version is sometimes tricky, especially in running together with OpenCV. I remember the 1.x series was not compatible with OpenCV 2.3 in my previous trial (compilation errors were thrown due to some problem with ffmpeg libraries). The current 2.4.3 version seems to be at least fine with ffmpeg 0.11.2 in compilation.
[Update] From test by Mr. Alok Singh Mahor (comments below), installing Ubuntu package “libopencv-dev” would already get things work, though the linking order with g++ has to be taken care of as discussed (also comments below and here) . Of course, it’s not bad idea to install from source that always guarantees to bring in the newest feature of actively developed OpenCV. [Dec 28 2012]
I am using Ubuntu 12.10
if I install libopencv-dev then it automatically install libavcodec-dev libavformat-dev libswscale-dev
why did you write this as two step process?
my (pkg-config –cflags –libs opencv)’s output is
-I/usr/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
I am trying to compile the program using command (g++ `pkg-config –cflags –libs opencv` alok.cpp)
but I am getting linker errors
alok.cpp is
#include
#include
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( “No image data \n” );
return -1;
}
namedWindow( “Display Image”, CV_WINDOW_AUTOSIZE );
imshow( “Display Image”, image );
waitKey(0);
return 0;
}
error I am getting is
alok@alok:~/exp/opencv/exp$ g++ `pkg-config –cflags opencv` `pkg-config –libs opencv` alok.cpp
/tmp/cc8oVSIW.o: In function `main’:
alok.cpp:(.text+0×67): undefined reference to `cv::imread(std::string const&, int)’
alok.cpp:(.text+0xfa): undefined reference to `cv::namedWindow(std::string const&, int)’
alok.cpp:(.text+0×122): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)’
alok.cpp:(.text+0×159): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)’
alok.cpp:(.text+0x17b): undefined reference to `cv::waitKey(int)’
/tmp/cc8oVSIW.o: In function `cv::Mat::~Mat()’:
alok.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x2b): undefined reference to `cv::fastFree(void*)’
/tmp/cc8oVSIW.o: In function `cv::Mat::operator=(cv::Mat const&)’:
alok.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0xf2): undefined reference to `cv::Mat::copySize(cv::Mat const&)’
/tmp/cc8oVSIW.o: In function `cv::Mat::release()’:
alok.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x3b): undefined reference to `cv::Mat::deallocate()’
collect2: error: ld returned 1 exit status
could you please tell me where I am wrong?
thanks in advance
I have no idea why it is so — but in my experience the only way you get OpenCV program compiled in Ubuntu is to “Install from Source”, which is exactly what the pointed script is doing. Your errors will be resolved after doing that.
My doubt about the script is it seems the two lines
” sudo apt-get install libopencv-dev” and
” sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev”
are unnecessary since they will be overwritten by the subsequent operations. But anyway leave them as is won’t hurt.
Good luck!
problem solved by using command
g++ `pkg-config –cflags opencv` alok.cpp `pkg-config –libs opencv`
Glad to hear! It seems like some linking order issue … I happened to also encounter similar issue before with gcc here
Btw, I’m a bit confused by your installation process. Did you make it work by just doing the
” sudo apt-get install libopencv-dev”
stuff or executing the script? I’m a bit curious because I tried to install the ubuntu package in previous ubuntu distros but never got any test program running.
yes I installed only one package libopencv-dev
and all the required libraries got automatically installed as dependancy of libopencv-dev
so only one sudo apt-get install libopencv-dev” will work
but there is some linking problem with some version of Ubuntu so I had to use (g++ `pkg-config –cflags opencv` alok.cpp `pkg-config –libs opencv`)
instead of (g++ `pkg-config –cflags opencv` `pkg-config –libs opencv` alok.cpp )
Great to learn this! And I’d recall my previous blame on ubuntu opencv package; also this means opencv is ready-to-deploy on ubuntu platform without much pain.
Thanks for the test-drive!
you are welcome
its very clean and straight forward process to setup openCV envirnment on Ubuntu.
Im sorry but I’m just a day old in ubuntu. can you tell me what commands im to use to insatall OpenCV completely
According to Alok’s experience, this will do
sudo apt-get install libopencv-dev
Good luck!
Greetings from Colorado! I’m bored to death at work so I decided to browse your website on my iphone during lunch break. I really like the info you present here and can’t wait to take a look when
I get home. I’m surprised at how quick your blog loaded on my cell phone .. I’m
not even using WIFI, just 3G .. Anyways, awesome
site!
Click on
and click the following article
and Click Here
and also click the next internet site
Hi, I discovered your page on and although the information
seems great, I suspect your blog might be going through a
few internet browser compatibility problems. If I view it
in Firefox, it loads okay, but if I use Chrome, it comes
up appearing overlapped and off-kilter. Just wanted to inform you.