我使用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;
}