ubuntu20.04安装OpenCV4.3.0
ubuntu20.04安装OpenCV4.3.0
一、安装步骤
1.下载源文件
可以在官网下载相应版本的OpenCV,也可以上GitHub下载。
主要下载两个文件OpenCV和OpenCV_contrib。
OpenCV_contrib4.3.0下载地址 github
2.安装依赖
2.1 先更新系统
sudo apt-get update
sudo apt-get upgrade
2.2 安装编译环境和依赖库
sudo apt-get install build-essential #如果已经安装过build-essential,则该步骤省略
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
2.3 支持Python
# python3支持
sudo apt install python3-dev python3-numpy
# streamer支持
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
# 其他依赖
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libwebp-dev
2.4 可能遇到的问题
其中可能遇见无法安装libjasper-dev的问题,类似下面的页面
[email protected]:~/Files/opencv-4.3.0$ sudo apt-get install libjasper-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libjasper-dev
解决方案:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
其中libjasper1是libjasper-dev的依赖包,可以完成所有依赖包的安装操作。
3.编译
3.1将源代码进行解压
sudo tar -zxf opencv-4.3.0.tar.gz
sudo tar -zxf opencv_contrib4.3.0.tar.gz
3.2 在opencv-4.3.0目录下新建一个build文件夹
cd opencv-4.3.0
mkdir build
cd build
3.3 cmake一下
这里我修改了安装路径为/usr/local/opencv4,可以自己修改到自己需要的位置,如果该命令中不加-D CMAKE_INSTALL_PREFIX=/usr/local/opencv4,则默认各部分分别安装在/usr/local/目录的include/ bin/ lib/3个文件夹下。
命令说明:
-
注意OPENCV_EXTRA_MODULES_PATH 要根据你的实际情况转变,把它换成你的 contrib 目录下的 modules 目录,在我的电脑上这个目录是…/opencv3.4.12_contrib/modules, 此外,命令的最后两个.不能落掉
-
-D OPENCV_GENERATE_PKGCONFIG=YES:OpenCV4以上版本默认不使用pkg-config,该编译选项开启生成opencv4.pc文件,支持pkg-config功能。
-
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv4:指定安装目录。
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/nash/Desktop/opencv4/opencv_contrib-4.3.0/modules -D BUILD_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..
该cmake命令的结果里可以查看是否支持Python:
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.6)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
-- packages path: lib/python2.7/dist-packages
--
-- Python 3:
-- Interpreter: /usr/bin/python3.4 (ver 3.4.3)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
-- packages path: lib/python3.4/dist-packages
表明已经建立了Python绑定。(如果只选择了python3,则该输出结果里只有python3部分)
3.4 进行make编译,可能需要很久,静静等待。
sudo make -j$(nproc)
make -jn表示用n个系统内核进行编译,如果你知道自己的系统内核数目可以手写,如make -j4 make -j8等,如果不知道就可以使用make -j$(nproc)来自动获取内核数量。
有时候使用使用多线程编译会出现一些错误,为了求稳可以直接使用sudo make, 但是这样会慢一些
可能出现的错误
Makefile:162:recipe for target ‘all’ failed
make:*** [all] Error 2
原因
出现问题的原因是因为boostdesc_bgm.i等文件没有下载成功。我们到opencv/build/下的CMakeDownLog.txt中可以看到下载失败的信息,在该文件中还提供了下载链接。
解决方法
主要是2个位置的文件下载有问题:
①opencv/.cache/xfeatures2d/boostdesc
②opencv/.cache/xfeatures2d/vgg
cd到对应文件夹下,里面的文件是0K,然后用下载好的文件覆盖即可。
注:重新make前cmake一下。
具体看这里
文件下载
3.5 make install
sudo make install
到这一步代表opencv安装成功,若第4步或第5步中间问题,可以在重新make前cmake一下。实在不放心就直接删除bulid文件夹,并从建立build文件夹开始重新走一遍。
二、OpenCV配置环境
1.pkg-config环境
1.1配置OpenCV 的 pgk-config环境
找到opencv4.pc文件所在目录:
#一般在/usr/local/opencv4/lib/pkgconfig/opencv4.pc
#没找到可通过find命令查找
$ sudo find / -iname opencv4.pc
将/usr/local/opencv4/lib/pkgconfig/路径加入PKG_CONFIG_PATH:
#打开pkgconfig.sh
$ sudo vim /etc/profile.d/pkgconfig.sh
#在文件中加入下面一行:
export PKG_CONFIG_PATH=/usr/local/opencv4/lib/pkgconfig:$PKG_CONFIG_PATH
#保存退出后,使用命令**:
$source /etc/profile
验证是否配置成功:
$ pkg-config --libs opencv4
-L/usr/local/opencv4/lib -lopencv_ml -lopencv_dnn -lopencv_video -lopencv_stitching -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_photo -lopencv_gapi -lopencv_imgproc -lopencv_core
存在问题
这样配置的环境变量只能在该终端生效,切换终端或者退出该roo权限则失效,需要重新执行下面的命令
$source /etc/profile
替代方案
配置系统bash
sudo vim /etc/bash.bashrc
在文末添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4/lib/pkgconfig
export PKG_CONFIG_PATH
如下:
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4/lib/pkgconfig
export PKG_CONFIG_PATH
保存退出,然后执行如下命令使配置生效
source /etc/bash.bashrc
可以使用以下命令查看是否安装和配置成功
pkg-config --modversion opencv4
pkg-config --cflags opencv4
pkg-config --libs opencv4
结果如下:
[email protected]:~/Desktop/opencv4/opencv-4.3.0/samples/cpp/example_cmake$ pkg-config --modversion opencv4
4.3.0
[email protected]:~/Desktop/opencv4/opencv-4.3.0/samples/cpp/example_cmake$ pkg-config --cflags opencv4
-I/usr/local/opencv4/include/opencv4/opencv -I/usr/local/opencv4/include/opencv4
[email protected]:~/Desktop/opencv4/opencv-4.3.0/samples/cpp/example_cmake$ pkg-config --libs opencv4
-L/usr/local/opencv4/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
[email protected]:~/Desktop/opencv4/opencv-4.3.0/samples/cpp/example_cmake$
2.动态库环境
#配置OpenCV动态库环境——程序执行时加载动态库*.so的路径。
$ sudo vim /etc/ld.so.conf.d/opencv4.conf
#在该文件(可能是空文件)末尾加上:
/usr/local/opencv4/lib
#再执行以下命令使刚才配置的路径生效:
$ sudo ldconfig
3.python-opencv环境
找到编译好的python cv库:
$sudo find / -iname cv2*.so
$sudo find / -iname cv2*.so
/home/XXX/Downloads/opencv-4.1.1/build/lib/python3/cv2.cpython-35m-x86_64-linux-gnu.so
/usr/local/opencv4/lib/python3.5/dist-packages/cv2/python-3.5/cv2.cpython-35m-x86_64-linux-gnu.so
cv2.cpython-35m-x86_64-linux-gnu.so就是编译好的python3的opencv库,我们把它复制到对应python解释器的/path/to/dist-packages/(系统自带的python解释器)和/path/to/site-packages(用户安装的python解释器)目录下,之后就能在该python解释器中使用python-opencv库。
链接到系统自带的python3解释器中:
sudo ln -s /usr/local/opencv4/lib/python3.5/dist-packages/cv2/python-3.5/cv2.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.so
链接到Anaconda创建的虚拟环境python3解释器中:
ln -s /usr/local/opencv4/lib/python3.5/dist-packages/cv2/python-3.5/cv2.cpython-35m-x86_64-linux-gnu.so ~/anaconda3/lib/python3.7/site-packages/cv2.so
三、测试OpenCV
环境配置完成,对我们安装的OpenCV进行测试:
cd 到/opencv-4.3.0/samples/cpp/example_cmake目录下,查看当前目录内容:
[email protected]:~/Desktop/opencv4/opencv-4.3.0/samples/cpp/example_cmake$ ls -l
总用量 12
-rw-rw-r-- 1 nash nash 943 4月 3 2020 CMakeLists.txt
-rw-rw-r-- 1 nash nash 1119 4月 3 2020 example.cpp
-rw-rw-r-- 1 nash nash 281 4月 3 2020 Makefile
有两种方式进行测试:
1.通过Makefile测试
因为OpenCV 4.0需要C++11支持,且生成的pkg-config文件名为opencv4.pc,所以需要对当前目录下的Malefile文件进行修改:
$ vim Makefile
修改后的内容为:
CXX ?= g++
CXXFLAGS += -c -std=c++11 -Wall $(shell pkg-config --cflags opencv4)
LDFLAGS += $(shell pkg-config --libs --static opencv4)
all: opencv_example
opencv_example: example.o; $(CXX) $< -o [email protected] $(LDFLAGS)
%.o: %.cpp; $(CXX) $< -o [email protected] $(CXXFLAGS)
clean: ; rm -f example.o opencv_example
执行make命令生成可执行文件opencv_example:
$ ./opencv_example
2.通过Cmake测试
因为我们更改了opencv的安装路径(/usr/local/opencv4),所以测试前我们需要在CMakeLists.txt文件的10行find_package(OpenCV REQUIRED)前面加上
set(OpenCV_DIR /usr/local/opencv4/lib/cmake/opencv4)
如下:
1 # cmake needs tis line
2 cmake_minimum_required(VERSION 3.1)
3
4 # Define project name
5 project(opencv_example_project)
6
7 # Find OpenCV, you may need to set OpenCV_DIR variable
8 # to the absolute path to the directory containing OpenCVConfig.cmake file
9 # via the command line or GUI
10 set(OpenCV_DIR /usr/local/opencv4/lib/cmake/opencv4)
11 find_package(OpenCV REQUIRED)
12
13 # If the package has been found, several variables will
14 # be set, you can find the full list with descriptions
15 # in the OpenCVConfig.cmake file.
16 # Print some message showing some of them
17 message(STATUS "OpenCV library status:")
18 message(STATUS " config: ${OpenCV_DIR}")
19 message(STATUS " version: ${OpenCV_VERSION}")
20 message(STATUS " libraries: ${OpenCV_LIBS}")
21 message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
22
23 # Declare the executable target built from your sources
24 add_executable(opencv_example example.cpp)
25
26 # Link your application with OpenCV libraries
27 target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})
修改后,执行命令:
$ mkdir build && cd build
$ cmake ..
$ make
$ ./opencv_example
3.python-opencv测试
前面已经将Python-opencv库链接到系统自带python3中,所以简单的测试如下:
#python3
$ [email protected]:~$ python
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.3.0
>>>
#python2
[email protected]:~$ python2.7
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.3.0
>>>
下一篇: Ubuntu20.04安装opencv4