Open images from USB camera on linux using V4L2 with OpenCV
I have always been using OpenCV’s VideoCapture
API to capture images from webcam or USB cameras. OpenCV supports V4L2
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 using V4L2 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. Hereyou 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 found this 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.
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 use v4l2_capability
structure and VIDIOC_QUERYCAP
to query the capture. Read More here.
Here xioctl
is a wrapper function over ioctl
. ioctl() is a function to manipulate device parameters of special files. Read more here.
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.
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.
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 more here
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 more here
Step 6: Capture Image
After querying the buffer, the only thing left is capturing the frame and saving it in the buffer.
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.
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.
Open images from USB camera on linux using V4L2 with OpenCV的更多相关文章
- Jetson TX1使用usb camera采集图像 (2)
该方法只启动usb摄像头 import cv2 import numpy import matplotlib.pyplot as plot class Camera: cap = cv2.VideoC ...
- android4.0 USB Camera实例(三)UVC
前面我写了两篇文章说明了zc301的实现 详细请看 http://blog.csdn.net/hclydao/article/details/21235919 以下顺便把通用的USB也写上 前面的ZC ...
- Android USB Camera(1) : 调试记录【转】
转自:http://blog.csdn.net/eternity9255/article/details/53069037 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 前言 ...
- I.MX6 USB Camera
/************************************************************************* * I.MX6 USB Camera * 说明: ...
- Android开发之《USB Camera》
SimpleWebCam Source Code:https://bitbucket.org/neuralassembly/simplewebcam/src 1. USB摄像头UVC兼容(如今大部分兼 ...
- ROS_Kinetic_12 ROS程序基础Eclipse_C++(三)usb camera
ROS_Kinetic_12 ROS程序基础Eclipse_C++(三)usb camera 软件包下载地址:https://github.com/bosch-ros-pkg/usb_cam 下载后, ...
- Power OFF and ON USB device in linux (ubuntu)
Power OFF and ON USB device in linux (ubuntu) http://loginroot.com/power-off-and-on-usb-device-in-li ...
- Ffmpeg 获取USB Camera 视频流
本文讲述的案例是如何通过Ffmpeg实现从USB Camera中获取视频流并将视频流保存到MP4文件. 本文亦适用于从USB Camera 获取视频流并将视频流转发到rtmp服务的案例,二者基本的原理 ...
- android4.0 USB Camera示例(四)CMOS
上一页下一页说usb camera uvc标准 顺便说说CMOS一起做 操作基本一至, 前HAL在那里我已经提供了层CMOS相关接口 JNIEXPORT jint JNICALL Java_com_d ...
随机推荐
- 错误及异常处理-[PathTooLongException]指定的路径或文件名太长
错误信息 System.IO.PathTooLongException:"指定的路径或文件名太长,或者两者都太长.完全限定文件名必须少于 260 个字符,并且目录名必须少于 248 个字符. ...
- poi 导出工具类
工具类 package com.banxue.kmsservice.helper; import net.sf.json.JSONArray; import net.sf.json.JSONObjec ...
- mysql编码不统一形成的错误
错误提示:[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) ...
- 08-oracle统计函数(单组分组函数)
--count时尽量count(列名),count(*)也可以. --count,max,min,sum,avg,median(中位数) select count(empno),count(disti ...
- Javascript面向对象编程(转)
http://blog.csdn.net/lmj623565791/article/details/29210679 其实,从这个面向对象编程的例子来看,思路还是很清晰的. 第一步: 构造函数,用于初 ...
- FocusBI:SSAS体系结构(原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. SSAS是微软BI组件系列中最核心的组件,商业智能的心脏所有的数据都从这里统一输出,它能把 ...
- 快速上手:在CVM上安装Apache
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由一步 发表于云+社区专栏 介绍 Apache HTTP服务器是世界上使用最广泛的Web服务器.它提供了许多强大的功能,包括可动态加载的 ...
- Windows加密API的功能分类
本地数据加密保护本地数据加密保护机制提供了简单的DAPI调用接口,密钥管理等等一概由系统来处理.DAPI的数据加密保护机制在用户登录会话范围或者本地计算范围,使用操作系统设计的方式加密保护数据和解密还 ...
- Iterator和ListIterator的区别 ---面试题
Iterator和ListIterator的区别是什么? 下面列出了他们的区别: Iterator可用来遍历Set和List集合,但是ListIterator只能用来遍历List. Iterator对 ...
- android socket 通讯(客户端) 发送数据
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...