PageRenderTime 117ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/main.c

https://github.com/cledersonbd/trab1_multimidia
C | 189 lines | 144 code | 34 blank | 11 comment | 23 complexity | 2a4e63387f3e13dc792fbd08f9bdfa41 MD5 | raw file
  1. #include <opencv/cv.h>
  2. #include <opencv/cxcore.h>
  3. #include <opencv/highgui.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7. #include <time.h>
  8. // Numero de quadros ate a camera "calibrar"
  9. #define CATCH 60
  10. #define WINSIZEX 300
  11. #define WINSIZEY 210
  12. #define MOV_LIMIT 5
  13. #define PIP_PERCENT (0.3)
  14. //#define FROMFILE
  15. // A Simple Camera Capture Framework
  16. int main(int argc, char **argv) {
  17. unsigned long int cont=0;
  18. struct timeval tv_a, tv_d;
  19. struct timezone tz;
  20. CvMoments moments;
  21. IplImage *frame,
  22. *fundo,
  23. *frame_dst,
  24. *ts,
  25. *foto,
  26. *fotoTMP,
  27. *pip_frame,
  28. *framec,
  29. *fotoWIN;
  30. CvSize ROI, foto_size;
  31. CvPoint cabeca, margemTL, margemOLD;
  32. CvPoint2D64f razao;
  33. CvRect retangulo, bigr;
  34. CvSeq *seq;
  35. CvMemStorage *storage;
  36. CvCapture* capture;
  37. if(argc == 1)
  38. capture = cvCaptureFromCAM( CV_CAP_ANY );
  39. else
  40. capture = cvCaptureFromAVI( argv[2] );
  41. if( !capture ) {
  42. fprintf( stderr, "ERROR: capture is NULL \n" );
  43. getchar();
  44. return -1;
  45. }
  46. foto = cvLoadImage(argv[1], -1);
  47. fotoTMP = cvLoadImage(argv[1], -1);
  48. foto_size = cvGetSize(foto);
  49. ROI = cvGetSize(cvQueryFrame( capture ));
  50. frame = cvCreateImage(ROI, IPL_DEPTH_8U, 1);
  51. fundo = cvCreateImage(ROI, IPL_DEPTH_8U, 1);
  52. frame_dst = cvCreateImage(ROI, IPL_DEPTH_8U, 1);
  53. ts = cvCreateImage(ROI, IPL_DEPTH_8U, 1);
  54. pip_frame = cvCreateImage(cvSize(foto_size.width * PIP_PERCENT, foto_size.height * PIP_PERCENT), IPL_DEPTH_8U, 3);
  55. framec = cvCreateImage(ROI, IPL_DEPTH_8U, 3);
  56. fotoWIN = cvCreateImage(cvSize(WINSIZEX * 2 , WINSIZEY * 2), IPL_DEPTH_8U, 3);
  57. cvNamedWindow( "FOTO", CV_WINDOW_AUTOSIZE );
  58. cvNamedWindow( "WINDOW", CV_WINDOW_AUTOSIZE );
  59. cvMoveWindow("WINDOW", 700, 0);
  60. // rLargura = (int) foto_size.width/ROI.width ;
  61. // rAltura = (int) foto_size.height/ROI.height ;
  62. razao.x = (double) foto_size.width/ROI.width ;
  63. razao.y = (double) foto_size.height/ROI.height ;
  64. margemOLD.x = - MOV_LIMIT - 1;
  65. margemOLD.y = - MOV_LIMIT - 1;
  66. while( 1 ) {
  67. gettimeofday(&tv_a, &tz);
  68. framec = cvQueryFrame( capture );
  69. cvCvtColor(framec, frame, CV_RGB2GRAY);
  70. if( !frame ) {
  71. fprintf( stderr, "ERROR: frame is null...\n" );
  72. getchar();
  73. break;
  74. }
  75. if ((cont == CATCH)) {
  76. cvCopy(frame, fundo, NULL);
  77. cvSmooth(fundo, fundo, CV_GAUSSIAN, 3, 0, 0, 0);
  78. printf("Peguei o fundo: [%p]\n", fundo);
  79. }
  80. else if ( (cont > CATCH ) && (fundo != frame) ) {
  81. cvSmooth(frame, frame, CV_GAUSSIAN, 3, 0, 0, 0);
  82. cvAbsDiff(frame, fundo, frame_dst);
  83. #ifdef FROMFILE
  84. cvThreshold(frame_dst, ts, 60.0, 255.0, CV_THRESH_BINARY);
  85. #else
  86. cvThreshold(frame_dst, ts, 30.0, 255.0, CV_THRESH_BINARY);
  87. #endif
  88. storage = cvCreateMemStorage(0);
  89. cvClearMemStorage(storage);
  90. cvFindContours(ts, storage, &seq, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
  91. bigr.width = 0;
  92. bigr.height = 0;
  93. for(; seq; seq = seq->h_next) {
  94. retangulo = cvBoundingRect(seq, 0);
  95. if ( (retangulo.width * retangulo.height) > (bigr.width * bigr.height) ) {
  96. bigr.x = retangulo.x;
  97. bigr.y = retangulo.y;
  98. bigr.width = retangulo.width;
  99. bigr.height = retangulo.height;
  100. }
  101. }
  102. cvRectangle(framec, cvPoint(bigr.x, bigr.y), cvPoint(bigr.x + bigr.width, bigr.y + bigr.height), CV_RGB(0,0,255), 2, 8, 0);
  103. cvRectangle(ts, cvPoint(bigr.x, bigr.y), cvPoint(bigr.x + bigr.width, bigr.y + bigr.height), CV_RGB(255,255,255), CV_FILLED, 8, 0);
  104. cabeca.x = bigr.x + bigr.width / 2 ;
  105. cabeca.y = bigr.y + (bigr.height / 8) * 1.5;
  106. cvCircle(framec, cvPoint(cabeca.x , cabeca.y), 25, CV_RGB(255, 0, 0), 2, 8, 0);
  107. cvResize(framec, pip_frame, CV_INTER_AREA);
  108. cvCopy(foto, fotoTMP, NULL);
  109. cvRectangle(fotoTMP, cvPoint( (int)cabeca.x * razao.x - WINSIZEX, (int)cabeca.y * razao.y - WINSIZEY), cvPoint( (int)cabeca.x * razao.x + WINSIZEX, (int)cabeca.y * razao.y + WINSIZEY), CV_RGB(255, 0, 0), 2, 8, 0);
  110. if ( cabeca.x * razao.x < WINSIZEX )
  111. margemTL.x = 0;
  112. else if ( (cabeca.x * razao.x + WINSIZEX) > (foto_size.width - 1) )
  113. margemTL.x = foto_size.width - 1 - WINSIZEX * 2;
  114. else
  115. margemTL.x = (int) cabeca.x * razao.x - WINSIZEX;
  116. if ( cabeca.y * razao.y < WINSIZEY)
  117. margemTL.y = 0;
  118. else if ( (cabeca.y * razao.y + WINSIZEY) > (foto_size.height - 1) )
  119. margemTL.y = foto_size.height - 1 - WINSIZEY * 2;
  120. else
  121. margemTL.y = (int) cabeca.y * razao.y - WINSIZEY;
  122. if ( abs(margemTL.x - margemOLD.x) > MOV_LIMIT || abs(margemTL.y - margemOLD.y) > MOV_LIMIT ) {
  123. cvSetImageROI(foto, cvRect( margemOLD.x + (margemTL.x - margemOLD.x), margemOLD.y + (margemTL.y - margemOLD.y), WINSIZEX * 2 , WINSIZEY * 2));
  124. margemOLD.x = margemTL.x;
  125. }
  126. cvCopy(foto, fotoWIN, NULL);
  127. cvResetImageROI(foto);
  128. cvShowImage("WINDOW", fotoWIN);
  129. }
  130. else
  131. cvCopy(foto, fotoTMP, NULL);
  132. // Criando PIP
  133. cvSetImageROI(fotoTMP, cvRect( 0, 0, foto_size.width * PIP_PERCENT , foto_size.height * PIP_PERCENT));
  134. cvCopy(pip_frame, fotoTMP, NULL);
  135. cvResetImageROI(fotoTMP);
  136. // cvShowImage("CAM", framec);
  137. cvShowImage("FOTO", fotoTMP);
  138. // Do not release the frame!
  139. //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
  140. //remove higher bits using AND operator
  141. if( (cvWaitKey(10) & 255) == 27 )
  142. break;
  143. struct timeval tv;
  144. cont++;
  145. char buffer[30];
  146. gettimeofday(&tv_d, &tz);
  147. tv.tv_sec = tv_d.tv_sec - tv_a.tv_sec;
  148. tv.tv_usec = tv_d.tv_usec - tv_a.tv_usec;
  149. strftime(buffer, 30, "%M:%S", localtime(&(tv.tv_sec)));
  150. printf("Tempo do quadro: %s.%ld\n", buffer, tv.tv_usec);
  151. }
  152. // Release the capture device housekeeping
  153. cvReleaseCapture( &capture );
  154. return 0;
  155. }