标签:spl gen red tin iplimage only width offset really
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.
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.
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.
In Linux, default capture devide is generally /dev/video0
, but if you’re using USB webcams, the index will vary accordingly.
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.
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.
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.
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
After querying the buffer, the only thing left is capturing the frame and saving it in the buffer.
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
标签:spl gen red tin iplimage only width offset really
原文地址:https://www.cnblogs.com/jins-note/p/9534577.html