这里所有的故事开始都和 VideoCapture有关哦。
/** @brief Default constructor
@note In @ref videoio_c "C API", when you finished working with video, release CvCapture structure with
cvReleaseCapture(), or use Ptr\ that calls cvReleaseCapture() automatically in the
destructor.
*/
CV_WRAP VideoCapture();
/** @overload
@brief Opens a video file or a capturing device or an IP video stream for video capturing with API Preference
@param filename it can be:
- name of video file (eg. `video.avi`)
- or image sequence (eg. `img_%02d.jpg`, which will read samples like `img_00.jpg, img_01.jpg, img_02.jpg, ...`)
- or URL of video stream (eg. `protocol://host:port/script_name?script_params|auth`).
Note that each video stream or IP camera feed has its own URL scheme. Please refer to the
documentation of source stream to know the right URL.
@param apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader
implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW.
@sa The list of supported API backends cv::VideoCaptureAPIs
*/
CV_WRAP VideoCapture(const String& filename, int apiPreference = CAP_ANY);
/** @overload
@brief Opens a camera for video capturing
@param index id of the video capturing device to open. To open default camera using default backend just pass 0.
(to backward compatibility usage of camera_id + domain_offset (CAP_*) is valid when apiPreference is CAP_ANY)
@param apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader
implementation if multiple are available: e.g. cv::CAP_DSHOW or cv::CAP_MSMF or cv::CAP_V4L.
@sa The list of supported API backends cv::VideoCaptureAPIs
*/
CV_WRAP VideoCapture(int index, int apiPreference = CAP_ANY);
基本用法
- 我写了一个读视频和打开本地摄像头的方法,至于IP地址方式,自己可以用VLC之类软件推流模拟一下。
- 读视频
/*
* open a video by path
*/
void videoDemo(string path)
{
VideoCapture capture(path.c_str());
if (!capture.isOpened())
{
cout << "can`t opeen this vido" << endl;
}
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
int fps = capture.get(CAP_PROP_FPS);
int countOfFrame = capture.get(CAP_PROP_FRAME_COUNT);
cout << "w:" << width << " height:" << height << " fps:" << fps <<" count: "<
- 打开本地摄像头
void cameraDemo()
{
VideoCapture capture(0);
if (!capture.isOpened())
{
cout << "can`t opeen this camera" << endl;
}
namedWindow("frame", WINDOW_AUTOSIZE);
Mat frame;
while (true)
{
bool ret = capture.read(frame);
if (!ret)
{
break;
}
imshow("frame", frame);
char c = waitKey(23);
if (c == 27)
{
break;
}
}
capture.release();
}
关于视频的一些基本信息
CAP_PROP_POS_MSEC =0, //!< Current position of the video file in milliseconds.
CAP_PROP_POS_FRAMES =1, //!< 0-based index of the frame to be decoded/captured next.
CAP_PROP_POS_AVI_RATIO =2, //!< Relative position of the video file: 0=start of the film, 1=end of the film.
CAP_PROP_FRAME_WIDTH =3, //!< Width of the frames in the video stream.
CAP_PROP_FRAME_HEIGHT =4, //!< Height of the frames in the video stream.
CAP_PROP_FPS =5, //!< Frame rate.
CAP_PROP_FOURCC =6, //!< 4-character code of codec. see VideoWriter::fourcc .
CAP_PROP_FRAME_COUNT =7, //!< Number of frames in the video file.
CAP_PROP_FORMAT =8, //!< Format of the %Mat objects (see Mat::type()) returned by VideoCapture::retrieve().
//!< Set value -1 to fetch undecoded RAW video streams (as Mat 8UC1).
CAP_PROP_MODE =9, //!< Backend-specific value indicating the current capture mode.
CAP_PROP_BRIGHTNESS =10, //!< Brightness of the image (only for those cameras that support).
CAP_PROP_CONTRAST =11, //!< Contrast of the image (only for cameras).
CAP_PROP_SATURATION =12, //!< Saturation of the image (only for cameras).
CAP_PROP_HUE =13, //!< Hue of the image (only for cameras).
CAP_PROP_GAIN =14, //!< Gain of the image (only for those cameras that support).
CAP_PROP_EXPOSURE =15, //!< Exposure (only for those cameras that support).
CAP_PROP_CONVERT_RGB =16, //!< Boolean flags indicating whether images should be converted to RGB.
CAP_PROP_WHITE_BALANCE_BLUE_U =17, //!< Currently unsupported.
CAP_PROP_RECTIFICATION =18, //!< Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently).
这里截取了一些基本的参数。
实际我们常用的视频的长宽、帧率可以这样获取
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
int fps = capture.get(CAP_PROP_FPS);
cout << "w:" << width << " height:" << height << " fps:" << fps << endl;
也可以直接这样
int width = capture.get(3);
int height = capture.get(4);
int fps = capture.get(5);
cout << "w:" << width << " height:" << height << " fps:" << fps << endl;
那么我们现在来写一个实施视频展示和保存吧。
/*
* use the camera to svae real time video stream
*/
void RealTimeDemo(string savePath)
{
VideoCapture capture(0);
if (!capture.isOpened())
{
cout << "can`t opeen this camera" << endl;
}
namedWindow("frame", WINDOW_AUTOSIZE);
double width = capture.get(CAP_PROP_FRAME_WIDTH);
double height = capture.get(CAP_PROP_FRAME_HEIGHT);
double fps = capture.get(CAP_PROP_FPS);
int videoType = capture.get(CAP_PROP_FOURCC);
VideoWriter writer(savePath.c_str(),videoType,fps,Size(width,height),true);
Mat frame;
while (true)
{
bool ret = capture.read(frame);
if (!ret)
{
break;
}
imshow("frame", frame);
writer.write(frame);
char c = waitKey(23);
if (c == 27)
{
break;
}
}
capture.release();
writer.release();
}
本节代码地址:https://github.com/cyssmile/openCV_learning_notes/blob/master/opencv_test/opencv_041/opencv_041.cpp