赞
踩
下面开始安装,环境为python3,ubuntu16.04(18.04也可以)
本文还有一个参考链拉https://gist.github.com/nathzi1505/858a140b4ab621209b0e71c8a55df221,请参看
#更新系统
sudo apt-get update
安装开发工具
sudo apt-get install build-essential cmake unzip pkg-config yasm git checkinstall
安装图像和视频 I/O库
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
sudo apt-get install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install libfaac-dev libmp3lame-dev libvorbis-dev
安装GTK作为GUI的后端
sudo apt-get install libgtk-3-dev
安装优化的数学计算包optimization libraries for OpenCV
sudo apt-get install libatlas-base-dev gfortran
OpenCore - Adaptive Multi Rate Narrow Band (AMRNB) and Wide Band (AMRWB) speech codec
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
Cameras programming interface libs
sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
Parallelism library C++ for CPU
sudo apt-get install libtbb-dev
其它
sudo apt install libgl1-mesa-glx sudo apt-get install libatlas-base-dev \ libgflags-dev \ libgoogle-glog-dev \ libhdf5-serial-dev \ libleveldb-dev \ liblmdb-dev \ libgflags-dev \ libprotobuf-dev \ libsnappy-dev \ protobuf-compiler \ libgphoto2-dev \ libeigen3-dev \ libhdf5-dev \ doxygen #安成以上步骤用时约10分钟
还需要安装openjpg:
#从 https://github.com/uclouvain/openjpeg/archive/v2.4.0/openjpeg-2.4.0.tar.gz下载安装包
cd openjpeg
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr \
-DBUILD_STATIC_LIBS=OFF ..
make -j8
sudo make install
#2019年8月29日,可以到github上找不同的版本安装方法相同
wget -O opencv.tar.gz https://github.com/opencv/opencv/archive/4.1.1.tar.gz
wget -O opencv_contrib.tar.gz https://github.com/opencv/opencv_contrib/archive/4.1.1.tar.gz
#解压
tar -xzf opencv.tar.gz
tar -xzf opencv_contrib.tar.gz
#重命名
mv opencv_contrib-4.1.1 opencv_contrib
2020年12月15日补充:同于网速原因,每次下载都很慢,所以将下载好的包上传到到CSDN上,参见:opencv4.4和opencv_contrib4.4.这两个包中有所有需要的包,例如常见的下载失败的xfeatures2d的一些内容。
在opencv包中,build文件夹已建好,我个人已经走通,先执行make clean
,然后修改make.sh文件的内容即可。
cd ~/opencv mkdir build cd build #运行cmake cmake -D CMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_C_COMPILER=/usr/bin/gcc \ -DCMAKE_INSTALL_PREFIX=/usr/local \ -DINSTALL_PYTHON_EXAMPLES=ON \ -DINSTALL_C_EXAMPLES=OFF \ -DOPENCV_ENABLE_NONFREE=ON \ -DOPENCV_EXTRA_MODULES_PATH=/opt/opencv/opencv_contrib/modules \ -DPYTHON_EXECUTABLE=/opt/conda/bin/python \ -DWITH_IPP=OFF \ -DWITH_CUDA=ON \ -DBUILD_TESTS=OFF \ -DWITH_OPENCL=OFF \ -DBUILD_PERF_TESTS=OFF \ -DBUILD_DOCS=OFF \ -DBUILD_EXAMPLES=OFF \ -DWITH_TBB=ON \ -DOPENCV_DNN_CUDA=ON \ -DBUILD_opencv_cudacodec=OFF \ -DWITH_CUBLAS=1 \ -DWITH_V4L=ON \ -DWITH_QT=OFF \ -DWITH_CUDNN=ON \ -DWITH_FFMPEG=ON \ -DWITH_OPENGL=ON \ -DWITH_GSTREAMER=ON \ -DOPENCV_GENERATE_PKGCONFIG=ON \ -DOPENCV_PC_FILE_NAME=opencv.pc \ -DOPENCV_PYTHON3_INSTALL_PATH=/opt/conda/lib/python3.8/site-packages ..
有几点要强调:
安装完应该是我这个样子或类似:
可以看到安装位置在/usr/local.
“non-free” 算法安装成功。
以上过程中会下载一些文件,很多情况下,下载不下来,会报错,boostdesc_bgm.i,boostdesc_bgm_bi.i,boostdesc_bgm_hd.i,boostdesc_bgm_hd.i 等11个文件,这些文件放在build/downloads下,需要的要以去这里下载。
#进行编译
make -j"$(nproc)" #"$(nproc)"表示核数,通常有2,4,8,根据实际来设置,lscpu可以查看是几核
#如果失败,可以make 后都不加
make完是这个样子的(中途换了台机器,原来那个是云务器,编不过)
然后安装
sudo make install
sudo ldconfig
python3
import cv2
print(cv2.__version__)
这个是没问题的,但是在安装darknet时,开启Opencv选项时,总提示“Package opencv was not found in the pkg-config search path”并提示将opencv.pc加到PKG_CONFIG_PATH环境变量中。
执行:
pkg-config --cflags opencv
输出:
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文件:
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.5.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
Version可以按照真实版本来。
保存退出,然后将文件导入到环境变量:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
#或写入.bashrc
#并source ~/.bashrc
至此就配置好opencv.pc啦~
再执行 pkg-config --cflags --libs opencv时输出结果如下:
-I/usr/local/include/opencv4 -L/usr/local/lib \
-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
这部分要参考:https://blog.csdn.net/PecoHe/article/details/97476135
查看opencv版本命令:
pkg-config --modversion opencv
看下opencv算法在cpu和gpu下速度的不同:
upload可以装同存中数据转到相关device中。同时可以看到相关的进程。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。