Advertisement
giammo

OpenCV.c

Nov 23rd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. /*
  2.  * webcam.c
  3.  *
  4.  *  Created on: 15/feb/2013
  5.  *      Author: gian_marco
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <opencv/highgui.h>
  11. #include <opencv/cv.h>
  12.  
  13. IplImage* frame;
  14.  
  15. void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,double scale, CvScalar color) //crea pallini e asptine che si muovono
  16. {
  17.     int x, y;
  18.     for( y = 0; y < cflowmap->rows; y += step)
  19.         for( x = 0; x < cflowmap->cols; x += step)
  20.         {
  21.             CvPoint2D32f fxy = CV_MAT_ELEM(*flow, CvPoint2D32f, y, x);
  22.             cvLine(cflowmap, cvPoint(x,y), cvPoint(cvRound(x+fxy.x), cvRound(y+fxy.y)),
  23.                  color, 1, 8, 0);
  24.             cvCircle(cflowmap, cvPoint(x,y), 2, color, -1, 8, 0);
  25.             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
  26.                cvSaveImage("foto.jpg",frame,0);
  27.         }
  28. }
  29.  
  30.  
  31. int main()
  32. {
  33.     CvCapture* webcam = cvCreateCameraCapture(0); //crea oggetto webcam
  34.     CvMat* previmg = 0, *nextimg = 0, *movimento = 0, *output = 0; //crea matrici
  35.     int firstframe;
  36.  
  37.     if( !webcam) //crontrollo webcam se c'è
  38.         return 1;
  39.  
  40.  
  41.     while(1) //sempre vero, non smette mai
  42.     {
  43.         firstframe = nextimg == 0;  // contronto, se è vero vale 1 e lo assegno a firstframe
  44.  
  45.         frame = cvQueryFrame(webcam); //mette dentro frame l'immagine della webcam
  46.        
  47.     if(!frame) // controlla se c'è l'immagine
  48.             break;
  49.  
  50.  
  51.         if(!nextimg) // se esiste immagine successiva crea matrici mettendoci i valori dell'immagine della webcam
  52.         {
  53.             nextimg = cvCreateMat(frame->height, frame->width, CV_8UC1);
  54.             previmg = cvCreateMat(nextimg->rows, nextimg->cols, nextimg->type);
  55.             movimento = cvCreateMat(nextimg->rows, nextimg->cols, CV_32FC2);
  56.             output = cvCreateMat(nextimg->rows, nextimg->cols, CV_8UC3);
  57.         }
  58.  
  59.         cvCvtColor(frame, nextimg, CV_BGR2GRAY); // converte da colorato a grigio
  60.  
  61.         if( !firstframe ) // controlla se non è la prima immagine (cioè se hai a disposizione le tre matrici piene)
  62.         {
  63.             cvCalcOpticalFlowFarneback(previmg, nextimg, movimento, 0.5, 3, 15, 3, 5, 1.2, 0); // rileva il movimento
  64.             cvCvtColor(previmg, output, CV_GRAY2BGR); // converte da grigio a colorato
  65.             drawOptFlowMap(movimento, output, 16, 1.5, CV_RGB(0, 255, 0)); // crea i pallini (dentro la funzione rileva il movimento dei punti)
  66.             cvShowImage("movimento", output); //manda in output sulla finestra l'immagine della webcam
  67.         }
  68.  
  69.  
  70.         if(cvWaitKey(1)>0) // se premi un pulsante si interrompe il programma
  71.             break;
  72.            
  73.         {
  74.         CvMat* temp; //crea matrice temporanea
  75.         CV_SWAP(previmg, nextimg, temp); //scambio immagini, 1 diventa 2, 2 diventa temp, nuova immagine diventa 1
  76.         }
  77.     }
  78.     cvReleaseCapture(&webcam); //rilascia la webcam
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement