/trunk/Graber.cpp

https://gitlab.com/BGCX067/eyemouse-svn-to-git · C++ · 258 lines · 168 code · 51 blank · 39 comment · 18 complexity · bf70e6779745485877861a3d7635bd15 MD5 · raw file

  1. ///////////////////////////////////////////////////////////
  2. // CGraber.cpp
  3. // Implementation of the Class CGraber
  4. // Created on: 10-lut-2009 15:39:54
  5. ///////////////////////////////////////////////////////////
  6. #include "Graber.h"
  7. #include <string>
  8. using namespace std;
  9. #include "Utilitis.h"
  10. #include "cv.h"
  11. #include "highgui.h"
  12. #define USE_ORGINAL_FRAME_SIZE 0
  13. //============================================================================
  14. CGraber g_Graber;
  15. CGraber::CGraber()
  16. {
  17. m_pFrameCopy =NULL;
  18. m_ImageSourceType = GS_CAM;
  19. m_pCapture = 0;
  20. m_szFileName = "";
  21. m_iWith = 320;
  22. m_iHeight = 240;
  23. m_iWith = 640;
  24. m_iHeight = 480;
  25. m_pGrayFrame = NULL;
  26. m_bReziseFrame = false;
  27. }
  28. //============================================================================
  29. CGraber::~CGraber()
  30. {
  31. cvReleaseCapture( &m_pCapture );
  32. }
  33. //===========================================================================
  34. void CGraber::Init()
  35. {
  36. // m_pGrayFrame = cvCreateImage(cvSize(m_iWith,m_iHeight), 8,1);
  37. switch(m_ImageSourceType)
  38. {
  39. case GS_CAM :
  40. {
  41. cout<<"source : camera"<<endl;
  42. m_pCapture = cvCaptureFromCAM( 0);
  43. cvSetCaptureProperty(m_pCapture,CV_CAP_PROP_FRAME_WIDTH,m_iWith) ;
  44. cvSetCaptureProperty(m_pCapture,CV_CAP_PROP_FRAME_HEIGHT,m_iHeight) ;
  45. cvSetCaptureProperty(m_pCapture,CV_CAP_PROP_FPS,30) ;
  46. if( !m_pCapture )
  47. throw "camera inicalization fail!";
  48. break;
  49. }
  50. case GS_AVI:
  51. {
  52. cout<<"source file:"<< m_szFileName<<endl;
  53. // m_pCapture = cvCaptureFromAVI( m_szFileName.c_str() );
  54. m_pCapture = cvCaptureFromAVI( m_szFileName);
  55. if( !m_pCapture )
  56. throw "loading avi file fail!";
  57. break;
  58. }
  59. case GS_IMG:
  60. {
  61. cout<<"source file:"<< m_szFileName<<endl;
  62. m_pFrame = cvLoadImage( m_szFileName, 1 );
  63. if( !m_pFrame )
  64. throw "loading image file fail!";
  65. break;
  66. }
  67. }
  68. getNextFrame(&m_pFrame); // get first frmae, nesserre for proper bufer size inicialization
  69. double fps = cvGetCaptureProperty( m_pCapture, CV_CAP_PROP_FPS);
  70. cout<<"FPS:"<<(int)fps<<endl;
  71. }
  72. //============================================================================
  73. void CGraber::UpdateBuffers()
  74. {
  75. m_bFrameEqualizeHistComputed = false;
  76. m_bFrameCopyComputed = false;
  77. if (m_pGrayFrame ==NULL)
  78. {
  79. if (USE_ORGINAL_FRAME_SIZE)
  80. {
  81. m_iWith = m_pOrginalFrame->width;
  82. (m_iHeight = m_pOrginalFrame->height);
  83. };
  84. if ((m_iWith == m_pOrginalFrame->width) && (m_iHeight == m_pOrginalFrame->height) )
  85. {
  86. m_bReziseFrame = false;
  87. }
  88. else
  89. {
  90. m_bReziseFrame = true;
  91. m_pFrame = cvCreateImage(cvSize(m_iWith,m_iHeight), 8,3);
  92. }
  93. //m_iWith = m_pFrame->width;
  94. //m_iHeight = m_pFrame->height;
  95. m_pGrayFrame = cvCreateImage(cvSize(m_iWith,m_iHeight), 8,1);
  96. m_pGrayFrameEqualizeHist = cvCreateImage(cvSize(m_iWith,m_iHeight), 8,1);
  97. m_pPrevGrayFrame = cvCreateImage(cvSize(m_iWith,m_iHeight), 8,1);
  98. };
  99. if (m_bReziseFrame)
  100. cvResize( m_pOrginalFrame, m_pFrame, CV_INTER_LINEAR );
  101. else
  102. m_pFrame = m_pOrginalFrame;
  103. if (m_pFrameCopy ==NULL)
  104. {
  105. m_pFrameCopy = cvCreateImage(cvSize(m_iWith,m_iHeight), m_pFrame->depth,m_pFrame->nChannels);
  106. m_pTmpDebug = cvCreateImage(cvSize(m_iWith,m_iHeight),8, 1);
  107. m_pTmpDebug2 = cvCreateImage(cvSize(m_iWith,m_iHeight),8, 1);
  108. };
  109. cvResetImageROI(m_pFrame);
  110. cvResetImageROI(m_pGrayFrame);
  111. cvResetImageROI(m_pGrayFrameEqualizeHist);
  112. cvResetImageROI(m_pFrameCopy);
  113. IplImage *tmp;
  114. tmp = m_pPrevGrayFrame;
  115. m_pPrevGrayFrame = m_pGrayFrame;
  116. m_pGrayFrame = tmp;
  117. //cvCopyImage(m_pFrame,m_pFrameCopy);
  118. cvCvtColor( m_pFrame, m_pGrayFrame, CV_BGR2GRAY );
  119. //cvEqualizeHist( m_pGrayFrame, m_pGrayFrameEqualizeHist );
  120. //-------------------------debug------------------------------------
  121. //cvSub( m_pPrevGrayFrame,m_pGrayFrame,m_pTmpDebug );
  122. //cvNormalize(m_pTmpDebug,m_pTmpDebug,250,0,CV_MINMAX);
  123. //g_GUI.ShowImage(m_pTmpDebug,"dif");
  124. //cvSmooth(m_pPrevGrayFrame, m_pTmpDebug, CV_BILATERAL,5);
  125. ////cvNormalize(m_pTmpDebug2,m_pTmpDebug2,250,0,CV_MINMAX);
  126. //g_GUI.ShowImage(m_pTmpDebug,"smoof dif");
  127. // cvSub( m_pTmpDebug,m_pTmpDebug2,m_pTmpDebug );
  128. //cvNormalize(m_pTmpDebug,m_pTmpDebug,250,0,CV_MINMAX);
  129. // g_GUI.ShowImage(m_pTmpDebug,"dif difu dif");
  130. //-------------------------------------------------------------
  131. }
  132. //============================================================================
  133. IplImage * CGraber::getFrameEqualizeHist()
  134. {
  135. if (! m_bFrameEqualizeHistComputed)
  136. {
  137. m_bFrameEqualizeHistComputed = true;
  138. cvEqualizeHist( m_pGrayFrame, m_pGrayFrameEqualizeHist );
  139. }
  140. return m_pGrayFrameEqualizeHist;
  141. }
  142. //============================================================================
  143. IplImage * CGraber::getFrameCopy()
  144. {
  145. if (! m_bFrameCopyComputed)
  146. {
  147. m_bFrameCopyComputed = true;
  148. cvCopyImage(m_pFrame,m_pFrameCopy);
  149. }
  150. return m_pFrameCopy;
  151. }
  152. //============================================================================
  153. bool CGraber::getNextFrame(IplImage **outFrame)
  154. {
  155. switch(m_ImageSourceType)
  156. {
  157. case GS_IMG:
  158. {
  159. *outFrame = m_pFrame;
  160. return false;
  161. if( !m_pFrame )
  162. throw "loading image file fail!";
  163. break;
  164. }
  165. default :
  166. {
  167. if( m_pCapture )
  168. {
  169. if( cvGrabFrame( m_pCapture ))
  170. {
  171. m_pOrginalFrame = cvRetrieveFrame( m_pCapture );
  172. //m_pFrame = cvRetrieveFrame( m_pCapture );
  173. UpdateBuffers();
  174. outFrame = &m_pFrame;
  175. return true;
  176. }
  177. else
  178. return false;
  179. }
  180. else
  181. {
  182. cout<<"twhro"<<endl;
  183. throw "capture not initialized!";
  184. };
  185. break;
  186. }
  187. }
  188. return false;
  189. }
  190. //============================================================================
  191. //
  192. //void CGraber::setSorceFileName(string & fileName){
  193. //
  194. //}
  195. //============================================================================
  196. bool CGraber::setFrameSize(int x, int y ){
  197. return 0;
  198. }
  199. //============================================================================
  200. bool CGraber::setFrameSoruce(int SourceType){
  201. return 0;
  202. }
  203. //============================================================================
  204. void CGraber::Stop(){
  205. cvReleaseCapture( &m_pCapture );
  206. }
  207. //============================================================================