Monday, September 17, 2018

How To Install OpenCV In Ubuntu Linux

How To Install OpenCV In Ubuntu Linux



Today, I'm going to show you how to install OpenCV in Ubuntu Linux (I'm using Ubuntu 18.04 for this tutorial)

First Download OpenCV and opencv_contrib

git clone https://github.com/opencv/opencv.git --depth=1

and

git clone https://github.com/opencv/opencv_contrib.git --depth=1

After completing the download extract the zip files and navigate to opencv directory

Then, create the directory named build inside the opencv directory and navigate to that newly created one..

cd opencv && mkdir build && cd build

Now run the cmake command from build directory...

cmake ../

It will take some time.. Please be patient... :)
At this time the output will look like this...

root@10a4bdccb67a:~/opencv/build# cmake ../ -- The CXX compiler identification is GNU 7.3.0 -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "2.7") -- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "2.6") -- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "3.4") -- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "3.2") -- Looking for ccache - not found -- Performing Test HAVE_CXX_FSIGNED_CHAR -- Performing Test HAVE_CXX_FSIGNED_CHAR - Success -- Performing Test HAVE_C_FSIGNED_CHAR -- Performing Test HAVE_C_FSIGNED_CHAR - Success -- Performing Test HAVE_CXX_W -- Performing Test HAVE_CXX_W - Success -- Performing Test HAVE_C_W -- Performing Test HAVE_C_W - Success -- Performing Test HAVE_CXX_WALL -- Performing Test HAVE_CXX_WALL - Success -- Performing Test HAVE_C_WALL -- Performing Test HAVE_C_WALL - Success ......

Then run the make command..

make

The above commands output will looks like..

[ 0%] Generate opencv.pc [ 0%] Built target gen-pkgconfig Scanning dependencies of target zlib [ 0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/adler32.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/compress.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/crc32.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/deflate.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzclose.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzlib.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzread.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/gzwrite.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/inflate.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/infback.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/inftrees.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/inffast.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/trees.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/uncompr.c.o [ 1%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/zutil.c.o [ 2%] Linking C static library ../lib/libzlib.a [ 2%] Built target zlib Scanning dependencies of target libjpeg-turbo [ 2%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapimin.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapistd.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jccoefct.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jccolor.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcdctmgr.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jchuff.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcinit.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmainct.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmarker.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmaster.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcomapi.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcparam.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcphuff.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcprepct.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcsample.c.o [ 3%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jctrans.c.o [ 4%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jdapimin.c.o [ 4%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jdapistd.c.o ..... [100%] Linking CXX executable ../../bin/opencv_annotation [100%] Built target opencv_annotation Scanning dependencies of target opencv_visualisation [100%] Building CXX object apps/visualisation/CMakeFiles/opencv_visualisation.dir/opencv_visualisation.cpp.o [100%] Linking CXX executable ../../bin/opencv_visualisation [100%] Built target opencv_visualisation Scanning dependencies of target opencv_interactive-calibration [100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibController.cpp.o [100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibPipeline.cpp.o [100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/frameProcessor.cpp.o [100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/main.cpp.o [100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/parametersController.cpp.o [100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/rotationConverters.cpp.o [100%] Linking CXX executable ../../bin/opencv_interactive-calibration [100%] Built target opencv_interactive-calibration Scanning dependencies of target opencv_version [100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o [100%] Linking CXX executable ../../bin/opencv_version [100%] Built target opencv_version

After this.. run make install with sudo..

sudo make install

The output will look like below one..

root@10a4bdccb67a:~/opencv/build# make install [ 0%] Built target gen-pkgconfig [ 2%] Built target zlib [ 6%] Built target libjpeg-turbo [ 9%] Built target libtiff [ 17%] Built target libwebp [ 19%] Built target libjasper [ 20%] Built target libpng [ 25%] Built target IlmImf [ 27%] Built target ippiw [ 32%] Built target libprotobuf [ 32%] Built target ittnotify [ 32%] Built target opencv_test_core_pch_dephelp [ 32%] Built target opencv_core_pch_dephelp [ 32%] Built target opencv_ts_pch_dephelp [ 32%] Built target pch_Generate_opencv_ts [ 32%] Built target pch_Generate_opencv_core [ 38%] Built target opencv_core [ 39%] Built target opencv_imgproc_pch_dephelp [ 39%] Built target pch_Generate_opencv_imgproc [ 43%] Built target opencv_imgproc [ 43%] Built target opencv_imgcodecs_pch_dephelp [ 43%] Built target pch_Generate_opencv_imgcodecs [ 45%] Built target opencv_imgcodecs [ 45%] Built target opencv_videoio_pch_dephelp [ 45%] Built target pch_Generate_opencv_videoio [ 46%] Built target opencv_videoio [ 46%] Built target opencv_highgui_pch_dephelp [ 46%] Built target pch_Generate_opencv_highgui [ 46%] Built target opencv_highgui [ 47%] Built target opencv_ts [ 47%] Built target pch_Generate_opencv_test_core [ 49%] Built target opencv_test_core ...... -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_licence_plate_rus_16stages.xml -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_lowerbody.xml -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_profileface.xml -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_righteye_2splits.xml -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_russian_plate_number.xml -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_smile.xml -- Installing: /usr/local/share/OpenCV/haarcascades/haarcascade_upperbody.xml -- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalcatface.xml -- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml -- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface_improved.xml -- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_profileface.xml -- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_silverware.xml -- Installing: /usr/local/bin/opencv_traincascade -- Installing: /usr/local/bin/opencv_createsamples -- Installing: /usr/local/bin/opencv_annotation -- Installing: /usr/local/bin/opencv_visualisation -- Installing: /usr/local/bin/opencv_interactive-calibration -- Installing: /usr/local/bin/opencv_version root@10a4bdccb67a:~/opencv/build#

One more step.. just link and cache the required files with following commands

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig

Now you can check the installation by running below command.. it will print out the version of opencv in stdout

pkg-config --modversion opencv

Here's the small shell script to automate the process... :)

That's it for now.. If you like.. don't forget to share it guys.. You can follow us on fb.com/opensourceinside and also subscribe our channel on Youtube..