Reputation: 61
I am doing a project for video monitoring.
I can't see the conversion from RGB to gray; I get a black window for the gray.
Could you please help me with the problem? (code attached)
Also, how can I get the difference between current frame and previous frame?
Thanks a lot.
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;
}
Upvotes: 1
Views: 27035
Reputation: 7543
You need to convert each frame from color to gray scale. It does not get converted automatically if you do it once in the beginning. So you need to add
cvCvtColor(frame, gray, CV_BGR2GRAY);
into your while-loop after your call to cvQueryFrame
.
Upvotes: 3
Reputation: 1
use one of below opencv
functions based on you need:
subtract(img_current,img_prev, img_diff);
absdiff(img_current, img_prev, img_diff);
Upvotes: 0
Reputation: 93410
There were a few problems with your code.
cvCaptureFromAVI()
;cvQueryFrame()
might fail, you need to check it's return as well;cvCvtColor(frame, gray, CV_RGB2GRAY);
before displaying the gray frame;Isn't it easier to read/understand a code when it looks like this?
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;
}
Upvotes: 1