OpenCV进行视频读写
发布日期:2021-05-14 15:16:29 浏览次数:29 分类:精选文章

本文共 4477 字,大约阅读时间需要 14 分钟。

OpenCV������������������������������������������

������������������������������������������OpenCV���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������OpenCV������������������������������������������������������������������������������������������������������������


1. ������������������

OpenCV ��������� VideoCapture ���������������������������������������������������������������������

  • ���������������������������������������������������������������
#include 
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
VideoCapture capture(0);
if (!capture.isOpened()) {
printf("could not open the camera...\n");
return -1;
}
namedWindow("frame", WINDOW_AUTOSIZE);
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret) break;
imshow("frame", frame);
char c = waitKey(50);
if (c == 27) // ESC
break;
}
return 0;
}
  • ���������������������������������������������������������������������������������
#include 
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
VideoCapture capture("C:/path/to/test.mp4");
if (!capture.isOpened()) {
printf("could not open the video file...\n");
return -1;
}
namedWindow("frame", WINDOW_AUTOSIZE);
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret) break;
imshow("frame", frame);
char c = waitKey(50);
if (c == 27)
break;
}
return 0;
}
  • ������������������������������������������ URL ������������������������������
#include 
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
VideoCapture capture("http://example.com/video.mp4");
if (!capture.isOpened()) {
printf("could not open the video...\n");
return -1;
}
namedWindow("frame", WINDOW_AUTOSIZE);
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret) break;
imshow("frame", frame);
char c = waitKey(50);
if (c == 27)
break;
}
return 0;
}

2. ������������������

���������������������������������������������������������������OpenCV ������ VideoWriter ������������������������������

#include 
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
VideoCapture capture("C:/path/to/input.mp4");
if (!capture.isOpened()) {
printf("could not open the video file...\n");
return -1;
}
namedWindow("frame", WINDOW_AUTOSIZE);
Mat frame;
VideoWriter writer("C:/path/to/output.mp4",
capture.get(CAP_PROP_FOURCC),
capture.get(CAP_PROP_FPS),
Size(capture.get(CAP_PROP_FRAME_WIDTH),
capture.get(CAP_PROP_FRAME_HEIGHT)),
true);
while (true) {
bool ret = capture.read(frame);
if (!ret) break;
imshow("frame", frame);
writer.write(frame);
char c = waitKey(50);
if (c == 27)
break;
}
capture.release();
writer.release();
return 0;
}

3. ������������������

���������������������������������������������������������������������������������������

  • CAP_PROP_FRAME_RATE���������(Hz)
  • CAP_PROP_FRAME_SIZE������ x ���������
  • CAP_PROP_FRAME_COUNT������������������
  • CAP_PROP_FOURCC���������������������������FourCC���

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


������������

  • ������������������������������������������������������������������������������������������������ 2GB ���������

  • ������������������������������������������������������ FourCC ��������� YUV420p���������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������


  • ��������������������������������� OpenCV ������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:OpenCV的一些传统实例
    下一篇:OpenCV获取图像直方图

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年04月19日 08时58分06秒