Open images from USB camera on linux using V4L2 with OpenCV
Posted Jerry_Jin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章
关于protobuf报错:If this call came from a _pb2.py file, your generated code is out of date and must be r