In this post we will see How to Install OpenCV in Ubuntu for C/C++
OpenCV is an image processing library created by Intel and later supported by Willow Garage and now maintained by Itseez. OpenCV means Intel® Open Source Computer Vision Library. It is a collection of C functions and a few C++ classes that implement some popular Image Processing and Computer Vision algorithms. OpenCV is Available on Mac, Windows, Linux (Terminal environment).
Step 1 – Updating Ubuntu
$ sudo apt-get update $ sudo apt-get upgrade
Step 2 – Install dependencies
$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev $ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev $ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev
Step 3 – Get OpenCV
$ sudo -s $ cd /opt /opt$ git clone https://github.com/Itseez/opencv.git /opt$ git clone https://github.com/Itseez/opencv_contrib.git
Step 4 – build and install OpenCV
/opt$ cd opencv /opt/opencv$ mkdir release /opt/opencv$ cd release /opt/opencv/release$ cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/ /opt/opencv/release$ make -j4 /opt/opencv/release$ make install /opt/opencv/release$ ldconfig /opt/opencv/release$ exit /opt/opencv/release$ cd ~
Now to check if OpenCV is installed on a machine, run the following commands
$ pkg-config --modversion opencv 3.2.x
We will get the opencv version installed
in this case it’s 3.2.x
Online Course – OpenCV Python Tutorial For Beginners. At the end of this course, you will have a firm grasp of Computer Vision techniques using OpenCV libraries. This course will be your gateway to the world of data science.
Create a C++ program
Follow the commands
$ mkdir cpp_test $ cd cpp_test $ touch main.cpp
The above command will create a folder called cpp_test
and create a main.cpp
file inside it
Now place any .jpeg
image inside the cpp_test
folder.
So Now your cpp_test
folder will contain two files as follows
.
├── sample.jpeg
└── main.cpp
Now open the main.cpp and add the following code
#include <opencv2/highgui.hpp> #include <iostream> int main( int argc, char** argv ) { cv::Mat image; image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR); if(! image.data ) { std::cout << "Could not open or find the image" << std::endl ; return -1; } cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE ); cv::imshow( "Display window", image ); cv::waitKey(0); return 0; }
Now compile your code with the following command
g++ main.cpp -o output `pkg-config --cflags --libs opencv`
Now run the C++ program with the following command
$ ./output
press ESC to exit
Video instruction – How to Install OpenCV in Ubuntu 16.04 LTS for C / C++
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found https://uploads.disquscdn.com/images/e079f0ae50f33017e24db8d8690d3d29ce58a8b2fb44994835f2df22ec0a1306.png
Hello! I am having the same error. Did you find any solution for this?
try this one it works with me
sudo apt-get install libopencv-dev
Thank you, this really helped!
Thanks. Can you also show how to use opencv in python using a simple example.
Does this build also work for use in python.
I followed the procedure above. For c++ it’s running fine but for python its throwing error.
What mistake I am doing. Please solve this.
https://uploads.disquscdn.com/images/10592378160c3560490f804150b29de092d1c4e3921549418d4d5803e7690724.png
try the python program in
http://www.codebind.com/python/install-opencv-ubuntu-16-04-lts-python/
Thanks for sharing~ It success on my computer.
In step 4, 5th command onwards, the current working directory is /opt/opencv/releases, and not /opt/opencv
Its missleading, please correct it.
thanks. that was really helpful
In order to use #include and #include
in opencv project, we need to install
libvtk5-dev and libeigen3-dev:
sudo apt-get isntall libvtk5-dev
sudo apt-get isntall libeigen3-dev
and turn on the build composantes in cmake. Also, the path /opt/opencv/ stil correct:
/opt/opencv/release$ cmake … -D WITH_EIGEN=ON -D WITH_V4L=ON … /opt/opencv/
My Installation was successful but now I have a big Problem, my Computer isn´t working anymore.
When I´m starting the login comes up on my Ubuntu 16.04, then I type the right Datas.
It shows me then only quick an Blackscreen with an error message(/dev/nvme0n1p2: clean, 359022/60407808 files, 9919453/241604864 blocks) and then it goes back to the login.
I think something with the screen resolution is wrong. Here I have another error messages:
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 155 (NV-GLX)
Minor opcode of failed request: 4 ()
Resource id in failed request: 0x1400003
Serial number of failed request: 40
Current serial number in output stream: 40
gnome-session-check-accelerated: Helper exited with code 256
gnome-session-binary[25119]: CRITICAL: We failed, but the fail whale is dead. Sorry….
CRITICAL: We failed, but the fail whale is dead. Sorry….
Hopefully can someone help me with my Problem.
how to resolve this issue while installing opencv https://uploads.disquscdn.com/images/07d95776086306f69fc3ee5ad1c2b651a5a287ac8c32228cb47a88f35d91d915.png
Thanks bro!
Hi. I followed all the steps mentioned above and it was completed successfully.
But I am facing this error now..
Anyone knows the solution for this?
https://uploads.disquscdn.com/images/a4cca9f1ff04bc913b3d0029b14458886c1fb71e94032d5c09e6ded46c1c89bb.png
Please help! 🙂
Plz help me,
when i run .test, i have a problem: https://uploads.disquscdn.com/images/42ec1fc2db845c81c8762d60fd1a991517efc34a03e07f5922f0226984423bda.png
Tks,
You have to declare you opencv install system variables.
hello
https://uploads.disquscdn.com/images/d44241ab84e000378f7d6450e711d83972c8589ce192f87e61cc9c034f63984d.png
Why make a mistake?????????????????????????????????
My Linux version: 18.04 LTS Ubuntu
I failed to install libjasper-dev and python3.5-dev too.
Attached Files CMakeOutput.log and CMakeError.log.
http://mihanfarm.com/c/CMakeOutput.log
http://mihanfarm.com/c/CMakeError.log
hey same mistake ,how did you solve it?
you have to manually install: https://ubuntu.pkgs.org/18.04/trinity-r14-builddeps-amd64/libjasper-dev_1.900.1-debian1-2.5ubuntu18.04.0+0_amd64.deb.html and it requires libjasper1.
for openvc Version 4.0.0 (v4.0) , you must use -std=c++14:
g++ main.cpp -o output `pkg-config –cflags –libs opencv` -std=c++14
and change ‘CV_LOAD_IMAGE_COLOR ‘ to cv::IMAGE_COLOR
when i’m trying “sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev”, the terminal says:
Lecture des listes de paquets… Fait
Construction de l’arbre des dépendances
Lecture des informations d’état… Fait
Aucune version du paquet pkg-config n’est disponible, mais il existe dans la base
de données. Cela signifie en général que le paquet est manquant, qu’il est devenu obsolète
ou qu’il n’est disponible que sur une autre source
E: Impossible de trouver le paquet libgtk2.0-dev
E: Impossible de trouver de paquet correspondant à l’expression rationnelle « libgtk2.0-dev »
E: Impossible de trouver de paquet correspondant à l’expression rationnelle « libgtk2.0-dev »
E: Le paquet « pkg-config » n’a pas de version susceptible d’être installée
E: Impossible de trouver le paquet libavcodec-dev
E: Impossible de trouver le paquet libavformat-dev
E: Impossible de trouver le paquet libswscale-dev
When I type the cmake command, it says : CMake Error: `The source directory “/opt/opencv” does not exist.
Specify –help for usage, or press the help button on the CMake GUI.`
I created the directory and I am typing the command from inside it, so it’s really strange!
You must type inside /release bro
If you copy-paste the compile instruction, it should work. The single quotes need to be from the key above the Tab key, NOT the single quote next to the Enter key.
hi, I’m getting this error in ubuntu xenial:
Any hints?
Install the development package as:
apt install libopencv-dev
Thanks That resolved issue!
but it shows installed version is a bit older.2.4.9.any way to upgrade that
Me too
Thanks, that works! but every time I compile, I get this message:
OpenCV: FFMPEG: tag 0x67706a6d/’mjpg’ is not supported with codec id 8 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x7634706d/’mp4v’
Hi everyone,
I’m getting this error when trying to compile above code.
$ pkg-config –modversion opencv
> 3.4.5
$ g++ main.cpp -o main `pkg-config –cflags –libs opencv`
> /tmp/cc4vbYSm.o: In function `main’:
main.cpp:(.text+0x64): undefined reference to `cv::imread(cv::String const&, int)’
main.cpp:(.text+0xfd): undefined reference to `cv::namedWindow(cv::String const&, int)’
main.cpp:(.text+0x154): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)’
/tmp/cc4vbYSm.o: In function `cv::String::String(char const*)’:
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x54): undefined reference to `cv::String::allocate(unsigned long)’
/tmp/cc4vbYSm.o: In function `cv::String::~String()’:
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()’
collect2: error: ld returned 1 exit status
Please help.
its https://uploads.disquscdn.com/images/5f476ecd1c8156d269db4eb020b8440375f87ac72a77fae5f196d5e10be91095.png
Thanks working as mentioned
https://github.com/Itseez/opencv.git in github changed to https://github.com/opencv/opencv.git. If I use https://github.com/opencv/opencv.git is it oke?
I have very slow get clone, and failed suddenly
I used make -j1. That was worked for me
g++ main.cpp -o output `pkg-config –cflags –libs opencv`
main.cpp:1:31: fatal error: opencv2/highgui.hpp: No such file or directory
compilation terminated.
if you are using opencv 4 try this g++ main.cpp -o output `pkg-config –cflags –libs opencv4` -std=c++11
hi, i have the same problem, i solved changing the first include by this
#include “highgui.h”
why? i check the includes directory and actually there is not any opencv2/highgui.hpp file, instead there is an highgui.h file. and i used that and works great
hello,I’m getting this error in debian gnu linux :
user@DESKTOP-M1UBACJ:~/cpp_test$ g++ -L/usr/local/lib main.cpp -o output `pkg-config –cflags –libs opencv`
/usr/bin/ld: warning: libopencv_core.so.3.2, needed by /usr/local/lib/libopencv_viz.so, may conflict with libopencv_core.so.4.1
/usr/bin/ld: /tmp/ccdH6K1Z.o: undefined reference to symbol ‘_ZN2cv6String10deallocateEv’
//usr/local/lib/libopencv_core.so.3.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
please help me.
I have followed all steps, and they seemed to be ok. But when I try to verify that OpenCV has been installed (with “pkg-config –modversion opencv”) this apears:
pkg-config –modversion opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found
Please, could you help me?
I could solve my problems making this chages:
Step 2
*2nd command: change “python3.5-dev” by “python3.6-dev” (or the version of python that points ubuntu when you use “python3 -V”)
*3th command: delete “libjasper-dev”
Step 4
*After all commands and before cheking if OpenCV is installed, use this command:
sudo apt-get install libopencv-dev
Despide de solution I have written, every time I compile using, for example:
$ g++ main.cpp -o main `pkg-config –cflags –libs opencv`
I get this message:
“OpenCV: FFMPEG: tag 0x67706a6d/’mjpg’ is not supported with codec id 8 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x7634706d/’mp4v'”
Do you know why that happens and what can I do to avoid it?
pkg-config –modversion opencv
the above code needs
‘libopencv-dev’ to be installed.
So try ‘sudo apt-get install libopencv-dev’ before checking the version.
i am facing Segmentation fault because of imshow(“Display window”,image) function.
I have tried looking at almost everything, but can’t find the issue. Any help would be appreciated.