Welcome to my GitHub
https://github.com/zq2599/blog_demos
Content: All original articles are categorized and summarized and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;
Overview of this article
- This is a note that records the entire process of compiling, installing, and using OpenCV4 on a pure Ubuntu16 desktop computer. Generally speaking, it is divided into the following parts:
- Install necessary software such as cmake
- Download the OpenCV source code, including opencv and opencv_contrib, and unzip it and place it in place
- Run cmake-gui to configure compilation items on the graphical page
- compile, install
- Configuration Environment
- verify
surroundings
- The environmental information is as follows:
- Operating system: Ubuntu16.04 desktop version
- OpenCV:4.1.1
- Note: <font color="red"> This article uses a non-root account to operate </font>
- Don't talk nonsense, start operating directly in the newly installed Ubuntu16 desktop version
change source
- In order to quickly install the dependent software, first change the source to domestic one, I am using Alibaba Cloud here
- Backup the source configuration first:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bk
- Modify /etc/apt/sources.list to 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 Alibaba Cloud'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
- renew:
sudo apt-get update
Install the app
- Execute the following commands to install all applications. If there are individual prompts that fail, you can try several 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 the source code
- Execute the following command to download all the source code, unzip it, and place it in a suitable 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
Configure with cmake-gui
- Execute <font color="blue">cmake-gui ..</font> in the opencv-4.1.1 directory to start the cmake-gui page and start the graphical configuration
- The absolute path of my opencv-4.1.1 folder here is <font color="blue">/home/will/opencv-4.1.1</font>, so the red box 1 in the figure below is the absolute path of the source code, and the red box 2 It is the build subdirectory in the source code folder. After the configuration is complete, click the red box 3 to start the initialization configuration:
- After clicking the button in the red box 3 above, select <font color="blue">Unix Makefiles</font> on the pop-up page, and then start the configuration:
- At this point, there are configuration items that can be used to edit, and then start the configuration:
- First, select <font color="blue">BUILD_opencv_world</font>:
- Second, set <font color="blue">CMAKE_BUILD_TYPE</font> to <font color="red">Release</font>
- Third, <font color="blue">OPENCV_EXTRA_MODULES_PATH</font> is a file path, here select <font color="red">/home/will/opencv-4.1.1/opencv_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 to start the configuration again:
- After the configuration is complete, 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 appears, indicating that the configuration is complete and the configuration item has been generated:
- So far, all configurations have been completed, please close cmake-gui, and then you can start compiling
compile
- Enter the directory <font color="blue">opencv-4.1.1/build</font> and execute the following command to start compiling:
make -j8
- Seeing the CPU goes up:
- Execute <font color="blue">sudo make install</font> to install into the current system (note to add sudo)
- At this point, the installation is complete, start the system configuration
System Configuration
- Execute the following command to edit the file (create it if it doesn't exist):
sudo vi /etc/ld.so.conf.d/opencv.conf
- Add the following at the end of the opened opencv.conf file:
/usr/local/lib
- Execute configuration:
sudo ldconfig
- Execute the following command to edit the file (create it if it doesn't exist):
sudo vi /etc/bash.bashrc
- Add the following to the end of the opened bash.bashrc file:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
- At this point, the configuration is complete, exit the console, reopen one, execute the command <font color="blue">pkg-config --modversion opencv4</font>, note that <font color="red">opencv4</font> , you can see the version number of opencv:
will@hp:~$ pkg-config --modversion opencv4
4.1.1
verify
- Next, write a helloworld project to verify that opencv is available
- I am using CLion here to create C++ projects:
- The contents of CMakeLists.txt are as follows, depending on the resources of OpenCV:
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, the function is to read the local image and create a window to display the image:
#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 below, the local image is displayed successfully:
- At this point, the actual combat of compiling, installing, setting, and verifying OpenCV4 in the Ubuntu16 desktop version has been completed;
You are not alone, Xinchen Original is with you all the way
- Java series
- Spring Collection
- Docker series
- kubernetes series
- database + middleware series
- DevOps Series
Welcome to the public number: Programmer Xin Chen
Search "Programmer Xinchen" on WeChat, I am Xinchen, I look forward to traveling the Java world with you...
https://github.com/zq2599/blog_demos
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。