使用cv :: videowriter时,视频总是错误的

时间:2017-11-24 08:38:19

标签: c++ opencv ffmpeg

我使用cv :: videowriter编写了测试代码,视频总是错误的(不需要的颜色和形状)。但是当我通过cv :: imwrite保存它们时帧是正确的。系统是centos 7,opencv 2.4.5,ffmpeg 2.6.8 非常感谢![抱歉,我无法发布测试结果,输出aaaaa.jpg是灰色图像,但视频采集是绿色和粉红色的彩色图像]

#include <stdio.h>

#include <unistd.h>
#include "/usr/include/opencv/cv.h"
#include "/usr/include/opencv/highgui.h"

int main(int, char**)
{
    cv::VideoWriter outputVideo;
    outputVideo.open("out.avi", CV_FOURCC('M','J','P','G'), 30,         cv::Size(400,400), true);
    if (!outputVideo.isOpened()){
        printf("aaa\n");
    }
    cv::Mat frame, f1,f2, f3;
    char *Frame_buff = (char *)(malloc(400 * 400 * 3));
    for(int iii = 0; iii < 400; iii++) {
        for(int jjj = 0; jjj < 400; jjj++) {
            Frame_buff[jjj * 400 * 3 + iii * 3 + 0] = iii;
            Frame_buff[jjj * 400 * 3 + iii * 3 + 1] = iii;
            Frame_buff[jjj * 400 * 3 + iii * 3 + 2] = iii;  
        }
    }

    frame = cv::Mat(400, 400, CV_8UC3, Frame_buff);
    f1 = cv::Mat(400, 400, CV_8UC3, Frame_buff);
    cv::imwrite("aaaaa.jpg", f1);

    for(int iii = 0; iii < 400; iii++) {
        for(int jjj = 0; jjj < 400; jjj++) {
            Frame_buff[jjj * 400 * 3 + iii * 3 + 0] = jjj;
            Frame_buff[jjj * 400 * 3 + iii * 3 + 1] = jjj;
            Frame_buff[jjj * 400 * 3 + iii * 3 + 2] = jjj;  
       }
    }
    f2 = cv::Mat(400, 400, CV_8UC3, cv::Scalar(0, 0, 255));
    cv::imwrite("bbbb.jpg", f2);
    f3 = cv::Mat(400, 400, CV_8UC3, Frame_buff);
    cv::imwrite("cccc.jpg", f3);

    for(int i = 0; i < 240; i++){
        if(i < 80){
            printf("ccc");
            outputVideo.write(f1);
        }
        else if(i<160){
            printf("bbb");
            outputVideo.write(frame);
        }
        else{
            printf("aaa");
            outputVideo.write(f2);
        }       
    }
    printf("\n");
    getchar();
    outputVideo.release();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

以下是我的测试结果,我不确定您的问题是什么,但您的说明与我在上面运行您的代码不一致:

enter image description here aaaa.jpg bbbb.jpg

输出视频的运行方式如下:

Shows an image like cccc.jpg for 4.5(ish) seconds, Shows bbbb.jpg for 3.5ish seconds