我正在使用opencv从原始数据编写视频。这个视频工作正常。但是当我尝试从混合图像创建视频(在每个原始图像上添加一个矩形)然后写入视频时,它会给出编解码器错误。 我使用以下代码:
#define drawCross( img, center, color, d ,d1 )\
line(img, Point(center.x - d - 20, center.y ), Point(center.x + d + 20, center.y ), color, 1, CV_AA, 0);\
line(img, Point(center.x , center.y - d), Point(center.x , center.y + d), color, 1, CV_AA, 0 );\
line(img, Point(center.x-d1-20 , center.y-d1 ), Point(center.x-d1-20, center.y - d ), color, 1, CV_AA, 0 );\
line(img, Point(center.x-d1-20 , center.y-d1 ), Point(center.x-d -20, center.y - d1), color, 1, CV_AA, 0 );\
line(img, Point(center.x-d1-20 , center.y+d1 ), Point(center.x-d1-20, center.y + d ), color, 1, CV_AA, 0 );\
line(img, Point(center.x-d1-20 , center.y+d1 ), Point(center.x-d -20, center.y + d1), color, 1, CV_AA, 0 );\
line(img, Point(center.x+d1+20 , center.y - d1 ), Point(center.x+d+20 , center.y - d1), color, 1, CV_AA, 0 );\
line(img, Point(center.x+d1+20 , center.y - d1 ), Point(center.x+d1+20 , center.y - d), color, 1, CV_AA, 0 );\
line(img, Point(center.x+d1+20 , center.y + d1 ), Point(center.x+d1+20 , center.y + d), color, 1, CV_AA, 0 );\
line(img, Point(center.x+d1+20 , center.y + d1 ), Point(center.x+d+20 , center.y + d1), color, 1, CV_AA, 0 );\
int main(int argc, char *argv[])
{
std::stringstream s;
string storedCorrect;
Mat frame;
Mat result = Mat::zeros(frame.size(), frame.type());
int key=0;
CvCapture* capture = cvCaptureFromAVI("zoomRecord.avi");
cvNamedWindow( "My_Win" );
//'X', 'V', 'I', 'D'
VideoWriter outputVideo;
int wwidth = 1392;
int wheight = 798;
outputVideo.open("MyVideo.avi", CV_FOURCC('X','V','I','D'), 25,
cvSize(wwidth,wheight),true);
//outputVideo.open ( "outputVideo.avi", CV_FOURCC('D','I','V','X'), 25, Size(1392,798), true);//'D','I','V','X'
if (!outputVideo.isOpened())
{
cout << "Could not open the output video for write: " << endl;
return -1;
}
cout<<"start"<<endl;
int i=0;
while(key!='q')
{
frame=cvQueryFrame(capture);
Mat mask = Mat::zeros(frame.size(), frame.type());
cout<<"Frame number : "<<i<<endl;
i++;
s <<" Frame Rate : 25 ";
storedCorrect = s.str();
s.str(" ");
//putText (mask,storedCorrect,cvPoint(450,450),FONT_HERSHEY_SIMPLEX, 0.6, cvScalar(150,150,150), 1.5, CV_AA);
drawCross(mask,Point(frame.cols/2,frame.rows/2) , Scalar(120, 120, 120),50,50);
addWeighted(frame,1,mask,-0.3,0,result,1);
cout<<result.cols<<" "<<result.rows<<endl;
outputVideo.write(result);
imshow("My_Win", result);
//cout<<"size : "<<result.size()<<endl;
key=waitKey(2);
}
cout<<"done"<<endl;
}