Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * webcam.c
- *
- * Created on: 15/feb/2013
- * Author: gian_marco
- */
- #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) //crea pallini e asptine che si muovono
- {
- 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 webcam se c'è
- return 1;
- while(1) //sempre vero, non smette mai
- {
- 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) // se premi un pulsante si interrompe 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement