如何从Linux上的摄像头拍摄静止图像(How to capture still image fro

2019-09-02 00:16发布

我想写为Linux,在那里我拍摄静止图像照片从一个网络摄像头一个C ++ / Qt的程序,进行一些转换到照片(裁剪,缩放等),并将其保存到一个JPEG文件。

但我已经遇到了一些问题。 主要的问题是,非标准UVC(USB视频设备类)的Linux驱动程序目前不支持直接拍摄静态图像: http://www.ideasonboard.org/uvc/ 。

因此,有两种可能的方式来捕获静止图像。 您可以从视频流中的一帧从摄像头,也可以采取单独的照片,像数码便携相机。 在Linux UVC驱动不支持第二种方式,因此第一种方法是唯一的出路。 但问题是,如果你想采取从视频流中的帧,照片的尺寸不能大于视频的视频预览窗口大小大。 所以,如果我要带200万像素的照片,我必须与尺寸1600×1200,这是不是那么舒服(至少,Qt中的视频流的大小取决于videopreview窗口大小)开始的视频流。

我知道,没有针对Linux 2 API,它可以在这个任务是有帮助的视频,但我不知道如何使用它。 目前我正在学习的GStreamer,但我现在不能弄清楚如何什么,我需要使用这些工具来完成。

所以,我会明白任何帮助。 我认为这不是谁知道Linux的,GStreamer的人,V4L2 API,和其他Linux特定的东西出了一道难题。

顺便说,该计划将只与网络摄像头罗技C270高清使用。

请帮我。 我不知道是什么API或框架可以帮助我做到这一点。 可能是你知道的。

Answer 1:

Unfortunately the C4V2 calls in opencv did not work for still image capture with any camera I have tried out of the box using the UVC driver.

To debug the issue I have been playing with trying to accomplish this with c code calling c4v2 directly.

I have been playing with the example code found here. It uses the method of pulling frames from the video stream.

You can compile it with:

gcc -O2 -Wall `pkg-config --cflags --libs libv4l2` filename.c -o filename

I have experimented with 3 logitech cameras. The best of the lot seems to be the Logitech C910. But even it has significant issues.

Here are the problems I have encountered trying to accomplish your same task with this code.

It works pretty much every time with width and height set to 1920x1080.

When I query other possibilities directly from the command line using for example:

v4l2-ctl --list-formats-ext

and I try some of the other "available" smaller sizes it hangs in the select waiting for the camera to release the buffer.

Also when I try to set other sizes directly from the command line using for example:

v4l2-ctl -v height=320 -v width=240 -v pixelformat=YUYV

Then check with

v4l2-ctl -V

I find that it returns the correct pixel format but quite often not the correct size.

Apparently this camera which is listed on the UVC site as being UVC and therefore v4l2 compatible is not up to snuff. I suspect it is just as bad for other cameras. The other two I tried were also listed as compatible on the site but had worse problems.

I did some more testing on the LogitechC910 after I posted this. I thought I would post the results in case it helps someone else out.

I wrote a script to test v4l2 grabber code mentioned above on all the formats the camera claims it supports when it is queried with v4l2 here are the results:

640x480 => Hangs on clearing buffer
160x120 => Works
176x144 => Works
320x176 => Works
320x240 => Works
432x240 => Works
352x288 => Works
544x288 => Works
640x360 =>  Works
752x416 => Hangs on clearing buffer
800x448 => Hangs on clearing buffer
864x480 => Works
960x544 => Works
1024x576 => Works
800x600 => Works
1184x656 => Works
960x720 => Works
1280x720 => Works
1392x768 => Works
1504x832 => Works
1600x896 => Works
1280x960 => Works
1712x960 => Works
1792x1008 => Works
1920x1080 => Works
1600x1200 => Works
2048x1536 => Works
2592x1944 => Hangs on clearing buffer.

It turns out that the default setting of 640x480 doesnt work and that is what trapped me and most others who have posted on message boards.

Since it is grabbing a video frame the first frame it grabs when starting up may have incorrect exposure (often black or close to it). I believe this is because since it is being used as a video camera it adjusts exposure as it goes and doesnt care about the first frames. I believe this also trapped me and other who saw the first frame as black or nearly black and thought it was some kind of error. Later frames have the correct exposure

It turns out that opencv with python wrappers works fine with this camera if you avoid the land mines listed above and ignore all the error messages. The error messages are due to the fact while the camera accepts v4l2 commands it doesnt respond correctly. So if you set the width it actually gets set correctly but it responds with an incorrect width.

To run under opencv with python wrappers you can do the following:

import cv2
import numpy

cap = cv2.VideoCapture(0)  #ignore the errors
cap.set(3, 960)        #Set the width important because the default will timeout
                       #ignore the error or false response
cap.set(4, 544)        #Set the height ignore the errors
r, frame = cap.read()
cv2.imwrite("test.jpg", frame)


Answer 2:

**Download And Install 'mplayer'** 
mplayer -vo png -frames 1 tv://


Answer 3:

mplayer -vo png -frames 1 tv://

作为相机尚未准备好可能会给一个绿屏输出。

mplayer -vo png -frames 2 tv://

你可以尝试增加帧数和选择一些从该相机提供了正确的图像。



Answer 4:

什么这个计划?

#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
    VideoCapture webcam;
    webcam.open(0);
    Mat frame;
    char key;

    while(true)
    {
        webcam >> frame;
        imshow("My Webcam",frame);
        key = waitKey(10);

        if(key=='s')
        break;
    }
    imwrite("webcam_capture.jpg", frame);
    webcam.release();
    return 0;
}

这将捕获通过你的摄像头允许的最大尺寸的图片。 现在,您可以添加效果或调整使用Qt所拍摄的图像。 和OpenCV是非常非常容易使用Qt整合,:)



文章来源: How to capture still image from webcam on linux