一个愿意伫立在巨人肩膀上的农民......
一、安装下载所需工具
1.打开终端,输入以下命令来更新软件源:
sudo apt-get update
2.安装wget:
sudo apt-get install wget
3.下载opencv和opencv-contrib包:
wget -O opencv-4.8.0.zip https://github.com/opencv/opencv/archive/refs/tags/4.8.0.zip
wget -O opencv-contrib-4.8.0.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.8.0.zip
解压opencv-4.8.0.zip和opencv-contrib-4.8.0.zip,并解压出来的文件放在任意你喜欢的位置。
二、安装opencv依赖包
sudo apt-get 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-dev \
libopenexr-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer1.0-dev \
libopencv-highgui-dev
如果以上依赖安装报网络错误可以使用如下指令更换下载源为‘豆瓣源’
sudo add-apt-repository ‘deb http://security.ubuntu.com/ubuntu xenial-security main’
sudo apt update
三、编译opencv
1.首先我们来到第一节最后你喜欢放opencv和opencv-contrib文件夹的那个位置,在当前窗口右键‘在终端打开’,在打开的终端里输入:
mv opencv_contrib-4.8.0 opencv-4.8.0
该指令是将opencv_contrib_4.8.0文件夹放入opencv-4.8.0文件夹下,再执行:
cd opencv-4.8.0
该指令是前往opencv-4.8.0文件夹下,再执行:
mkdir build
cd build
该指令是在当前目录下新建build文件夹,并进入build文件夹下。
2.使用如下指令进行预编译:
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D CMAKE_C_COMPILER=/usr/bin/gcc-11 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_opencv_python3=ON \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_TBB=ON \
-D OPENCV_DNN_CUDA=OFF \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_ARCH_BIN=8.9 \
-D WITH_CUBLAS=1 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/ub/opencv-4.8.0/opencv_contrib_4.8.0/modules \
-D PYTHON3_EXECUTABLE=/home/ub/anaconda3/envs/py10/bin/python3.10 \
-D PYTHON3_INCLUDE_DIR=/home/ub/anaconda3/envs/py10/include/python3.10 \
-D PYTHON3_LIBRARY=/home/ub/anaconda3/envs/py10/lib/libpython3.10.so.1.0 \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/ub/anaconda3/envs/py10/lib/python3.10/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/home/ub/anaconda3/envs/py10/lib/python3.10/site-packages \
-D PYTHON_DEFAULT_EXECUTABLE=/home/ub/anaconda3/envs/py10/bin/python3.10 \
-D CUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.0 \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda-11.8/targets/x86_64-linux/include \
-D CUDA_CUDA_LIBRARY=/usr/local/cuda-11.8/targets/x86_64-linux/lib/stubs/libcuda.so \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/ub/anaconda3/envs/py10/lib/python3.10/site-packages \
-D WITH_WEBP=OFF \
-D WITH_OPENCL=OFF \
-D ETHASHLCL=OFF \
-D ENABLE_CXX11=ON \
-D BUILD_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D HAVE_opencv_python3=ON ..
此处预编译可能出现相应路径下找不到文件,因此按照自己电脑中cuda和cudnn安装的路径进行更改,其中本人由于安装cudnn时cudnn.h和libcudnn.so.8.9.0两个文件路径不对,因此此处解决方案是将两个文件找到之后,使用如下指令直接拷贝到报错处的目标文件夹下。
sudo cp /usr/include/cudnn.h /usr/local/cuda-11.8/targets/x86_64-linux/include/
因为该拷贝有权限限制,建议使用指令获取权限拷贝。
3.编译
预编译完成之后,使用下方指令查看电脑核数:
nproc
使用下方指令开始加速编译和下载:
sudo make -j24
sudo make install
编译成功如下界面:
再执行:
sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
四、配置环境变量
使用如下指令打开bashrc并对bashrc文件进行编辑并保存。
sudo gedit ~/.bashrc
在打开的.bashrc最后添加如下语句:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:$PKG_CONFIG_PATH
保存并关闭,使用如下指令进行更新:
source ~/.bashrc
五、检查是否安装成功
检查opencv是否安装成功:
pkg-config --modversion opencv4
pkg-config --libs opencv4
打印如下界面,即是安装成功。
1.C++程序验证
也可以使用如下C++程序对代码进行测试,showPhoto.cpp
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("./chart_yolov4_car_person_v7.png");
imshow("LOVE", img);
waitKey(0);
return 0;
}
编译showPhoto.cpp运行
g++ -o main showPhoto.cpp `pkg-config opencv4 --cflags --libs`
./main
2.python程序验证
在终端上启动 python 并执行以下操作:
import cv2
print(cv2.getBuildInformation())
终端打印如下:
(base) ub@ub:~$ python
Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.getBuildInformation())
General configuration for OpenCV 4.6.0 =====================================
Version control: unknown
Platform:
Timestamp: 2022-06-07T10:28:59Z
Host: Linux 5.13.0-1025-azure x86_64
CMake: 3.22.5
CMake generator: Unix Makefiles
CMake build tool: /bin/gmake
Configuration: Release
CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (16 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (4 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (31 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
C/C++:
Built as dynamic libs?: NO
C++ standard: 11
C++ Compiler: /usr/lib/ccache/compilers/c++ (ver 10.2.1)
C++ flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/lib/ccache/compilers/cc
C flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/root/ffmpeg_build/lib -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/root/ffmpeg_build/lib -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
ccache: YES
Precompiled headers: NO
Extra dependencies: /lib64/libopenblas.so Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /usr/local/lib/libpng.so /lib64/libz.so dl m pthread rt
3rdparty dependencies: libprotobuf ade ittnotify libjpeg-turbo libwebp libtiff libopenjp2 IlmImf quirc ippiw ippicv
OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
Disabled: world
Disabled by dependency: -
Unavailable: java python2 ts
Applications: -
Documentation: NO
Non-free algorithms: NO
GUI: QT5
QT: YES (ver 5.15.0 )
QT OpenGL support: NO
GTK+: NO
VTK support: NO
Media I/O:
ZLib: /lib64/libz.so (ver 1.2.7)
JPEG: libjpeg-turbo (ver 2.1.2-62)
WEBP: build (ver encoder: 0x020f)
PNG: /usr/local/lib/libpng.so (ver 1.6.37)
TIFF: build (ver 42 - 4.2.0)
JPEG 2000: build (ver 2.4.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
Parallel framework: pthreads
Trace: YES (with Intel ITT)
Other third-party libraries:
Intel IPP: 2020.0.0 Gold [2020.0.0]
at: /io/_skbuild/linux-x86_64-3.6/cmake-build/3rdparty/ippicv/ippicv_lnx/icv
Intel IPP IW: sources (2020.0.0)
at: /io/_skbuild/linux-x86_64-3.6/cmake-build/3rdparty/ippicv/ippicv_lnx/iw
VA: NO
Lapack: YES (/lib64/libopenblas.so)
Eigen: NO
Custom HAL: NO
Protobuf: build (3.19.1)
OpenCL: YES (no extra features)
Include path: /io/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 3:
Interpreter: /opt/python/cp36-cp36m/bin/python3.6 (ver 3.6.15)
Libraries: libpython3.6m.a (ver 3.6.15)
numpy: /opt/python/cp36-cp36m/lib/python3.6/site-packages/numpy/core/include (ver 1.13.3)
install path: python/cv2/python-3
Python (for build): /bin/python2.7
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: /io/_skbuild/linux-x86_64-3.6/cmake-install
-----------------------------------------------------------------
python测试程序:
#读取图片
import cv2
frame=cv2.imread('test.png')
#上传到gpu进行处理
gpu_frame=cv2.cuda_GpuMat()
gpu_frame.upload(frame)
print(gpu_frame.cudaPtr())
#resize
gpu_resframe=cv2.cuda.resize(gpu_frame,(1024,512))
cpu_resfram=gpu_resframe.download()
print(cpu_resfram.shape)
终端答应结果如下:
至此opencv_cuda版本安装完毕。
温馨提示:如果不需要连接CUDA可以在第三节第二条中删除参数:
PYTHON3_EXECUTABLE
PYTHON3_INCLUDE_DIR
PYTHON3_LIBRARY
PYTHON3_NUMPY_INCLUDE_DIRS
PYTHON3_PACKAGES_PATH
PYTHON_DEFAULT_EXECUTABLE
OPENCV_PYTHON3_INSTALL_PATH
祝使用愉快,与君共勉。
欢迎在这里评论、沟通、指正。
创作不易,引用请附原文链接。。。。。。