Install OpenCV + opencv_contrib in Linux (Ubuntu 16.04)

Source: Internet
Author: User

Nearly two months of contact with the Linux system, in the boss's suggestion to turn over the Ubuntu brand, I installed the version is 16.04, after the habit feel good, than Windows stronger. Okay, nonsense, let's start by saying that installing Opemcv+opencv_contrib in Ubuntu.

First, prepare the development environment:

    • Ubuntu 16.04 64-bit
    • CMake
    • Ant
    • Jdk
    • Git
    • Python

Next, down the OpenCV + Opencv_contrib library from github, you can download it from the HTTPS://GITHUB.COM/OPENCV (note the version corresponds), or you can do it via the instructions (recommended):

git clone https://github.com/Itseez/opencv.gitgit clone https://github.com/itseez/opencv_contrib.git

This process needs to wait for a period of time, depending on your network speed, after the successful download, you can see in your home directory OpenCV and opencv_contrib two folders, the Opencv_contrib directory to move to the OpenCV directory.

In the process of downloading, we can add the dependent libraries required by OPENCV:

sudo apt-get install build-essentialsudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libav Format-dev libswscale-devsudo apt-get install python-dev python-numpy libtbb2 Libtbb-dev Libjpeg-dev Libpng-dev Libtiff-dev Libjasper-dev Libdc1394-22-dev

When the above steps are complete, we enter the OpenCV directory and create the build directory:

mkdir Build

Like this:

    

Go to the build directory, start compiling, execute (note that the next two points must not be less):

cmake-d cmake_install_prefix=/usr/local-d cmake_build_type=release-d opencv_extra_modules_path=. /opencv_contrib/modules.

The result of the compilation is:

    

Next, execute:

Make-j8

The results are as follows:

    

At this point, if you just need to use OPENCV in Python or C, you can do the next step directly, and if you need to use OPENCV in Java, you need to modify some of the source files. Edit features2d_manual.hpp file, directory file in/modules/features2d/misc/java/src/cpp中。

Specific changes:

  • In line 8th, add # include "Opencv2/xfeatures2d.hpp" in the # include "FEATURES2D_CONVERTERS.HPP"
  • on line 121th,
    //case SIFT: //name = name +  "
          sift   "  break  ;  case SURF: //name = name +  " surf  "  ;  break ; 

      Change to

    //case SIFT: //Fd=xfeatures2d::sift:: Create ();  break  ;  case SURF: //Fd=xfeatures2d::surf::create ();  break ; 
  • on line No. 248,
    //case SIFT: //name = name +  "
          sift   "  break  ;  case SURF: //name = name +  " surf  "  ;  break ; 

      Change to

     case sift:de  = Xfeatures2d::sift::create ();  break  ;case surf:de  = Xfeatures2d::surf::create ();  break ; 
  • on line No. 257,
    //case BRIEF: //name = name +  "
          brief   "  break ; 

      Change to

     case brief:de  = xfeatures2d::    Briefdescriptorextractor::create ();  break ; 
  • In line No. 263, the
    Case FREAK://    "FREAK"; Break    ;

    Switch

    Case FREAK:     = xfeatures2d::freak::create ();     break;

The modification is complete.

Next, execute:

sudo make install

The result of the compilation is:

    

In this way, we can use OpenCV in Ubuntu for a variety of languages.

We can see the results of the OPENCV compilation under/usr/local/lib:

    

  

Finally, let's briefly talk about the use of each language:

    • Python: Direct import of CV2 library is OK
      Import Cv2

      In this compilation should pay attention to the difference between Python2 and Python3, Python2 compiled OpenCV in Python3 is not available

    • C + +: I am using QT to add in Pro file
      Includepath + =         /usr/local/include         /usr/local/include/opencv         /usr/local/include/opencv2 LIBS + =     / home/fanxuan/opencv/build/lib/libopencv_java331.so \
    • Java: I said I was using idea, set the Opencv/build/lib directory in the idea to set the run space of the JVM.

Finally, the old way, the code is not, you can look at the example of OpenCV, or go to www.baidu.com or github.com.

Thank the Foreign Friend https://elbauldelprogramador.com/en/how-to-compile-opencv3-nonfree-part-from-source/blog in my exploration process to bring me the help.

Install OpenCV + opencv_contrib in Linux (Ubuntu 16.04)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.