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源后面编译有一定问题,遂直接下载安装包进行编译,可行。
Ubuntu20.04安装OpenCV4.5.1的详细步骤_第1张图片Ubuntu20.04安装OpenCV4.5.1的详细步骤_第2张图片Ubuntu20.04安装OpenCV4.5.1的详细步骤_第3张图片

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缺少以下文件Ubuntu20.04安装OpenCV4.5.1的详细步骤_第4张图片

/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
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的问题

你可能感兴趣的:(OpenCV,opencv)