Installation tutorial for Opencv in Ubuntu

  • 2020-05-30 21:57:50
  • OfStack

preface

OpenCV is a cross-platform computer vision library distributed under the BSD license (open source) and runs on Linux, Windows, and Mac OS. It is lightweight and efficient, provides Python, Ruby, MATLAB and other language interfaces, and implements many common algorithms in image processing and computer vision.

Not only does OpenCV need to be used in software development, but it is also the operating dependency of many open source software, so it is necessary to install an Opencv, even if you don't want to learn to use it.

The installation

The following are mainly available methods found on baidu:

Installation and operation dependencies


$ sudo apt-get install libqt4-dev libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config\
python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev \
libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev\
libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev \
libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip

Download source code

Download the appropriate version of the official website, I am under the 3.1.0.

Download complete decompression is good.

compile

It's a little bit of a hassle to compile, but now it's all compiled with cmake and make, and the first time you use this, it's a little bit of a nod.

1. Create a new folder called build/ in the file directory. The main purpose of this folder is to store temporary files generated by compilation. Of course you can call it something else.

2. Enter the build/ folder and enter the following command


cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON  -D WITH_XINE=ON  -D WITH_OPENGL=ON  -D WITH_TBB=ON  -D BUILD_EXAMPLES=ON  -D BUILD_NEW_PYTHON_SUPPORT=ON  -D WITH_V4L=ON ..

This is the cmake command followed by some configuration parameters, and finally the CMakeLists.txt configuration file location, which of course is.. /

3. Compile, install and input make -j $(nproc) (this is the command for multi-process make, \$(nproc) Is the number of processes, which can also be specified directly), which can take a long time, at the end $ sudo make install To install the file.

4. Finally, to configure some paths, type the following command


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

Then ldconfig. (note permissions)

5. Finally, you can use the following command to judge whether the installation is successful


pkg-config --modversion opencv
pkg-config --cflags opencv

test

OpenCV can be compiled and run under codeblock as a linked library. But if you're at the command line, you can write cmake by hand.

To facilitate testing, we create a new test folder and write a test program under it.

First create the following file and save it as test.cpp


#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;
int main(int argc ,char** argv){
 if(argc!=2){
 printf("No image data\n");
 return -1;
 }
 char *imageName=argv[1];
 Mat image;
 image=imread(imageName,1);
 if(!image.data){
 printf("No iamge data\n");
 return -1;
 }
 namedWindow(imageName,CV_WINDOW_AUTOSIZE);
 imshow(imageName,image);
 waitKey(0);
 return 0;
}

Then I copied a random test image into the test folder. I used his own classic lena.jpg.

Next, write the cmake configuration file and save the following file as CMakeLists.txt


project(test)
add_executable(test test.cpp)
find_package(OpenCV REQUIRED)
target_link_libraries(test ${OpenCV_LIBS})

The meaning of the various documents used inside is also very clear, then follow the line.

Finally, create a new build folder under test and enter it cmake ../ You can compile cmake and then type make to generate the executable.

Find the test file, and type it on the command line ./test ../lena.jpg Can run the program.

conclusion


Related articles: