Install opencv4.7 on ubuntu20.04

1. Preparation

 step1: install cmake

Execute the following command to install the latest cmake

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake

step2: Install opencv dependencies

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

Step3: Download opencv:

https://opencv.org/releases/

 step4: Unzip and create a new [build] folder

1. Unzip the downloaded file [opencv-4.7.0.zip] to the directory to be installed, and you will get the [opencv-4.7.0] folder after decompression.

2. Open the [opencv-4.7.0] folder and create a new build folder.

(I installed it in the /home/user_name/app directory, where user_name is my username)

2. Install opencv

step1: compile opencv

Open the newly created build folder, and execute the following command under the folder path:

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local .. 
sudo make -j8
sudo make install

explain:

Line 1: The [space+..] at the end of the first line must not be omitted. Its meaning is to tell the compiler that the file to be compiled is the CMakeList.txt file from the previous folder.

Line 2: where 8 means compiling with 8 threads at the same time, it can also be changed to 4 or only use a single thread, directly use [sudo make]

Line 3: Install opencv.

step2: Configure the parameters of opencv in ubuntu

1. Open the ld.so.conf file (the file may be blank, don’t worry about it), and add a line at the end of the file【/usr/local/lib】

sudo gedit /etc/ld.so.conf

2. Load the configuration to make the content just added take effect.

sudo ldconfig

3. Open the bash.bashrc file and add the following two lines at the end of the file:

sudo gedit /etc/bash.bashrc 

Added content:

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

The bash.bashrc file after adding content: 

 4. Run bash.bashrc:

 source /etc/bash.bashrc

5. Update the configuration environment of the system

sudo updatedb

6. Create and configure the opencv.pc file

Execute the following command to create and open the opencv.pc file:

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

With the file open, add the following content and save to close the file:

Note: I installed opencv4.7.0 here. If you install another version, the Version attribute needs to be modified to the corresponding version number.

prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib
 
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.7.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}

7. Check the installation of opencv:

pkg-config opencv --modversion

If the installation is successful, the version number will be returned:

3. Try to compile the opencv sample program with cmake

step1: Create a new folder and create the following two files:

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
# create proj
project(testopencv)
# c++ 11
set(CMAKE_CXX_FLAGS "-std=c++11")

find_package(OpenCV 4.7.0 REQUIRED) 	# find opencv4.7 installed path
include_directories(OpenCV_INCLUDE_DIRS)	# load opencv header files
add_executable(testopencv main.cpp)	# create exe file
target_link_libraries(testopencv ${OpenCV_LIBS})	# link llib files to exe

main.cpp

# include <iostream>
# include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
	Mat img=imread("dog.jpg");
	imshow("test", img);
	waitKey(0);
	return 0;
}

Step2: Put a random picture in the folder and name it [dog.jpg]

step3: Execute the following command in the folder directory :

cmake .
make

After compiling, a bunch of files will be generated in the same path, including an executable file named [testopencv].

step4: Execute the following command to run the executable file, and the dog.jpg file just now will be opened.

 ./testopencv

Note: The path [./] cannot be omitted, otherwise Linux will think it is a system command.

At this point, the entire opencv installation and testing process is completed.

————————————————————————————

If you want to also install the extension module opencv-contrib, just:

Step1: Go to github to download the opencv-contrib library:

GitHub - opencv/opencv_contrib at 3.3.1

Step2: Unzip the library to the same directory as the build.

Step3: Add the path of contrib when compiling the opencv code:

Just change the first command line of step1 in the second section to the following, and the remaining steps are the same as installing opencv. (In fact, it is just adding the path of contrib.)

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..

step4: Repeat the remaining steps of opencv.

Guess you like

Origin blog.csdn.net/wxyczhyza/article/details/128968849