Je fais un projet de surveillance vidéo.
Je ne vois pas la conversion de RVB en gris; J’ai une fenêtre noire pour le gris.
Pourriez-vous m’aider s’il vous plaît avec le problème? (code attaché)
Aussi, comment puis-je obtenir la différence entre l’image actuelle et l’image précédente?
Merci beaucoup. Ilan
#include "stdafx.h" #include // For printf #include #include #include 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; }
Vous devez convertir chaque image de couleur en échelle de gris. Il ne se convertit pas automatiquement si vous le faites une fois au début. Donc vous devez append
cvCvtColor(frame, gray, CV_BGR2GRAY);
dans votre boucle while après votre appel à cvQueryFrame
.
Il y a eu quelques problèmes avec votre code.
cvCaptureFromAVI()
; cvQueryFrame()
peut échouer, vous devez également vérifier son retour. cvCvtColor(frame, gray, CV_RGB2GRAY);
avant d’afficher le cadre gris; N’est-il pas plus facile de lire / comprendre un code quand il ressemble à ceci?
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: // // 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; }
utilisez l’une des fonctions ci-dessous, en fonction de vos besoins:
subtract(img_current,img_prev, img_diff); absdiff(img_current, img_prev, img_diff);