Ask Your Question
0

problems with cvCalcOpticalFlowFarneback and intrusion detection

asked 2014-01-27 09:03:43 -0600

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
               cvSaveImage("foto.jpg",frame,0);
        }
}


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;


    while(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
            break;


        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
            break;

        {
        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;
}
edit retag flag offensive close merge delete

Comments

I am new to OpenCV and do not know more complicated code that OpenCV has.

Could you put few of these in your code to see where it cwap out at:

printf("made it to here 1 to 10\n");

Good hunting

keghn
keghn gravatar imagekeghn ( 2014-01-27 13:18:57 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2015-11-03 14:57:18 -0600

This code more or less works:

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

void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,double scale, CvScalar color, IplImage* bgr_frame ) 
{
    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);
                    double combo = fabs( (1+fxy.x) * (1+fxy.y));
                    printf("combo is %f ====== x is %f, y is %f\n",combo,fxy.x, fxy.y);
                    if(combo > 1000){
                            printf("You moved!");
                            cvSaveImage("foto.jpg",bgr_frame,0);
                            exit(0);
                    }
            }
}

int main( int argc, char** argv ) {
    printf("Running... Press ESC key to exit\n");
    CvCapture* capture;
    CvMat* previmg = 0, *nextimg = 0, *movimento = 0, *output = 0; 

    capture = cvCreateCameraCapture(0);
    if( capture == NULL ){
            fprintf(stderr, "Unable to connect to webcam!");
            return 1;
    }


    IplImage* bgr_frame = cvQueryFrame(capture);

    nextimg = cvCreateMat(bgr_frame->height, bgr_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(bgr_frame, previmg, CV_BGR2GRAY);
    cvCvtColor(bgr_frame, nextimg, CV_BGR2GRAY);

    CvSize frame_size = cvSize(
                    (int)cvGetCaptureProperty( capture,
                            CV_CAP_PROP_FRAME_WIDTH),
                    (int)cvGetCaptureProperty( capture,
                            CV_CAP_PROP_FRAME_HEIGHT)
                    );


    while(  (bgr_frame = cvQueryFrame( capture )) != NULL ) {
            cvCvtColor(bgr_frame, nextimg, CV_BGR2GRAY);
            printf("previmg address is %p, nextimg address is %p\n", previmg, nextimg);
            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), bgr_frame); // crea i pallini (dentro la funzione rileva il movimento dei punti)
            cvShowImage("movimento", output); //manda in output sulla finestra l'immagine della webcam
            char c = cvWaitKey( 33 );

            //escape key will exit
            if( c == 27 ) break;
    }
    cvReleaseImage(&bgr_frame);
    output=Mat();
    cvReleaseCapture( &capture );
    return( 0 );
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-01-27 09:03:43 -0600

Seen: 749 times

Last updated: Jan 27 '14