Ask Your Question

Revision history [back]

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 );
}