首页 > 代码库 > TX1使用OpenCV3.1实时采集视频图像

TX1使用OpenCV3.1实时采集视频图像

转载请注明作者和出处: http://blog.csdn.net/c406495762
嵌入式平台(Target): Jeston TX1
OpenCV: OpenCV3.1
摄像头: USB 800W摄像头

  • 前言
  • 下载OpenCV3
  • 安装依赖库
  • 编译并安装OpenCV3
  • 运行hog例程
    • 1 编译并运行例程
    • 2 运行结果
  • 编写简单的摄像头采集程序
    • 1 代码如下
    • 2 运行结果
  • 可能遇到的问题

1 前言

    Jetpack3.0安装的OpenCV是OpenCV2.4.13,OpenCV2.4.13对于USB摄像头支持不好。NVIDIA以后更新的Jetpack会解决对USB摄像头支持不好的问题,在官方解决之前,一个临时的解决方案就是自己编译OpenCV3.1。

2 下载OpenCV3

    URL : http://www.opencv.org/releases.html

    点击Sources下载源码:

技术分享

3 安装依赖库

# Some general development libraries
sudo apt-get install -y build-essential make cmake cmake-curses-gui g++
# libav video input/output development libraries
sudo apt-get install -y libavformat-dev libavutil-dev libswscale-dev
# Video4Linux camera development libraries
sudo apt-get install -y libv4l-dev
# Eigen3 math development libraries
sudo apt-get install -y libeigen3-dev
# OpenGL development libraries (to allow creating graphical windows)
sudo apt-get install -y libglew1.6-dev
# GTK development libraries (to allow creating graphical windows)
sudo apt-get install -y libgtk2.0-dev

4 编译并安装OpenCV3

cd ~
mkdir src
cd src
unzip ~/Downloads/opencv-3.1.0.zip
cd opencv-3.1.0
mkdir build
cd build
cmake -DWITH_CUDA=ON -DCUDA_ARCH_BIN="5.3" -DCUDA_ARCH_PTX="" -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCUDA_FAST_MATH=ON -DCMAKE_INSTALL_PREFIX=/home/ubuntu/opencv-3.1.0 ..
make -j4 install

5 运行hog例程

5.1 编译并运行例程

cd ~/src/opencv-3.1.0/samples/gpu
g++ -o hog -I /home/ubuntu/opencv-3.1.0/include -O2 -g -Wall hog.cpp -L /home/ubuntu/opencv-3.1.0/lib -lopencv_core -lopencv_imgproc -l opencv_flann -l opencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_objdetect -lopencv_photo -lopencv_features2d -lopencv_calib3d -lopencv_stitching -lopencv_videostab -lopencv_shape -lopencv_cudaobjdetect -lopencv_cudawarping -lopencv_cudaimgproc
export LD_LIBRARY_PATH=/home/ubuntu/opencv-3.1.0/lib:$LD_LIBRARY_PATH
./hog --camera 0
  • export LD_LIBRARY_PATH=/home/ubuntu/opencv-3.1.0/lib:$LD_LIBRARY_PATH:添加LIBARARY,这个只是临时添加,永久添加可以放到~/.bashrc中,并使用source ~/.bashrc“激活”设置的环境变量;

  • ./hog –camera 0:最后一个参数0,代表使用/dev/video0设备,如果TX1插上板载的CSI摄像头,又插上USB摄像头,这个0,应该改为1。可以通过ls /dev/ | grep video*,查看有那些video设备。

技术分享

5.2 运行结果

技术分享

6 编写简单的摄像头采集程序

6.1 代码如下

#include <stdio.h>  
#include <opencv2/opencv.hpp>  

using namespace cv;  
using namespace std;  

int main(int argc, char** argv)  
{  
    VideoCapture cap("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)24/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink");  
    if (!cap.isOpened())  
    {  
        cout << "Failed to open camera." << endl;  
        return -1;  
    }  

    for(;;)  
    {  
        Mat frame;  
        cap >> frame;  
        imshow("original", frame);  
        //waitKey(1);  
        if(waitKey(30) >= 0)  
        break;  
    }  
    return 0;  
}

6.2 运行结果

    PS : 阳光原因,图像有些泛白,见谅

技术分享

7 可能遇到的问题

    在make的时候,我们可能遇到如下error:

技术分享

    解决办法:需要修改一处源码,在graphcuts.cpp中将

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

    改为:

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)

    如下图所示:

技术分享

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

    TX1使用OpenCV3.1实时采集视频图像