这文章相当好,没有理由不转载

I have always been using OpenCV’s VideoCapture API to capture images from webcam or USB cameras. OpenCV supportsV4L2 and I wanted to use something other than OpenCV’s VideoCapture API so I started digging up about v4l2 and got few links using and few examples using which I successfully wrote a small code to grab an image usingV4L2 and convert it to OpenCV’s Mat structure and display the image.
What is V4L2?

V4L2 is the second version of Video For Linux which is a video capturing API for Linux.Here you can find amazing documentation about the API. So it gives you a very easy inteface to use it with C, C++ and Python. I haven’t tried Python bindings yet.
How To Use V4L2 API?

I started reading documentation but didn’t really understand much until I foundthis example. The code had some issues and wasn’t working properly. But I just copied it and tried understanding it. So this is my understanding of the code.
Step 1: Open the Capture Device.

In Linux, default capture devide is generally /dev/video0, but if you’re using USB webcams, the index will vary accordingly.

int fd;
fd = open("/dev/video0", O_RDWR);
if (fd == -1)
{// couldn't find capture deviceperror("Opening Video device");return 1;
}

Step 2: Query the Capture

So, basically you check if the capture is available or not. V4L2 doesn’t support some cameras so it would throw an error here. We need to usev4l2_capability structure and VIDIOC_QUERYCAP to query the capture. Read Morehere.

struct v4l2_capability caps = {0};
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &caps))
{perror("Querying Capabilites");return 1;
}

Here xioctl is a wrapper function over ioctl. ioctl() is a function to manipulate device parameters of special files. Read morehere.

#include <sys/ioctl.h>static int xioctl(int fd, int request, void *arg)
{int r;do r = ioctl (fd, request, arg);while (-1 == r && EINTR == errno);return r;
}

Step 3: Image Format

V4L2 provides an easy interface to check the image formats and colorspace that your webcam supports and provide.v4l2_format sturcture is to be used to change image format.

struct v4l2_format fmt = {0};
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 320;
fmt.fmt.pix.height = 240;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field = V4L2_FIELD_NONE;if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
{perror("Setting Pixel Format");return 1;
}

I have set image width and height to be 320 and 240 respectively. You should check out the format that your camera supports. My Camera supports MJPEG and YUV and hence I have set image format to MJPEG.
Step 4: Request Buffers

A buffer contains data exchanged by application and driver using Streaming I/O methods.v4l2_requestbuffers is used to allocate device buffers. Read more here.

struct v4l2_requestbuffers req = {0};
req.count = 1;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;

if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req))
{
perror(“Requesting Buffer”);
return 1;
}

The ioctl is used to initialize memory mapped(mmap), user pointer based I/O.
Step 5: Query Buffer

After requesting buffer from the device, we need to query the buffer in order to get raw data. Read morehere

