Ubuntu20.04安装OpenCV4.5.1的详细步骤

1、安装依赖项

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
    libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

2、下载安装包并解压

直接从git库clone源后面编译有一定问题,遂直接下载安装包进行编译,可行。
在这里插入图片描述在这里插入图片描述在这里插入图片描述

tar -zxvf opencv.tar.gz
tar -zxvf opencv_contrib-4.5.1.tar.gz

3、CMake编译

cd opencv-4.5.1
mkdir build
cd build
#这步是重中之重。
cmake  -D CMAKE_BUILD_TYPE=RELEASE  
	   -D CMAKE_INSTALL_PREFIX=/usr/local  
	   -D WITH_TBB=ON  
	   -D WITH_V4L=ON  
	   -D WITH_QT=ON  
	   -D WITH_GTK=ON  
	   -D WITH_OPENGL=ON  
	   -D WITH_VTK=ON  
	   -D OPENCV_GENERATE_PKGCONFIG=YES  
	   -D INSTALL_C_EXAMPLES=ON  
	   -D INSTALL_PYTHON_EXAMPLES=ON  
	   -D OPENCV_GENERATE_PKGCONFIG=ON  
	   -D OPENCV_ENABLE_NONFREE = ON //surf特征检测用到
	   -D OPENCV_EXTRA_MODULES_PATH=/home/audrey/Desktop/workspace/opencv4.5/opencv_contrib-4.5.1/modules  #自己的路径哟
	   -D BUILD_EXAMPLES=ON ..

问题1 编译OpenCV提示opencv_contrib缺少以下文件在这里插入图片描述

/opencv_contrib-4.5.1/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory

解决方法
cd /home/audrey/Desktop/workspace/opencv4.5/opencv-4.5.1/.cache/xfeatures2d/
unzip /home/audrey/Downloads/patch.zip

使用以上命令把该压缩包https://github.com/opencv/opencv_contrib/files/4520852/patch__.zip解压到.cache/xfeatures2d/代替原有的boostdesc和vgg两个文件夹

make -j8
sudo make install

4、检验是否安装成功

pkg-config --modversion opencv4
python3 -c "import cv2; print(cv2.__version__)"

5、运行实例

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
        Mat img = imread("./love.jpg");
        imshow("LOVE", img);
        waitKey(0);
        return 0;
}
g++ -o main showPhoto.cpp `pkg-config opencv4 --cflags --libs`

./main

问题2 OpenCV runtime error: “libopencv_core.so.3.2: cannot open shared object file: No such file or directory” in Fedora 24

解决方法

1.Find the folder containing the shared library libopencv_core.so.3.2 using the following command line.

 sudo find / -name "libopencv_core.so.3.2*"

Then I got the result: /usr/local/lib/libopencv_core.so.3.2.
2. Create a file called /etc/ld.so.conf.d/opencv.conf and write to it the path to the folder where the binary is stored.For example, I wrote /usr/local/lib/ to my opencv.conf file.
3. Run the command line as follows.

sudo ldconfig -v

4.Try to run the test binary again.

问题三 缺失face_landmark_model.dat

解决方法

下载face_landmark_model.dat文件,把它放到/home/audrey/Desktop/workspace/opencv4.5/opencv-4.5.1/.cache/data/目录下

参考资料

How to Install OpenCV on Ubuntu 20.04
ubuntu 20.04下编译供C++/python使用的opencv
Opencv安装编译时,未启用QT的问题

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值