Ask Your Question
0

Problem inverting video

asked 2013-11-28 06:26:58 -0600

josealberto.fuentes gravatar image

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,

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-11-28 06:57:10 -0600

Haris gravatar image

You may need to copy or clone your aFrameMatrix to another Mat then push to your vector,

like

 Mat tmp=aFrameMatrix.clone();
 matCollection.push_back( tmp );

In the above code your passing the same pointer allocated for aFrameMatrix to your vector every time, so you will get array of Mat pointing single(same) memory location in your vector.

edit flag offensive delete link more

Comments

Thank you very much!! It works perfectly.

josealberto.fuentes gravatar imagejosealberto.fuentes ( 2013-11-28 07:02:17 -0600 )edit

Question Tools

Stats

Asked: 2013-11-28 06:26:58 -0600

Seen: 227 times

Last updated: Nov 28 '13