jetson xavier nx自带的opencv版本是4.1,有的时候,我们需要3.x版本的opencv,这时候就需要我们自己编译opencv。
脚本如下:
set -efolder=${HOME}/srcmkdir -p $folderecho "** Purge old opencv installation"sudo apt-get purge -y libopencv*echo "** Install requirements"sudo apt-get updatesudo apt-get install -y build-essential make cmake cmake-curses-gui git g++ pkg-config curlsudo apt-get install -y libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libeigen3-dev libglew-dev libgtk2.0-dev libpostproc-devsudo apt-get install -y libtbb2 libtbb-dev libv4l-dev v4l-utils qv4l2 v4l2ucpsudo apt-get install -y libdc1394-22-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev# sudo apt-get install -y libjasper-devsudo apt-get install -y libjpeg8-dev libjpeg-turbo8-dev libtiff-dev libpng-dev libjpeg-dev libtiff5-devsudo apt-get install -y libxvidcore-dev libx264-dev libgtk-3-devsudo apt-get install -y libatlas-base-dev libopenblas-dev liblapack-dev liblapacke-dev gfortransudo apt-get install -y qt5-defaultsudo apt-get install -y zlib1g-dev libgl1 libglvnd-devsudo apt-get install -y python2.7-dev python3.6-dev python3-testresourcesrm -f $folder/get-pip.pywget https://bootstrap.pypa.io/get-pip.py -O $folder/get-pip.pysudo python3 $folder/get-pip.pysudo python2 $folder/get-pip.pysudo pip3 install protobufsudo pip2 install protobufsudo pip3 install -U numpy matplotlibsudo pip2 install -U numpy matplotlibif [ ! -f /usr/local/cuda/include/cuda_gl_interop.h.bak ]; thensudo cp /usr/local/cuda/include/cuda_gl_interop.h /usr/local/cuda/include/cuda_gl_interop.h.bakfisudo patch -N /usr/local/cuda/include/cuda_gl_interop.h < opencv/cuda_gl_interop.h.patch && echo "** '/usr/local/cuda/include/cuda_gl_interop.h' appears to be patched already. Continue..."echo "** Download opencv-3.4.0"cd $folderif [ ! -f opencv-3.4.0.zip ]; thenwget /opencv/opencv/archive/3.4.0.zip -O opencv-3.4.0.zipfiif [ -d opencv-3.4.0 ]; thenecho "** ERROR: opencv-3.4.0 directory already exists"exitfiunzip opencv-3.4.0.zip cd opencv-3.4.0/echo "** Building opencv..."mkdir buildmkdir -p build/installcd build/cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=install -D WITH_CUDA=ON -D CUDA_ARCH_BIN="7.2" -D CUDA_ARCH_PTX="" -D WITH_CUBLAS=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D ENABLE_NEON=ON -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D WITH_QT=ON -D WITH_OPENGL=ON -D CUDA_NVCC_FLAGS="--expt-relaxed-constexpr" -D WITH_TBB=ON ..make -j8sudo make installsudo ldconfigpython3 -c 'import cv2; print("python3 cv2 version: %s" % cv2.__version__)'python2 -c 'import cv2; print("python2 cv2 version: %s" % cv2.__version__)'echo "** Install opencv-3.4.0 successfully"
可以使用参数 -D CMAKE_INSTALL_PREFIX指定安装路径,我这里不把opencv安装在/usr/locla目录下,防止和系统的opencv发生冲突。安装在build/install目录下
使用的时候,我们手动指定opencv的查找路径即可。在CMakeLists.txt中添加下面的语句:
FIND_PACKAGE(OpenCV REQUIRED)set(CMAKE_PREFIX_PATH "你的opencv安装路径")
参考链接:
/article/14863019134/
//11/08/build-opencv-3-4-on-nvidia-jetson-agx-xavier-developer-kit/
如果觉得《jetson xavier nx安装opencv3.4》对你有帮助,请点赞、收藏,并留下你的观点哦!