struct v4l2_buffer buf = {0};
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = bufferindex;
if(-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
{perror("Querying Buffer");return 1;
}

buffer = mmap (NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);

The mmap() function asks to map length bytes starting at offset in the memory of the device specified by fd into the application address space, preferably at address start. Read morehere
Step 6: Capture Image

After querying the buffer, the only thing left is capturing the frame and saving it in the buffer.

if(-1 == xioctl(fd, VIDIOC_STREAMON, &buf.type))
{perror("Start Capture");return 1;
}fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
struct timeval tv = {0};
tv.tv_sec = 2;
int r = select(fd+1, &fds, NULL, NULL, &tv);
if(-1 == r)
{perror("Waiting for Frame");return 1;
}if(-1 == xioctl(fd, VIDIOC_DQBUF, &buf))
{perror("Retrieving Frame");return 1;
}

Step 7: Store data in OpenCV datatype

I wanted to stored the retrieved data in OpenCV image structure. It took me few hours to figure out the perfect way. So here’s how I did it.

CvMat cvmat = cvMat(480, 640, CV_8UC3, (void*)buffer);
IplImage * img;
img = cvDecodeImage(&cvmat, 1);

So this how I captured frames from my webcam and stored in OpenCV Image data structure.

You can find the complete code here on my GitHub

P.S. Coding period for gsoc has started and I have to start working.

If you have some feedback or questions regarding this post, please add comments. I’d be happy to get some feedback.

Capture images using V4L2 on Linux相关推荐

  1. linux v4l2 pdf,linux V4L2编程

    前言:目前正在忙于ARM平台的Linux应用程序的开发(其实是刚刚起步学习啦).底层的东西不用考虑了,开发板子提供了NAND Bootloader,和Linux 2.6的源码,而且都编译好了.自己编译 ...

  2. linux摄像头V4L2 subdev,linux 摄像头驱动 详解linux 摄像头驱动编写

    想了解详解linux 摄像头驱动编写的相关内容吗,feixiaoxing在本文为您仔细讲解linux 摄像头驱动的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:linux,摄像头驱动,下面 ...

  3. Linux基于v4l2的视频采集(可用)

    Video4linux2(简称V4L2),是Linux中关于视频设备的内核驱动. V4L2较V4L有较大的改动,并已成为2.6的标准接口,函盖video\dvb\FM...,多数驱动都在向V4l2迁移 ...

  4. 关于Linux的视频编程(v4l2编程)

    最近在看TQ2440从摄像头采集图像,然后直接在LCD显示的程序,看到文章有用转过来吧.其实V4L2标准说的很清楚,只是太多了,只好找别人的经验直接拿过来看看,重要的参考文件就是V4L2页面的参考程序 ...

  5. 嵌入式linux环境视频采集知识(V4L2)

    Video for Linux two(Video4Linux2)简称V4L2,是V4L的改进版.V4L2是linux操作系统下用于采集图片.视频和音频数据的API接口,配合适当的视频采集设备和相应的 ...

  6. Linux应用开发【第七章】摄像头V4L2编程应用开发

    文章目录 7 摄像头V4L2编程应用开发 7.1 V4L2简介 7.2 V4L2视频采集原理 7.3 V4L2程序实现流程 7.4 V4L2程序实例 7.4.1 打开设备 7.4.2 查询设备属性 7 ...

  7. 【genius_platform软件平台开发】第五十二讲:Linux系统之V4L2视频驱动详解

    V4L2视频驱动详解 刚建的微信群欢迎加入一起学习.讨论: 1. 简介 1.1 视频输入输出设备(video capture device,video output device) 1.2 VBI设备 ...

  8. Linux的视频编程(V4L2编程)【转】

    本文转载自:http://blog.csdn.net/tommy_wxie/article/details/11472073 一.什么是video4linux Video4linux2(简称V4L2) ...

  9. linux之V4L2摄像头应用流程

    对于v4l2,上次是在调试收音机驱动的时候用过,其他也就只是用i2c配置一些寄存器就可以了.那时只是粗粗的了解了,把收音机当作v4l2的设备后会在/dev目录下生成一个radio的节点.然后就可以操作 ...

最新文章

  1. C语言:随笔6--指针1.2
  2. harbor安装_Harbor镜像仓库搭建
  3. 南阳oj 1的个数
  4. 软件测试作业7:构建弱健壮的等价类测试用例
  5. 魅蓝s6启动android密码_魅蓝s6怎么恢复出厂设置?忘记密码怎么办
  6. SpringCloud创建Eureka Client服务注册
  7. js+css实现验证码框,前端实现6位验证码输入框效果
  8. Mozilla为Firefox添加新的CSRF保护
  9. 转:NAT traversal 的概念
  10. 认识Python基础环境搭建
  11. web service中配置tcp/ip监视器
  12. 金三银四马-sb java面试突击资源
  13. android 通知 可横幅展示的哦
  14. Tekla图纸二次开发课程
  15. qt.modbus: (RTU client) Discarding response with wrong CRC, received: 16448 , calculated CRC: 49303
  16. linux设备驱动读书笔记
  17. 苹果手机很卡怎么解决_iPhone很卡怎么办,教您如何解决iPhone很卡问题!
  18. IRQL-NOT-LESS-OR-EQUAL异常分析
  19. 适合全屏手机的高清壁纸,看这里!
  20. python 培训基础

热门文章

  1. JPA字段长度 Mysql数据库
  2. 无论是cisco还是华三的书上对于子网划分有个问题需要解释
  3. OpenNMS全接触-事件及通知(九)
  4. VS2008水晶报表发布部署总结
  5. Android ImageView的scaleType(图片比例类型)属性与adjustViewBounds(调整视图边界)属性
  6. 外存中的对换区和文件区
  7. mysql update实质,UPDATE注射(mysql+php)的两个模式
  8. 谷歌虚拟服务器申请,【美国podserver.info】免费300M虚拟主机空间申请使用教程
  9. tornado学习笔记day01-高并发性能web框架
  10. c语言指针数组课件,C语言指针与数组教程课件.ppt