Keywords: OpenCV, Ubuntu
OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects etc. To know more about OpenCV visit OpenCV.org
Please follow steps mentioned in this post to install OpenCV in Ubuntu 18.04 (LTS). All the terminal commands are summarized in the end of the post and you can access the same by clicking here.
Step 1. Update the Ubuntu System Package
Open the terminal and Run command to update Ubuntu system package index.
1 2 3 |
$ sudo apt-get update && sudo apt-get upgrade $ sudo apt install software-properties-common $ sudo apt install apt-file |
Run commands one by one.
Step 2. Install Required tools and packages
1 |
$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev |
1 |
$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev |
If you encounter error like below screenshot, then follow the step 2.a
1 |
$ 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< |
if you encounter error like below screenshot, then follow the step 2.b
Step 2.a Add repository and ppa
1 |
$ sudo apt-add-repository ppa:deadsnakes/ppa |
Step 2.b Add repository and ppa
1 2 3 |
$ sudo apt-add-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" $ sudo apt update $ sudo apt install libjasper1 libjasper-dev |
Step 3. Download OpenCV Sources using git
We need to clone the OpenCV sources using “git” to build and install it. We will download the source in /opt/ directory. Downloading, building and installation process requires root permission. Execute the commands to proceed further.
1 2 3 4 |
$ sudo -s $ cd /opt $ git clone https://github.com/Itseez/opencv.git $ git clone https://github.com/Itseez/opencv_contrib.git |
Step 4. Build & Install OpenCV
Execute the commands one by one:
1 2 3 |
$ cd opencv $ mkdir release $ cd release |
1 |
$ 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 OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/ |
1 |
$ make -j4 |
1 |
$ make install |
1 2 3 |
$ ldconfig $ exit $ cd ~ |
Step 5. Check OpenCV version installed
Run the command in terminal
1 |
$ pkg-config --modversion opencv |
If you encounter error like “pakage opencv not found” then follow step 5.a.
Step 5.a. Find & Set “opencv.pc” file path
Run the command in terminal
1 2 3 4 |
$ apt-file search opencv.pc $ ls /usr/local/lib/pkgconfig/ $ sudo cp /usr/local/lib/pkgconfig/opencv4.pc /usr/lib/x86_64-linux-gnu/pkgconfig/opencv.pc $ pkg-config --modversion opencv |
Step 6. Compile & Run a Test Program
Create a directory and put 1 sample image in it. Create a .cpp file and copy and paste the code posted here.
Change the image name & extension (.jpeg/.png etc). Compile and Run the program.
(A) Make a directory and .cpp file
1 2 3 |
$ mkdir opencv_test $ cd opencv_test $ gedit test.cpp |
(B)Copy the below code and paste in the file just created
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <opencv2/highgui.hpp> #include <iostream> int main( int argc, char** argv ) { cv::Mat image; image = cv::imread("opencv_testimage.png" ,cv::IMREAD_COLOR); if(! image.data ) { std::cout << "Image not found or unable to open" << std::endl ; return -1; } cv::namedWindow( "Techawarey:OpenCV Test Program", cv::WINDOW_AUTOSIZE ); cv::imshow( "Techawarey:OpenCV Test Program", image ); cv::waitKey(0); return 0; } |
(C) Compile the Code
1 |
$ g++ test.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv` |
(D) Execute the Code
1 |
$ ./testoutput |
Command Summary
This is the summary of all commands needed to install OpenCV in Ubuntu. If you encounter any problem during installation please check the respective Step. You might get the resolution in the post itself as I have also covered the problem & resolution of the errors I faced during the installation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//Step 1. Update the Ubuntu System Package $ sudo apt-get update && sudo apt-get upgrade //Step 2. Install Required tools and packages $ sudo apt install software-properties-common $ sudo apt install apt-file $ 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. Download OpenCV Sources using git $ sudo -s $ cd /opt $ git clone https://github.com/Itseez/opencv.git $ git clone https://github.com/Itseez/opencv_contrib.git //Step 4. Build & Install OpenCV $ cd opencv $ mkdir release $ cd 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 OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/ $ make -j4 $ make install $ ldconfig $ exit $ cd ~ //Step 5. Check OpenCV version installed $ pkg-config --modversion opencv //Step 6. Compile & Run a Test Program $ mkdir opencv_test $ cd opencv_test $ gedit test.cpp //Copy & Paste the code psoted in Step 6 $ g++ test.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv` $ ./testoutput |
Please like and share Techawarey. Find and Like Techawarey on Facebook.😊
Note: Logo of OpenCV & Ubuntu used are the property of OpenCV & Ubuntu respectively.
[Update]: A lot of thanks to Ryton1729 for his commnet & suggestions to improve this post.
Thank you very much.
[…] code and cmake options for OpenCV compilation taken from here: http://techawarey.com/programming/install-opencv-c-c-in-ubuntu-18-04-lts-step-by-step-guide/ Testing code includes only "highgui" […]
[…] 本文主要参考http://techawarey.com/programming/install-opencv-c-c-in-ubuntu-18-04-lts-step-by-step-guide/ […]
Had to do step 2a and 2b. Was able to do step 2a but getting error at step 2b. Error message is:
E: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/main/binary-arm64/Packages 404 Not Found [IP: 91.189.91.39 80]
E: Some index files failed to download. They have been ignored, or old ones used instead.
Why do I get this message ?
terminate called after throwing an instance of ‘cv::Exception’ what(): OpenCV(4.5.3-dev) /opt/opencv/modules/core/src/array.cpp:2494: error: (-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type in function ‘cvGetMat’ Aborted
I have two cameras, I tried cap(0), cap(1), cap(-1)… When I list my devices it returns /dev/video2 /dev/video3
So i tried to cap(“/dev/video2”) and tried with video3 too But i get the same error. I
‘m compiling it with g++ testeVideo.cpp -o testoutput -std=c++11
pkg-config --cflags --libs opencv
Thanks
[…] followed the documentation http://techawarey.com/programming/install-opencv-c-c-in-ubuntu-18-04-lts-step-by-step-guide/ to install OpenCV and it was successful. then I followed the documentation […]
Thank you, it works!
root@websdk-backend-79f4d879f6-bz4cj:~/opencv_test# 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
root@websdk-backend-79f4d879f6-bz4cj:~/opencv_test#
Follow the step 5.a
Thank you for the tutorial! I tried step 5.a after getting the same error but it still does not fix the issue. could youo let me know if there is any other way to fix this?
your tutorial is the best to install opencv. thanks so much
How do you use in code:blocks? I’ve installed it and it works from console but when I try to run from code:blocks it gives undefined reference to cv. Sorry if this is basics.
Hi Daniel,
It look likes linker issue. I personally do not use code:block. So can not comment much.
Please check in code:block settings that it’s have correct path of opencv.
Great ! It worked nicely for me <3
Thanks, its working 🙂
You’re Welcome!!
Superb Walktrough!
Two-three side steps/error messages you might consider adding/discussing; which i ran into:
+ add in at/before step 2.b: (required if apt-add-repository is missing)
”’ $ sudo apt install software-properties-common ”’
+ add in 5.a.0: (required for + apt-file)
sudo apt install apt-file
(and make a step for 5a “Create a directory and put 1 sample image in i”, as else it trows an error (obviously)
But overall, a great and concise overview!
Kind regards.
@Ryton1729 .. Thanks a lot for your suggestions.. I have edited the post to include instruction shared by you.