OpenCV 4 (C++) 解决 “error: ‘CV_FOURCC’ was not declared

在 OpenCV 3 中我们使用 CV_FOURCC 来标识 codec1,例如:

writer.open("image.jpg", CV_FOURCC('M', 'J', 'P', 'G'), fps, size);

在这行代码中,我们给 cv::VideoWriter writer 的存储目标文件命名为 image.jpg,采用 MJPG 的 codec(此处MJPG 是 motion jpeg 的缩写),并输入相应的每秒帧数和视屏镜头的尺寸。

遗憾的是,在 OpenCV 4 (4.5.4-dev) 中, CV_FOURCC 已经被 VideoWriter 的函数 fourcc 所取代2。如果我们继续采用宏 CV_FOURCC 的话,编译时会报错:

error: ‘CV_FOURCC’ was not declared in this scope

下面我们给出 OpenCV4 中 fourcc 的一个简单用法:

writer.open("image.jpg", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, size);

为了更好的展示 fourcc 的标准用法,以及如何在 OpenCV 4 中存储视频,下面我们给出有关如何存储视频的一个完整示例3

1 #include <opencv2/core.hpp>
 2 #include <opencv2/videoio.hpp>
 3 #include <opencv2/highgui.hpp>
 4 #include <iostream>
 5 #include <stdio.h>
 6 using namespace cv;
 7 using namespace std;
 8 int main(int, char**)
 9 {
10     Mat src;
11     // use default camera as video source
12     VideoCapture cap(0);
13     // check if we succeeded
14     if (!cap.isOpened()) {
15         cerr << "ERROR! Unable to open camera\n";
16         return -1;
17     }
18     // get one frame from camera to know frame size and type
19     cap >> src;
20     // check if we succeeded
21     if (src.empty()) {
22         cerr << "ERROR! blank frame grabbed\n";
23         return -1;
24     }
25     bool isColor = (src.type() == CV_8UC3);
26     //--- INITIALIZE VIDEOWRITER
27     VideoWriter writer;
28     int codec = VideoWriter::fourcc('M', 'J', 'P', 'G');  // select desired codec (must be available at runtime)
29     double fps = 25.0;                          // framerate of the created video stream
30     string filename = "./live.avi";             // name of the output video file
31     writer.open(filename, codec, fps, src.size(), isColor);
32     // check if we succeeded
33     if (!writer.isOpened()) {
34         cerr << "Could not open the output video file for write\n";
35         return -1;
36     }
37     //--- GRAB AND WRITE LOOP
38     cout << "Writing videofile: " << filename << endl
39          << "Press any key to terminate" << endl;
40     for (;;)
41     {
42         // check if we succeeded
43         if (!cap.read(src)) {
44             cerr << "ERROR! blank frame grabbed\n";
45             break;
46         }
47         // encode the frame into the videofile stream
48         writer.write(src);
49         // show live and wait for a key with timeout long enough to show images
50         imshow("Live", src);
51         if (waitKey(5) >= 0)
52             break;
53     }
54     // the videofile will be closed and released automatically in VideoWriter destructor
55     return 0;
56 }

出处:

  1. Learning OpenCV 3. Example 02-11. Adrian Kaehler, Gary Bradski.

  2. https://docs.opencv.org/4.x/dd/d9e/classcv_1_1VideoWriter.html#afec93f94dc6c0b3e28f4dd153bc5a7f0

  3. https://docs.opencv.org/4.x/df/d94/samples_2cpp_2videowriter_basic_8cpp-example.html#a5
    原文链接: https://www.cnblogs.com/moyuzhe/p/15708774.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/215886

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午2:59
下一篇 2023年2月13日 上午3:00

相关推荐