配置和编译OpenCV

1.下载官方的OpenCV来源

https://github.com/opencv/opencv

https://opencv.org/releases/

2.配置&编译安装

mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

sudo make –j8

可以使用sudo make -j 8 、sudo make -j4 、sudo make
我这里就使用sudo make -j 8了

3.安装

sudo make install

4.配置环境

用gedit打开/etc/ld.so.conf
在文件中加上一行 /usr/loacal/lib
其中/user/loacal是opencv安装路径也就是makefile中指定的安装路

sudo gedit /etc/ld.so.conf

在这里插入图片描述

运行sudo ldconfig,
修改bash.bashrc文件

sudo gedit /etc/bash.bashrc 

在文件末尾加入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

在这里插入图片描述

然后在命令行中输入

source /etc/bash.bashrc

5.检验

在命令行中输入如下命令:

pkg-config opencv –modversion




出错

执行检验有可能出以下错误

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

经过网上详细查询资料后,是缺失了opencv.pc这个配置信息文件,故解决方法就是添加这个文件然后将其导入到环境变量中,具体操作如下:

首先创建opencv.pc文件,这里要注意它的路径信息

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

然后在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:

prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version:4.0.1
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann  -lopencv_core
~     

posted @ 2021-12-03 10:07  wuyuan2011woaini  阅读(270)  评论(0编辑  收藏  举报