走完卸载自带CV篇的小伙伴这边请,打开你的终端,按照如下步骤输入信息(声明:这里主体遵循了JetsonHacks的步骤,感谢)
首先新建一个文件夹,假如我的地址为/home/nano/opencv4
# 这是一句变量定义,定义了你要下载CV的绝对位置
OPENCV_SOURCE_DIR=$/home/nano/opencv4
PACKAGE_OPENCV="-D CPACK_BINARY_DEB=ON"
# 遍历命令行输入
while [ "$1" != "" ]; do
case $1 in
-s | --sourcedir ) shift
OPENCV_SOURCE_DIR=$1
;;
-i | --installdir ) shift
/usr/local=$1
;;
--no_package ) PACKAGE_OPENCV=""
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
CMAKE_INSTALL_PREFIX=/usr/local
# 安装必要的支持包
cd PWD
sudo apt-get install -y \
build-essential \
cmake \
libavcodec-dev \
libavformat-dev \
libavutil-dev \
libeigen3-dev \
libglew-dev \
libgtk2.0-dev \
libgtk-3-dev \
libjpeg-dev \
libpng-dev \
libpostproc-dev \
libswscale-dev \
libtbb-dev \
libtiff5-dev \
libv4l-dev \
libxvidcore-dev \
libx264-dev \
qt5-default \
zlib1g-dev \
pkg-config
# 获得openGL的支持
cd /usr/local/cuda/include
sudo patch -N cuda_gl_interop.h PWD'/patches/OpenGLHeader.patch'
#python支持,可以只要一个版本
# Python 2.7
sudo apt-get install -y python-dev python-numpy python-py python-pytest
# Python 3.6
sudo apt-get install -y python3-dev python3-numpy python3-py python3-pytest
# GStreamer支持
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
#从GitHub上下载cv4.1.1的两个包(很大,约900M)建议忽略本步,科学上网下载好,传到你的安装目录里
cd $OPENCV_SOURCE_DIR
git clone --branch "4.1.1" https://github.com/opencv/opencv.git
git clone --branch "4.1.1" https://github.com/opencv/opencv_contrib.git
#修补一些库的问题
cd $OPENCV_SOURCE_DIR/opencv
sed -i 's/include /include /g' modules/core/include/opencv2/core/private.hpp
#创建build文件夹,用于cmake
cd $OPENCV_SOURCE_DIR/opencv
mkdir build
cd build
#可以按需求自己更改条目以及on、off
time cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN=${ARCH_BIN} \
-D CUDA_ARCH_PTX="" \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_LIBV4L=ON \
-D WITH_V4L=ON \
-D WITH_GSTREAMER=ON \
-D WITH_GSTREAMER_0_10=OFF \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D BUILD_opencv_python2=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
$"PACKAGE_OPENCV" \
../
#如果成功,终端输出CMake configuration make successful,有时出错,再来一次就好了
if [ $? -eq 0 ] ; then
echo "CMake configuration make successful"
else
# Try to make again
echo "CMake issues " >&2
echo "Please check the configuration being used"
exit 1
fi
#开始make
time make -j1
漫长的make过程,本人花费近3小时,据说改过最大交换内存可以提速至1.5h完成,nano是4核的,如果你是从内存卡上装,一定填j1,j4会死机,部分文章说填j8,不会报错,只会死的更快,然后只能从头开始。
中间会有一次bug,不要慌,报错如下
...
Scanning dependencies of target opencv_xfeatures2d
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/xfeatures2d_init.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/pct_signatures.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/affine_feature2d.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/gms.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o
~/opencv4/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:654:37: fatal error: boostdesc_bgm.i: No such file or directory
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/build.make:163: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o] Error 1
CMakeFiles/Makefile2:12636: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
解决办法
进入build
目录,下载所需要的文件到opencv_contrib/modules/xfeatures2d/src/
下,包含的文件有:boostdesc_bgm.i、boostdesc_bgm_bi.i、boostdesc_bgm_hd.i、boostdesc_lbgm.i、boostdesc_binboost_064.i、boostdesc_binboost_128.i、boostdesc_binboost_256.i、vgg_generated_120.i、vgg_generated_64.i、vgg_generated_80.i、vgg_generated_48.i
下载好放入上述目录即可,这里也上传资源辽(好好找不花钱也找的到)
然后,一定执行make
make
即可继续上次进度,如果执行其他,那还得把之前make好的部分做检查,会多费一点点时间
#在终端输入如下,如果成功会打印成功!
if [ $? -eq 0 ] ; then
echo "OpenCV make successful"
else
# Try to make again; Sometimes there are issues with the build
# because of lack of resources or concurrency issues
echo "Make did not build " >&2
echo "Retrying ... "
# Single thread this time
make
if [ $? -eq 0 ] ; then
echo "OpenCV make successful"
else
# Try to make again
echo "Make did not successfully build" >&2
echo "Please fix issues and retry build"
exit 1
fi
fi
如果不成功,在make一次,有时再make一次会解决一些bug,这里给到(丰色木夕前辈的bug解决文章链接:https://me.csdn.net/youlinhuanyan) 以示敬仰,虽然执行我的步骤不会有那么多bug,但该博主的bug解决堪称经典
#最后执行make安装
sudo make install
sudo ldconfig
哈哈,到这里就装完了,不放心可以做个小测试
输入 sudo opencv_version
输出4.1.1
完结撒花!
致敬jetsonhacks https://www.jetsonhacks.com/2019/11/22/opencv-4-cuda-on-jetson-nano/