日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Capture images using V4L2 on Linux

發(fā)布時間:2023/12/20 linux 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Capture images using V4L2 on Linux 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這文章相當(dāng)好,沒有理由不轉(zhuǎn)載

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.

總結(jié)

以上是生活随笔為你收集整理的Capture images using V4L2 on Linux的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 极品色av影院| 国产成人免费片在线观看 | 国产乱码精品一区二区三区五月婷 | 熊出没之冬日乐翻天免费高清观看 | 欧美黑人精品一区二区 | 国模视频一区二区 | 亚洲精品乱码久久久久久麻豆不卡 | 国产农村老头老太视频 | 国产精品嫩草影院av蜜臀 | 久久精品一区二区免费播放 | 性网爆门事件集合av | 国产又粗又猛又爽又黄的视频一 | 在线观看99 | 成人一二三区 | 国产精品久久久久毛片大屁完整版 | 精品国产96亚洲一区二区三区 | 精品国内自产拍在线观看视频 | 日韩精品成人在线 | 国产精品久久久久久精 | 最新在线观看av | 美女免费黄视频 | 少妇被黑人到高潮喷出白浆 | 绿帽av| 免费日韩成人 | 亚色av| 亚洲国产精品成人无久久精品 | 日韩欧美毛片 | 波多野结衣一级 | 欧美午夜理伦三级在线观看 | 欧美人妻精品一区二区免费看 | av片网站 | 五月婷综合 | 欧美永久精品 | 日本爽爽爽爽爽爽在线观看免 | 字幕网在线 | 日韩av在线免费播放 | 美女在线观看视频 | 性三级视频 | www国产免费 | 亚洲不卡在线观看 | 欧美日韩久久久久 | 欧美特黄 | 麻豆免费在线观看 | 国产欧美专区 | wwwwxxx日本| 成人av社区 | 最新天堂av | 国产又爽又黄免费视频 | 欧美午夜精品久久久久久蜜 | av手机在线免费观看 | 91在线免费视频观看 | 青青草av在线播放 | 欧美二三区 | 美女久久| 亚洲av毛片| 曰韩毛片 | 国产人成免费视频 | 一卡二卡三卡 | 国产视频一级 | 国产传媒视频 | 非洲黑人狂躁日本妞 | 91视频久久久久 | 99re免费视频精品全部 | 国精产品乱码一区一区三区四区 | 狠狠操一区二区 | 国产综合免费视频 | 手机在线精品视频 | 欧美特黄 | 天堂av网手机版 | 肥臀熟女一区二区三区 | 深夜激情网 | 99久久精品免费看国产四区 | 欧美亚洲自拍偷拍 | 美女av免费在线观看 | 久久国产中文字幕 | 你懂的网站在线 | 黄视频在线免费看 | 青青草免费在线视频 | 精品区| 极品销魂美女少妇尤物 | 久久一区二区三区四区 | 白白色视频在线 | 理论片午夜| 国产综合一区二区 | 武林美妇肉伦娇喘呻吟 | 美女网站在线 | 国产精品亚洲一区二区 | 亚洲av片不卡无码久久 | 外国电影免费观看高清完整版 | 国产精品欧美综合 | 天天爽天天插 | 黄色一极毛片 | 一级黄色特级片 | 欧美理论片在线观看 | 中文在线字幕免费观 | 亚洲黄色精品视频 | 国产在线视频91 | 亚洲精品国产精品乱码不卡√香蕉 | 久久疯狂做爰流白浆xx |