OpenCV RGB - Gray

I am doing a project for video surveillance.
I do not see the transition from RGB to gray; I get a black window for gray.
Could you help me with the problem? (code attached)
Also, how can I get the difference between the current frame and the previous frame?
Many thanks. Ilan

#include "stdafx.h"
#include <stdio.h>  // For printf
#include <cv.h>  
#include <cxcore.h>  
#include <highgui.h>      

int main()  
{  


    int key = 0; 



    CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" );  
    IplImage* frame = cvQueryFrame( capture );
    IplImage* gray = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
    cvCvtColor(frame, gray, CV_RGB2GRAY);

        if ( !capture ) 

    {  
        fprintf( stderr, "Cannot open AVI!\n" );  
        return 1;  
        }
          int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );

           cvNamedWindow( "video", CV_WINDOW_AUTOSIZE ); 
         cvNamedWindow( "grayvideo", CV_WINDOW_AUTOSIZE ); 

        while( key != 'x' )  
    {  
        frame = cvQueryFrame( capture );




    if(key==27 )break;
     cvShowImage( "video",frame );
     cvShowImage( "grayvideo",gray );


           key = cvWaitKey( 1000 / fps );  
    }  
           cvDestroyWindow( "video" );  
    cvReleaseCapture( &capture );  

    return 0;  
}  
+1
source share
3 answers

You need to convert each frame from color to grayscale. It does not automatically convert if you do it once at the beginning. Therefore you need to add

cvCvtColor(frame, gray, CV_BGR2GRAY);

into your while loop after your call cvQueryFrame.

+3
source

There were a few problems with your code.

  • It was a mess and poorly identified;
  • , . : cvCaptureFromAVI();
  • cvQueryFrame() , ;
  • RGB → GREY: cvCvtColor(frame, gray, CV_RGB2GRAY); ;
  • ;

/ , ?

int main()  
{      
    CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" );  
    if ( !capture ) 
    {  
        fprintf( stderr, "Cannot open AVI!\n" );  
        return 1;  
    }

    int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
    printf("FPS: %d\n", fps);

    cvNamedWindow( "video", CV_WINDOW_AUTOSIZE ); 
    cvNamedWindow( "grayvideo", CV_WINDOW_AUTOSIZE ); 

    int key = 0; 
    IplImage* gray = NULL;
    IplImage* prev_frame = NULL;

    while( key != 'x' )  
    {  
        frame = cvQueryFrame( capture );
        if (!frame) 
        {
            // print error and abort the loop
            break;
        }

        cvShowImage( "video", frame );

        if (!gray) // allocate space for the GRAY frame only once
        {
            gray = cvCreateImage(cvGetSize(frame), frame->depth,1);
        }
        cvCvtColor(frame, gray, CV_RGB2GRAY); // convert RGB frame to GRAY
        cvShowImage( "grayvideo", gray );

        if (!prev_frame) // allocate space for the GRAY frame only once
        {
            prev_frame = cvCreateImage(cvGetSize(frame), frame->depth,1);
            cvCopy( frame, prev_frame, 0 );
        }

        // perform process to compute the "difference" of the current 
        // and previous frames:
        // <add your code here>

        // then, update prev_frame now so in the next iteration it holds the previous frame
        cvCopy( frame, prev_frame, 0 );

        key = cvWaitKey( 1000 / fps );  
        if ( key==27 ) // ESC was pressed
            break;
    }  

    cvDestroyWindow( "video" );  
    cvDestroyWindow( "grayvideo" );  

    cvReleaseCapture( &capture );  

    if (gray)
        cvReleaseImage( &gray );

    if (prev_frame)
        cvReleaseImage( &prev_frame );

    return 0;  
}  
+1

use one of the below opencvfunctions that you need:

subtract(img_current,img_prev, img_diff);
absdiff(img_current, img_prev, img_diff);
0
source

All Articles