sudo apt-get install build-essential
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git(可选)
4.1 版本(我使用迅雷下啦):
# curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip
# curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip
unzip opencv-4.1.0.zip
unzip opencv_contrib-4.1.0.zip(可选)
# 进入 opencv目录
cd opencv-4.1.0
# 创建build目录
mkdir build
#进入build目录
cd build
# CUDA_ARCH_BIN (TX2) 6.2 , (TX1 / Nano) 5.3
#执行cmake
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \
-D CUDA_ARCH_BIN=6.2 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDA=ON \
-D WITH_TBB=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D WITH_V4L=ON \
-D BUILD_EXAMPLES=OFF \
-D WITH_OPENGL=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_NVCUVID=ON \
-D WITH_GSTREAMER=ON \
-D WITH_OPENCL=YES \
-D BUILD_opencv_cudacodec=OFF ..
编译过程摘录(会下载一些模型)
-- Performing Test HAVE_CXX_WNO_UNUSED_BUT_SET_VARIABLE
-- Performing Test HAVE_CXX_WNO_UNUSED_BUT_SET_VARIABLE - Success
-- Excluding from source files list: modules/imgproc/src/corner.avx.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.sse4_1.cpp
-- Excluding from source files list: modules/imgproc/src/resize.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/resize.sse4_1.cpp
-- Excluding from source files list: modules/imgproc/src/sumpixels.avx512_skx.cpp
-- Excluding from source files list: /modules/dnn/layers/layers_common.avx.cpp
-- Excluding from source files list: /modules/dnn/layers/layers_common.avx2.cpp
-- Excluding from source files list: /modules/dnn/layers/layers_common.avx512_skx.cpp
-- freetype2: YES
-- harfbuzz: YES
-- Excluding from source files list: modules/features2d/src/fast.avx2.cpp
-- Performing Test HAVE_CXX_WNO_OVERLOADED_VIRTUAL
-- Performing Test HAVE_CXX_WNO_OVERLOADED_VIRTUAL - Success
-- Checking for modules 'tesseract;lept'
-- No package 'tesseract' found
-- No package 'lept' found
-- Tesseract: NO
-- Excluding from source files list: modules/calib3d/src/undistort.avx2.cpp
-- xfeatures2d/boostdesc: Download: boostdesc_bgm.i
-- xfeatures2d/boostdesc: Download: boostdesc_bgm_bi.i
-- xfeatures2d/boostdesc: Download: boostdesc_bgm_hd.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_064.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_128.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_256.i
-- xfeatures2d/boostdesc: Download: boostdesc_lbgm
sudo make -j4
如果编译不成功,把脚本中make -j3 改成make -j1
[100%] Built target gen_opencv_python_source
[100%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python2
[100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-36m-aarch64-linux-gnu.so
[100%] Built target opencv_python3
[100%] Built target opencv_annotation
[100%] Built target opencv_visualisation
[100%] Built target opencv_interactive-calibration
[100%] Built target opencv_version
sudo make install
我的 cv2 路径:
opencv-4.1.0/build/install/lib/python3.6/site-packages/cv2
我是 virtualenvs 做的 py 环境隔离 所以 拷贝 路径 为 (可以加 sudo):
cp -r /home/tx2/project/opencv4/opencv-4.1.0/build/install/lib/python3.6/site-packages/cv2 /home/tx2/.virtualenvs/tf14_py36/lib/python3.6/site-packages
另一种 (利用软链接)做法(Raspberry 适用):
$ cd /usr/local/lib/python3.6/site-packages/
$ sudo mv cv2.cpython-36m-arm-linux-gnueabihf.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.6/site-packages/
$ ln -s /usr/local/lib/python3.6/site-packages/cv2.so cv2.so
(tf14_py36)tx2@tx2-desktop:python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.1.0
>>>