Compiling and installing OpenCV4 for Ubuntu 16 desktop

Posted by BobcatM on Mon, 17 Jan 2022 14:28:46 +0100

Welcome to my GitHub

https://github.com/zq2599/blog_demos

Content: classification and summary of all original articles and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc;

Overview of this article

  • This is a note recording the whole process of compiling, installing and using OpenCV4 on the pure Ubuntu 16 desktop computer. In general, it is divided into the following parts:
  • Install the necessary software, such as cmake
  • Download opencv source code, including opencv and opencv_contrib, and decompress and position it
  • Run cmake GUI to configure compilation items on the graphical page
  • Compilation and installation
  • Configuration environment
  • verification

environment

  • The environmental information is as follows:
  • Operating system: Ubuntu 16 04 desktop version
  • OpenCV: 4.1.1
  • Note: < font color = "red" > this article uses a non root account throughout the operation < / font >
  • Cut the crap and start directly in the newly installed Ubuntu 16 desktop version

Source change

  • In order to quickly install dependent software, first change the source to domestic. I use Alibaba cloud here
  • Backup source configuration first:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bk

-Modify / etc / apt / sources List is the following:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
  • If alicloud's source update is too slow, you can try this:
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
  • to update:
sudo apt-get update

Install application

  • Execute the following command to install all applications. If individual prompts fail, you can try several more times:
sudo apt-get install -y unzip build-essential curl cmake cmake-gui git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Download source code

  • Execute the following command to download all the source code, decompress it, and place it in an appropriate location:
curl -fL -o opencv-4.1.1.zip https://codeload.github.com/opencv/opencv/zip/4.1.1; \
unzip opencv-4.1.1.zip; \
rm -rf opencv-4.1.1.zip; \
curl -fL -o opencv_contrib-4.1.1.zip https://codeload.github.com/opencv/opencv_contrib/zip/refs/tags/4.1.1; \
unzip opencv_contrib-4.1.1.zip; \
rm -rf opencv_contrib-4.1.1.zip; \
mv opencv_contrib-4.1.1 opencv_contrib; \
mv opencv_contrib opencv-4.1.1/; \
mkdir opencv-4.1.1/build

Configuring with cmake GUI

  • Execute < font color = "blue" > cmake GUI in opencv-4.1.1 directory</ Font > start cmake GUI page to start graphical configuration
  • Here, the absolute path of opencv-4.1.1 folder is < font color = "blue" > / home / will / opencv-4.1.1 < / font >, so in the following figure, red box 1 is the absolute path of the source code, and red box 2 is the build subdirectory in the source code folder. After configuration, click red box 3 to start initialization configuration:

  • Click the button in red box 3 in the figure above, select < font color = "blue" > UNIX makefiles < / font > in the pop-up page, and then start configuration:
  • At this time, configuration items that can be edited appear. Next, start configuration:

  • First, select < font color = "blue" > build_ opencv_ world</font>:

  • Second, set < font color = "blue" > cmake_ BUILD_ Type < / font > is set to < font color = "red" > release < / font >

  • Third, < font color = "blue" > opencv_ EXTRA_ MODULES_ Path < / font > is a file path. Select < font color = "red" > / home / will / opencv-4.1.1/opencv here_ contrib/modules</font>

  • Fourth, select < font color = "blue" > OpenCV_ GENERATE_ PKGCONFIG</font>

  • Click the < font color = "blue" > configure < / font > button in the red box below again to start configuration:

  • After the configuration is completed, click the Generate button in the red box below to start generating configuration items:

  • Wait until the prompt in the red box in the figure below shows that the configuration is completed and the configuration item has been generated:

  • So far, all configurations have been completed. Please close cmake GUI and start compiling

compile

  • Enter the directory < font color = "blue" > opencv-4.1.1/build < / font > and execute the following command to start compilation:
make -j8
  • When I saw the CPU, I went up:

  • Execute < font color = "blue" > sudo make install < / font > to install into the current system (Please add sudo)
  • At this point, the installation is complete and the system configuration begins

system configuration

  • Execute the following command to edit the file (if not, create it):
sudo vi /etc/ld.so.conf.d/opencv.conf
  • In the open OpenCV Add the following content at the end of conf file:
/usr/local/lib
  • Execute configuration:
sudo ldconfig
  • Execute the following command to edit the file (if not, create it):
sudo vi /etc/bash.bashrc
  • In the open bash Add the following content at the end of bashrc file:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH
  • Now the configuration is completed, exit the console, open another one, execute the command < font color = "blue" > PKG config -- modversion opencv4 < / font >, note that < font color = "red" > opencv4 < / font >, and you can see the version number of OpenCV:
will@hp:~$ pkg-config --modversion opencv4
4.1.1

verification

  • Next, write a helloworld project to verify that opencv is available
  • I use CLion to create C + + projects:

  • CMakeLists.txt is as follows, which depends on OpenCV resources:
cmake_minimum_required(VERSION 3.20)
project(helloworld)

set(CMAKE_CXX_STANDARD 14)

find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(helloworld main.cpp)
target_link_libraries(helloworld ${OpenCV_LIBS})
  • main.cpp is as follows. Its function is to read the local picture and create a window to display the picture:
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    Mat mat = imread("/home/will/temp/202110/30/111.png");

    if(!mat.data) {
        cout<<"Image not exists!";
        return -1;
    }

    namedWindow("src", WINDOW_AUTOSIZE);
    imshow("[src]", mat);

    waitKey(0);
    return 0;
}
  • Compile and run, as shown in the following figure. The local picture is displayed successfully:

  • So far, the actual combat of compiling, installing, setting and verifying OpenCV4 in Ubuntu 16 desktop version has been completed;

You're not alone. Xinchen's original accompanies you all the way

  1. Java series
  2. Spring collection
  3. Docker series
  4. kubernetes series
  5. Database + middleware series
  6. DevOps series

Welcome to the official account: programmer Xin Chen

Wechat search "programmer Xinchen", I'm Xinchen, looking forward to traveling with you in the Java World
https://github.com/zq2599/blog_demos

Topics: cloud computing