概述
环境:ubuntu18.04 opencv4.4.0
摄像头:usb摄像头,挺老的摄像头,还是usb2.0的
csi摄像头也支持这种方法
插入摄像头后 ls /dev可以看到 /dev/video0
video后面的数字就是后面需要的设备id
代码段摘自opencv手册,基本使用这段代码就够用了
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
// cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID, apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open cameran";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbedn";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
API选择
在linux下自然选择v4l2,也可以用CAP_ANY让opencv自己选择,在linux下gstreamer也是基于V4L实现的,所以gstreamer和v4l2都可以。
需要注意的是,在编译opencv时就要把WITH_V4L打勾,好在在默认情况下WITH_V4L是勾选的
enum | cv::VideoCaptureAPIs { cv::CAP_ANY = 0, cv::CAP_VFW = 200, cv::CAP_V4L = 200, cv::CAP_V4L2 = CAP_V4L, cv::CAP_FIREWIRE = 300, cv::CAP_FIREWARE = CAP_FIREWIRE, cv::CAP_IEEE1394 = CAP_FIREWIRE, cv::CAP_DC1394 = CAP_FIREWIRE, cv::CAP_CMU1394 = CAP_FIREWIRE, cv::CAP_QT = 500, cv::CAP_UNICAP = 600, cv::CAP_DSHOW = 700, cv::CAP_PVAPI = 800, cv::CAP_OPENNI = 900, cv::CAP_OPENNI_ASUS = 910, cv::CAP_ANDROID = 1000, cv::CAP_XIAPI = 1100, cv::CAP_AVFOUNDATION = 1200, cv::CAP_GIGANETIX = 1300, cv::CAP_MSMF = 1400, cv::CAP_WINRT = 1410, cv::CAP_INTELPERC = 1500, cv::CAP_REALSENSE = 1500, cv::CAP_OPENNI2 = 1600, cv::CAP_OPENNI2_ASUS = 1610, cv::CAP_GPHOTO2 = 1700, cv::CAP_GSTREAMER = 1800, cv::CAP_FFMPEG = 1900, cv::CAP_IMAGES = 2000, cv::CAP_ARAVIS = 2100, cv::CAP_OPENCV_MJPEG = 2200, cv::CAP_INTEL_MFX = 2300, cv::CAP_XINE = 2400 } |
CAP_PROP属性
高分辨率下有时延:在缺省情况下用VideoCapture打开摄像头,小分辨率640x480可以实时显示,但1920x1080时就会有将近1秒多的时延,这是不可接受的的事情
解决:用set接口将buffersize改小,使得高分辨率下也可以实时显示
cap.set(cv::CAP_PROP_BUFFERSIZE,1);
尝试过修改CAP_PROP_FPS,发现效果并没有改变,猜测这不是一个从软件层面限制帧率的接口,而是与硬件通信修改帧率,而我的摄像头是固定帧率,所以这个属性并不生效,手册中许多属性后面都有一句only for those cameras that support
大部分摄像头的属性我都没有通过设定这些属性而成功,我的摄像头是支持VISCA协议的,我是通过这个协议来设定摄像头属性的,尚不清楚哪些摄像头能够支持这些CAP_PROP
常用的已标红
frame_width,frame_height:图像长宽
fourcc:四个字母用于表示图像格式,当不能自主识别时就需要自行设定
buffersize:缓冲区 value 0-10
enum | cv::VideoCaptureProperties { cv::CAP_PROP_POS_MSEC =0, cv::CAP_PROP_POS_FRAMES =1, cv::CAP_PROP_POS_AVI_RATIO =2, cv::CAP_PROP_FRAME_WIDTH =3, cv::CAP_PROP_FRAME_HEIGHT =4, cv::CAP_PROP_FPS =5, cv::CAP_PROP_FOURCC =6, cv::CAP_PROP_FRAME_COUNT =7, cv::CAP_PROP_FORMAT =8, cv::CAP_PROP_MODE =9, cv::CAP_PROP_BRIGHTNESS =10, cv::CAP_PROP_CONTRAST =11, cv::CAP_PROP_SATURATION =12, cv::CAP_PROP_HUE =13, cv::CAP_PROP_GAIN =14, cv::CAP_PROP_EXPOSURE =15, cv::CAP_PROP_CONVERT_RGB =16, cv::CAP_PROP_WHITE_BALANCE_BLUE_U =17, cv::CAP_PROP_RECTIFICATION =18, cv::CAP_PROP_MONOCHROME =19, cv::CAP_PROP_SHARPNESS =20, cv::CAP_PROP_AUTO_EXPOSURE =21, cv::CAP_PROP_GAMMA =22, cv::CAP_PROP_TEMPERATURE =23, cv::CAP_PROP_TRIGGER =24, cv::CAP_PROP_TRIGGER_DELAY =25, cv::CAP_PROP_WHITE_BALANCE_RED_V =26, cv::CAP_PROP_ZOOM =27, cv::CAP_PROP_FOCUS =28, cv::CAP_PROP_GUID =29, cv::CAP_PROP_ISO_SPEED =30, cv::CAP_PROP_BACKLIGHT =32, cv::CAP_PROP_PAN =33, cv::CAP_PROP_TILT =34, cv::CAP_PROP_ROLL =35, cv::CAP_PROP_IRIS =36, cv::CAP_PROP_SETTINGS =37, cv::CAP_PROP_BUFFERSIZE =38, cv::CAP_PROP_AUTOFOCUS =39, cv::CAP_PROP_SAR_NUM =40, cv::CAP_PROP_SAR_DEN =41, cv::CAP_PROP_BACKEND =42, cv::CAP_PROP_CHANNEL =43, cv::CAP_PROP_AUTO_WB =44, cv::CAP_PROP_WB_TEMPERATURE =45, cv::CAP_PROP_CODEC_PIXEL_FORMAT =46, cv::CAP_PROP_BITRATE =47 } |
最后
以上就是繁荣大炮为你收集整理的opencv4(五) VideoCapture获取摄像头图像的全部内容,希望文章能够帮你解决opencv4(五) VideoCapture获取摄像头图像所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复