I am making a small application in C++ (using OpenCV) that records a video and then lets you warp it by rotating your view in x,y,z.
Right now I am just starting to test the rotation by making it rotate a small angle every iteration. Although the display of the video works fine, the writing into a file doesn't. As you can see from the code I am warping each frame using the purely rotational homography and then showing and writing the warped frame. For some reason even though the un-warped video is a few MB big, the warped video is only a few KB and it doesn't play using Ubuntu's standard video player. My best guess is that since I am warping the frame I am affecting the frame size in a way that openCV fails to save correctly. I have no idea on how to fix it though.
Any ideas?
Code:
//I already recorded the video and saved it into a file called videoName with FPS frames/sec.
VideoCapture video(videoName);
if(!video.isOpened()) {
std::cerr << videoName <<" could not be opened\n";
return -1;
}
namedWindow(videoName, CV_WINDOW_AUTOSIZE);
namedWindow("lol",CV_WINDOW_AUTOSIZE);
std::cout << video.get(CV_CAP_PROP_FPS) << std::endl;
float xTheta = 0;
float yTheta = 0;
string oVideoName = "oVideo.avi";
Size ofSize(video.get(CV_CAP_PROP_FRAME_WIDTH),video.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter vwriter2(oVideoName,CV_FOURCC('D','I','V','X'),FPS,ofSize,true);
if (!vwriter2.isOpened())
{
std::cout << "ERROR: Failed to write the video" << std::endl;
return -1;
}
while(true) {
Mat frame;
bool bSuccess = video.read(frame);
if(!bSuccess || waitKey(1000/FPS) >= 0)
{
std::cout << "END\n";
break;
}
yTheta += 0.00002;
Mat Rx = (Mat_<float>(3,3) << 1,0,0,0,cos(xTheta),-sin(xTheta),0,sin(xTheta),cos(xTheta));
Mat Ry = (Mat_<float>(3,3) << cos(yTheta),0,sin(yTheta),0,1,0,-sin(yTheta),0,cos(yTheta));
Mat H = Rx * Ry;
Mat pFrame(frame.size(),frame.type());
warpPerspective(frame,pFrame,H,frame.size());
imshow("lol", pFrame);
vwriter.write(pFrame);
}
return 0;