Ask Your Question

Revision history [back]

problems with cvCalcOpticalFlowFarneback and intrusion detection

Hi everibody , i tried to realize an "intruder detector" using opencv and the function cvCalcOpticalFlowFarneback. i have to compare two images grabbed with webcam , compare these images with CvCalcOpticalFlowFarneback , and if there is movement , i take a photo , and save it in a file.

I wrote my code and i compiled it without error. But when i tryed to execute it , the prompt command open and close immediately , and program don't executes. I can't understand where is the problem , my libraries are correctly configured (if i try to execute another program that uses opencv libraries , it executes correctly. i hope you can help me to fix this problem. My code is below , thanks in advance and sorry for my english

#include <stdio.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>

IplImage* frame;

void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,double scale, CvScalar color) 
    int x, y;
    for( y = 0; y < cflowmap->rows; y += step)
        for( x = 0; x < cflowmap->cols; x += step)
            CvPoint2D32f fxy = CV_MAT_ELEM(*flow, CvPoint2D32f, y, x);
            cvLine(cflowmap, cvPoint(x,y), cvPoint(cvRound(x+fxy.x), cvRound(y+fxy.y)),
                 color, 1, 8, 0);
            cvCircle(cflowmap, cvPoint(x,y), 2, color, -1, 8, 0);
            if (((fabs(fxy.x)>8)&&fabs(fxy.x<10)&&((fabs(fxy.y)>8&&fabs(fxy.y)<10)))) // controlla movimento astine tra i valori 8 e 10

int main()
    CvCapture* webcam = cvCreateCameraCapture(0); //crea oggetto webcam
    CvMat* previmg = 0, *nextimg = 0, *movimento = 0, *output = 0; //crea matrici
    int firstframe;

    if( !webcam) //crontrollo accesso webcam
        return -1;

        firstframe = nextimg == 0;      // contronto, se è vero vale 1 e lo assegno a firstframe

        frame = cvQueryFrame(webcam); //mette dentro frame l'immagine della webcam

        if(!frame) // controlla se c'è l'immagine

        if(!nextimg) // se esiste immagine successiva crea matrici mettendoci i valori dell'immagine della webcam
            nextimg = cvCreateMat(frame->height, frame->width, CV_8UC1);
            previmg = cvCreateMat(nextimg->rows, nextimg->cols, nextimg->type);
            movimento = cvCreateMat(nextimg->rows, nextimg->cols, CV_32FC2);
            output = cvCreateMat(nextimg->rows, nextimg->cols, CV_8UC3);

        cvCvtColor(frame, nextimg, CV_BGR2GRAY); // converte da colorato a grigio

        if( !firstframe ) // controlla se non è la prima immagine (cioè se hai a disposizione le tre matrici piene)
            cvCalcOpticalFlowFarneback(previmg, nextimg, movimento, 0.5, 3, 15, 3, 5, 1.2, 0); // rileva il movimento
            cvCvtColor(previmg, output, CV_GRAY2BGR); // converte da grigio a colorato
            drawOptFlowMap(movimento, output, 16, 1.5, CV_RGB(0, 255, 0)); // crea i pallini (dentro la funzione rileva il movimento dei punti)
            cvShowImage("movimento", output); //manda in output sulla finestra l'immagine della webcam

        if(cvWaitKey(1)>0) //premere un pulsante per interrompere il programma

        CvMat* temp; //crea matrice temporanea
        CV_SWAP(previmg, nextimg, temp); //scambio immagini, 1 diventa 2, 2 diventa temp, nuova immagine diventa 1
    cvReleaseCapture(&webcam); //rilascia la webcam

    return 0;