1 Super detailed ubuntu installation opencv

1 First make sure that cmake and make have been installed on Ubuntu

1.1 install make

sudo apt-get install  make

1.2 install cmake

To install cmake, please refer to my blog https://blog.csdn.net/weixin_44698673/article/details/125964197?spm=1001.2014.3001.5502

2 Installation dependent environment

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libcanberra-gtk-module
sudo apt-get install pkg-config

3 Download opencv source code

Download address: https://opencv.org/releases/
insert image description here
Click sources to download the source code, I downloaded the source code of 4.5.4.
Unzip the downloaded source code into ubuntu.

4 Compile the source code and install

4.1 Enter the opencv source code directory and create a new build folder

insert image description here

4.2 Enter the build folder, open the terminal and use cmake to generate makefile

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local .. 

Execute make after waiting for the compilation to complete

sudo make 

Start compiling the code, it will take about 1-2 hours

4.3 install opencv

After the code is compiled

sudo make install

5 Configure environment variables

Open the file /etc/ld.so.conf and add /usr/local/lib to the last line

Note: /etc/ld.so.conf This file records the path of the dynamic library used during compilation, that is, the path to load the so library.
By default, the compiler will only use the library files in the two directories /lib and /usr/lib. Usually, when installing through the source package, if you do not specify --prefix, the library will be installed in the /usr/local directory Next, but did not add the /usr/local/lib directory> in the file /etc/ld.so.conf. In this way, although the source package is installed, the relevant .so library cannot be found when using it, and an error will be reported. That is to say, the system does not know that the source package is installed.

There are two solutions to this situation:
1) When installing with source code, use –prefix to specify the installation path as /usr/lib. In this case, there is no need to configure PKG_CONFIG_PATH
2) Directly add the path /usr/local/lib path to the file /etc/ld.so.conf. Add directly at the end of the file /etc/ld.so.conf: /usr/local/lib

ldconfig
Let's take a look at the ldconfig program, which is located under /sbin. Its function is to cache the library files under the path listed in the file /etc/ld.so.conf to /etc/ld.so.cache for use, so After installing some library files, or modifying /etc/ld.so.conf to add a new library search path, you need to run > run ldconfig, so that all library files are cached in the file /etc/ld.so. In the cache, if you don't do it, you may not be able to find the library you just installed.

PKG_CONFIG_PATH:
Finally, let’s talk about the environment variable PKG_CONFIG_PATH, which appears after installing pkg-config
In fact, pkg-config is a program that provides system information to the configure program, such as the version of the software, the version of the library, and the path of the library
PKG_CONFIG_PATH Indicate the *.pc file (package configuration file, which saves the header file of the library and the path information of the library. For example, when compiling a project and using the glib-2.0 library, pkg-config will search for glib-2.0 according to the path line in PKG_CONFIG_PATH. pc, and then pass lib-2.0.pc as a parameter to GCC, then GCC can find the path of the library).
Such as: PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig

WC is so far away from the topic, return to the topic to configure environment variables

sudo gedit /etc/ld.so.conf

Add a line /usr/local/lib to the file, because opencv is installed in this path by default in the makefile.
insert image description here
Run ldconfig to make the added library path take effect

sudo ldconfig

modify bash.bashrc

sudo gedit /etc/bash.bashrc 

Add at the end of the file

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

Run the script to take effect of the newly set environment variables

 source /etc/bash.bashrc

Enter the following command on the command line to check whether opencv is installed successfully

pkg-config opencv --modversion

insert image description here
Indicates that opencv is installed successfully. (Here is a question. I clearly downloaded the source code of 4.5.4. Why is the version shown here as 3.2.0? Is there any veteran passing by who can answer it)

If there is no opencv.pc, then create it manually

cd /usr/local/lib/pkgconfig
touch opencv.pc
sudo gedit opencv.pc

document content

prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib
 
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.4.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}

6 Write a small program to test and verify

Create a test.cpp, the source code is as follows

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv )
{

  Mat image = imread("123.jpg");

  namedWindow("SSJ",WINDOW_FREERATIO);

  imshow("SSJ", image);

  waitKey(0);

  return 0;
}

compile

g++ test.cpp -o test.out $(pkg-config --cflags --libs opencv)

Note: pkg-config is the program already installed in the previous article, a package management tool that can be used to obtain all compilation-related information of a certain library/module, pkg-config --cflags --libs opencv will opencv package header file and library information to the compiler. No need to manually specify the header file and library path
g++ test.cpp -o test.out -I /usr/include/opencv2 -l /usr/local/lib/opencv

  • –libs, view library information.

  • –cflags, view header file information.

  • The information of pkg-config comes from (1) all .pc files
    under /usr/lib of the system . (2) All .pc files under the path pointed to by the PKG_CONFIG_PATH environment variable .

Run the program
./test.out
insert image description here
and the installation is successful! ! !
If the old iron gains something, give it a thumbs up

Guess you like

Origin blog.csdn.net/weixin_44698673/article/details/127082446