转载:http://dev.t7.ai/jetson/opencv/
Installing OpenCV 3.1 on Jetson TX1
Uninstall OpenCV4Tegra
Reference here
Uninstall OpenCV for Tegra packages, purge local repositories, and update the apt cache using the following commands:
sudo apt-get purge libopencv4tegra-dev libopencv4tegra
sudo apt-get purge libopencv4tegra-repo
sudo apt-get update
Installing OpenCV 3.1
required packages
Base requirements
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
GStreamer support (recommended)
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
Python support
sudo apt-get install python2.7-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
OpenGL support
sudo apt-get install libgtkglext1 libgtkglext1-dev
sudo apt-get install qtbase5-dev
video4linux2 support (better handling of usb cameras modes)
sudo apt-get install libv4l-dev v4l-utils qv4l2 v4l2ucp
download sources
- Download from latest sources. You may want this option as it contains a compatibility fix to allow CUDA 8.0.
git clone https://github.com/opencv/opencv.git
- Download from a stable version from official site
curl -L https://github.com/opencv/opencv/archive/3.2.0.zip -o opencv-3.2.0.zip
unzip opencv-3.2.0.zip
cd opencv-3.2.0
compiling
This is a list of usefull cmake
params to add some extra features to your OpenCV compilation to get things working on your TX1.
required
- CMAKE_BUILD_TYPE=RELEASE compiles the release version of OpenCV
- CMAKE_INSTALL_PREFIX=/usr/local path where OpenCV will be installed
- WITH_CUDA=ON
- CUDA_ARCH_BIN="5.3" check latest version in CUDA GPUs reference
- CUDA_ARCH_PTX=""
optional
- WITH_OPENGL=ON OpenGL compatibility, requires GTK+ OpenGL Extensions the QT backend. When this guide was write, compilation faild for the combination CUDA 8.0 + OpenGL support.
- WITH_LIBV4L=ON will use video4linux2 libraries
- CUDA_FAST_MATH=ON Mathematical operations will run faster but less precise. See intrinsic mathematical functions
- ENABLE_PRECOMPILED_HEADERS=OFF your TX1 does not have enough disk space for pre-compiling everything but you can trade space required by compilation time with this flag. Other options is to cross compile from a desktop pc or to download & compile from an external device, an USB 3.0 external drive will do the work and is fast enough.
- ENABLE_NEON=ON ref
- BUILD_TESTS=OFF disable tests
- BUILD_PERF_TESTS=OFF disable performance tests
- BUILD_EXAMPLES=OFF disable examples
mkdir release
cd release
cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D WITH_OPENGL=ON -D WITH_LIBV4L=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
After running the cmake
command everything is ready to be compiled, it worth to check that you got the following outputs on the cmake
summary:
TX1 platform
-- Platform:
-- Host: Linux 3.10.96-tegra aarch64
-- Configuration: RELEASE
CUDA 8.0
-- Other third-party libraries:
-- Use Cuda: YES (ver 8.0)
-- NVIDIA CUDA
-- Use CUFFT: YES
-- Use CUBLAS: NO
-- USE NVCUVID: NO
-- NVIDIA GPU arch: 53
-- NVIDIA PTX archs:
-- Use fast math: NO
OpenGL
-- GUI:
-- GTK+ 2.x: YES (ver 2.24.30)
-- OpenGL support: YES (/usr/lib/aarch64-linux-gnu/libGLU.so /usr/lib/aarch64-linux-gnu/libGL.so)
If you enabled video4linux2
-- Video I/O:
-- V4L/V4L2: Using libv4l1 (ver 1.10.0) / libv4l2 (ver 1.10.0)
If you enabled GStreamer
-- Video I/O:
-- GStreamer:
-- base: YES (ver 1.8.2)
-- video: YES (ver 1.8.2)
-- app: YES (ver 1.8.2)
-- riff: YES (ver 1.8.2)
-- pbutils: YES (ver 1.8.2)
installing
Now that all dependencies are ready, is time to compile
make -j4 # ... because you want to use all your fancy TX1 cores isn't it? ;)
sudo make install
Other Resources
Extra parameters used for TX1 here
Official OpenCV install guide here
video4linux2 useful commands
This command changes camera mode to full hd resolution
v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1
v4l2-ctl --set-parm=30
v4l2-ctl -V
Python script to test change of resolution
import cv2
vc = cv2.VideoCapture(0)
print "capture stream open"
if not vc.isOpened():
print "unable to capture camera"
exit(-1)
print "resolution is %dx%d" % (vc.get(cv2.CAP_PROP_FRAME_WIDTH), vc.get(cv2.CAP_PROP_FRAME_HEIGHT))
print "attempt to switch to full hd"
vc.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
print "resolution is %dx%d" % (vc.get(cv2.CAP_PROP_FRAME_WIDTH), vc.get(cv2.CAP_PROP_FRAME_HEIGHT))
_, frame = vc.read()
print "frame captured"
cv2.imwrite("frame.png", frame)