Annexes
1. Programme en C ++ pour la détection par
différence d'images
#include "highgui.h"
#include "cv.h"
int seuil_diff = 30;
IplImage *prev_img = 0;
IplImage *curr_img = 0;
void MotionDetection(IplImage* img, IplImage* dst, int
diff_threshold)
{
cvCvtColor(img, curr_img, CV_BGR2GRAY);
IplImage* silh = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U,
1);
cvAbsDiff(prev_img, curr_img, silh);
cvThreshold(silh, silh, diff_threshold, 255,
CV_THRESH_BINARY);
cvCopy(curr_img, prev_img);
cvMerge(silh, 0,0,0, dst);
cvReleaseImage(&silh);
}
// programme principal
int main()
{
CvCapture* capture = 0;
IplImage *frame = 0;
capture = cvCreateFileCapture("video.avi");
IplImage *video_image = 0, *motion = 0;
for(;;)
{
frame = cvQueryFrame(capture);
if (!frame)
break;
// Création initiale des tableaux utiles
if (!video_image)
{
65
video_image = cvCreateImage(cvGetSize(frame),8,3);
motion = cvCreateImage(cvGetSize(video_image),8,3);
prev_img = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
curr_img = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
cvZero(prev_img);
}
// Origine de l'image = coin haut gauche
if (frame -> origin == 1)
cvFlip(frame, video_image, 1);
else
cvCopy(frame, video_image, 0);
// Detection de mouvement
MotionDetection(video_image, motion, seuil_diff);
// Affichage des resultats
cvNamedWindow("video_image", 1);
cvShowImage("video_image", frame);
cvNamedWindow("frame",0);
cvShowImage("frame", curr_img);
cvNamedWindow("motion", 0);
cvShowImage("motion", motion);
cvCreateTrackbar("seuil_diff", "motion", &seuil_diff, 60,
0);
int delay = 30;
if (cvWaitKey(delay)>= 0)
break;
}
// Sortie du programme
cvReleaseCapture(&capture);
cvReleaseImage(&video_image);
cvReleaseImage(&motion);
cvReleaseImage(&prev_img);
cvReleaseImage(&curr_img);
}
return 0;
66
2. Programme en C++ pour la détection par
soustraction d'arrière-plan
#include <stdio.h>
#include <stdlib.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
//void MotionDetection(IplImage* img, IplImage* dst, int
diff_threshold);
//IplImage *prev_img=0, *curr_img =0;
IplImage *video_image = 0, *motion = 0, *ref_img=0;
int seuil_diff = 50;
int N =20;
IplImage *buf[20];
int last=0;
void MotionDetection(IplImage* img, IplImage* dst, int
diff_threshold)
{
cvCvtColor(img, buf[last], CV_BGR2GRAY);//image courante int idx1
= last;// indice de l'image courante (à t) int idx2 = (last + 1)% N;
last = idx2; // indice de la prochaine image (à t+1)
IplImage* moyenne = cvCreateImage(cvGetSize(img),32,1); IplImage*
silh = cvCreateImage(cvGetSize(img),8,1); cvZero(moyenne);
int i;
for (i = 0;i<N;i++)
if (i!=idx1) //exclusion de l'image courante
cvAcc (buf[i],moyenne,0);// somme des N-1 images
précédentes cvConvertScale(moyenne, ref_img, 1./(N-1),0);
cvReleaseImage(&moyenne);
cvAbsDiff(buf[idx1], ref_img, silh);
cvThreshold(silh, silh, diff_threshold, 255, CV_THRESH_BINARY);
cvMerge(0, silh,0,0,dst); //mouvement en vert cvReleaseImage(&silh);
}
67
int main()
{
CvCapture* capture = 0;
IplImage* frame = 0;
capture = cvCreateFileCapture("video.avi");
for(;;)
{
frame = cvQueryFrame(capture);
if (!frame)
break;
//creation des tableaux utiles
if (!video_image)
{
video_image = cvCreateImage(cvGetSize(frame), 8, 3);
motion = cvCreateImage(cvGetSize(video_image),8,3);
ref_img = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
int i;
for(i=0;i<N;i++)
{
buf[i]=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
cvZero(buf[i]);
}
}
//origine de l'image = coin haut gauche
if (frame->origin == 1)
cvFlip(frame, video_image, -1);
else
cvCopy(frame, video_image,NULL);
//detection de mouvement
MotionDetection(video_image, motion, seuil_diff);
//affichage des résultats
cvNamedWindow("Vidéo_Source",0);
cvShowImage("Vidéo_Source", video_image);
68
cvNamedWindow("Moyenne",0); cvShowImage("Moyenne", ref_img);
cvNamedWindow("Icourante",1); cvShowImage("Icourante", buf[last]);
cvNamedWindow("motion",0); cvShowImage("motion", motion);
cvCreateTrackbar("seuil_diff", "motion", &seuil_diff, 100,0); int delay =
5;
if (cvWaitKey(delay)>=0)
break;
}
//sortie du programme
cvReleaseCapture(&capture); cvReleaseImage(&video_image);
cvReleaseImage(&motion); cvReleaseImage(&ref_img); int i;
for(i=0;i<N;i++)
cvReleaseImage(&buf[i]); return 0;
}
|