概述
Backto OpenCV Index
VideoCaputre 是OpenCV 中用来从摄像头或视频文件或图像序列
来 获取/处理视频
的类.
Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences.
Read video file
输入文件路径
VideoCapture (const String &filename)
Read from Camera
输入相机的 device id, 通常内置摄像头是 0, 外接的从 1 开始增加.
VideoCapture (int index)
比如, 如果外接了camera, 就用外接的. 如果没有外接的, 就用内置的. 可以这样写.
VideoCapture cap(1); // try to open the USB camera
if (!cap.isOpened())
{
cap.open(0);// try to open the default camera if USB camera failed
if (!cap.isOpened()) {
cout << "No Camera Found!" << endl;
return;
}
}
// do normal bussiness
A full example: Read video, Capture image, Process image, Display image
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened())
// check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
- 就是不需要释放操作, VideoCapture类的析构函数会自动完成。
相机设置/视频参数: set()函数
virtual bool cv::VideoCapture::set(int
propId, double value )
- propId: Property identifier
- value: Value of the property.
Video files related
- CAP_PROP_POS_MSEC : Current position of the video file in milliseconds.
- CAP_PROP_POS_FRAMES: 0-based index of the frame to be decoded/captured next.
- CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
- CAP_PROP_FRAME_WIDTH: Width of the frames in the video stream.
- CAP_PROP_FRAME_HEIGHT: Height of the frames in the video stream.
- CAP_PROP_FPS: Frame rate.
- CAP_PROP_FOURCC: 4-character code of codec.
- CAP_PROP_FRAME_COUNT: Number of frames in the video file.
- CAP_PROP_FORMAT: Format of the Mat objects returned by retrieve() .
- CAP_PROP_MODE: Backend-specific value indicating the current capture mode.
Camera settings related
- CAP_PROP_BRIGHTNESS: Brightness of the image (only for cameras).
- CAP_PROP_CONTRAST: Contrast of the image (only for cameras).
- CAP_PROP_SATURATION: Saturation of the image (only for cameras).
- CAP_PROP_HUE: Hue of the image (only for cameras).
- CAP_PROP_GAIN: Gain of the image (only for cameras).
- CAP_PROP_EXPOSURE: Exposure (only for cameras).
- CAP_PROP_CONVERT_RGB: Boolean flags indicating whether images should be converted to RGB.
- CAP_PROP_WHITE_BALANCE: Currently unsupported
- CAP_PROP_RECTIFICATION: Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
examples
比如说有些摄像头会自动开启自动对焦功能(AutoFocus), 但是实际场景中我们需要禁掉这个功能. 比如, Logitech 的摄像头, 又没有官方的SDK可以使用. 这时候, 就可以通过 OpenCV中 VideoCaputure 的 set 函数, 对相机参数进行设置.
cv::VideoCapture cap(0);
if (cap.isOpened())
cap.set(cv::CAP_PROP_AUTOFOCUS, 0); // turn off autofocus
但在实际使用中, 发现这个总是失效, 可能涉及到相机底层有关. 可以换另一个方法,
cv::VideoCapture cap(0);
if (cap.isOpened())
cap.set(cv::CAP_PROP_SETTINGS, 1); // open setting panel
这样就会弹出一个完整的相机配置框, 选择想要的参数. 在实际测试中, 这个 100% 成功.
Ref
- VideoCapture Class - official doc: 官方出品, 很全面了
最后
以上就是火星上砖头为你收集整理的【OpenCV】VideoCapture类解析Read video fileRead from CameraA full example: Read video, Capture image, Process image, Display image相机设置/视频参数: set()函数Ref的全部内容,希望文章能够帮你解决【OpenCV】VideoCapture类解析Read video fileRead from CameraA full example: Read video, Capture image, Process image, Display image相机设置/视频参数: set()函数Ref所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复