Ask Your Question

Revision history [back]

Problem inverting video

Hi everybody,

I'm trying to invert a video that I'm reading from .avi file. This is my code:

#include "stdafx.h" 
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

cv::Mat aFrameMatrix;    /**< Original Frame Matrix */
cv::Mat anOutputMatrix;  /**< Output Frame Matrix */

String anInputPath;      /**< Input video path */
String anOutputPath;     /**< Output video path */   
bool anExit = true;      /**< Exit application */

vector<cv::Mat>   matCollection;

int main( )
{ 
    std::cout << "  Invert Video:  " << std::endl;
    std::cout << "------------------------" << std::endl;
    std::cout << "* Enter the input video path : "; std::cin >> anInputPath;
    std::cout << "* Enter the output video path : "; std::cin >> anOutputPath;
    std::cout << "\n";  

    cv::VideoCapture anInputCapture( anInputPath );

    if ( !anInputCapture.isOpened( ) )
    {
       std::cout << "!!! Input video could not be opened" << std::endl;
       return -1;
    }   

    cv::VideoWriter anOutputCapture( anOutputPath, 
                                     ( int ) anInputCapture.get( CV_CAP_PROP_FOURCC ),
                                     ( int ) anInputCapture.get( CV_CAP_PROP_FPS ),
                                     cv::Size( ( int ) anInputCapture.get( CV_CAP_PROP_FRAME_WIDTH ),
                                     ( int ) anInputCapture.get( CV_CAP_PROP_FRAME_HEIGHT ) ) );

    if ( !anOutputCapture.isOpened( ) )
    {
        std::cout << "!!! Output video could not be opened" << std::endl;
        return -1;
    }

    namedWindow( "INPUT VIDEO: " + anInputPath, WINDOW_AUTOSIZE );
    namedWindow( "OUTPUT VIDEO: " + anOutputPath, WINDOW_AUTOSIZE );   

    while( anExit )
    {
       if ( !anInputCapture.read( aFrameMatrix ) ) 
       {
          anExit = false;
       }
       else
       {
          matCollection.push_back( aFrameMatrix );

          imshow( "INPUT VIDEO: " + anInputPath, aFrameMatrix );               
       }

       if( waitKey( 30 ) >= 0 ) 
       {
          return -1;
       }
    }

    while( !matCollection.empty( ) )
    {       
        imshow( "OUTPUT VIDEO: " + anOutputPath, matCollection.back( ) );      

        anOutputCapture.write( matCollection.back( ) );

        matCollection.pop_back( );

        if( waitKey( 30 ) >= 0 ) 
        {
           return -1;
        }
    }

    anInputCapture.release( );
    anOutputCapture.release( );

return 0;
}

The input video lenght (input_video.avi) is 16 seconds. In "INPUT VIDEO" window I can see the input video correctly, but the "OUTPUT VIDEO" window only shows last frame of "INPUT VIDEO" during 16 seconds. At the end, I get an output_video.avi with the same duration that original video but It only shows the last frame that I saw in "OUTPUT VIDEO" window.

What is wrong?

Thanks,