PageRenderTime 95ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libopentld/imacq/ImAcq.cpp

https://bitbucket.org/sertacolgunsoylu/opentld
C++ | 245 lines | 163 code | 52 blank | 30 comment | 37 complexity | a8871ebead2ff4186de6d41649e6e7f7 MD5 | raw file
  1. /* Copyright 2011 AIT Austrian Institute of Technology
  2. *
  3. * This file is part of OpenTLD.
  4. *
  5. * OpenTLD is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * OpenTLD is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with OpenTLD. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "ImAcq.h"
  20. #include <cstdio>
  21. #include <opencv/cv.h>
  22. #include <opencv/highgui.h>
  23. ImAcq *imAcqAlloc()
  24. {
  25. ImAcq *imAcq = (ImAcq *)malloc(sizeof(ImAcq));
  26. imAcq->method = IMACQ_CAM;
  27. imAcq->currentFrame = 1;
  28. imAcq->lastFrame = 0;
  29. imAcq->camNo = 0;
  30. imAcq->fps = 24;
  31. return imAcq;
  32. }
  33. void imAcqInit(ImAcq *imAcq)
  34. {
  35. if(imAcq->method == IMACQ_CAM)
  36. {
  37. imAcq->capture = cvCaptureFromCAM(imAcq->camNo);
  38. if(imAcq->capture == NULL)
  39. {
  40. printf("Error: Unable to initialize camera\n");
  41. exit(0);
  42. }
  43. }
  44. else if(imAcq->method == IMACQ_VID)
  45. {
  46. imAcq->capture = cvCaptureFromAVI(imAcq->imgPath);
  47. if(imAcq->capture == NULL)
  48. {
  49. printf("Error: Unable to open video\n");
  50. exit(0);
  51. }
  52. // take all frames
  53. if(imAcq->lastFrame == 0)
  54. imAcq->lastFrame = imAcqVidGetNumberOfFrames(imAcq); //This sometimes returns garbage
  55. // lastFrame out of bounds
  56. if(imAcq->lastFrame > imAcqVidGetNumberOfFrames(imAcq))
  57. {
  58. printf("Error: video has only %d frames you selected %d as last frame.\n",
  59. imAcqVidGetNumberOfFrames(imAcq), imAcq->lastFrame);
  60. exit(1);
  61. }
  62. // something is wrong with startFrame and/or lastFrame
  63. if((imAcq->lastFrame < 1) || (imAcq->currentFrame < 1) || ((imAcq->currentFrame > imAcq->lastFrame)))
  64. {
  65. printf("Error: something is wrong with the start and last frame number. startFrame: %d lastFrame: %d\n",
  66. imAcq->currentFrame, imAcq->lastFrame);
  67. exit(1);
  68. }
  69. // set the video position to the correct frame
  70. //This produces strange results on some videos and is deactivated for now.
  71. //imAcqVidSetNextFrameNumber(imAcq, imAcq->currentFrame);
  72. }
  73. imAcq->startFrame = imAcq->currentFrame;
  74. imAcq->startTime = cvGetTickCount();
  75. }
  76. void imAcqFree(ImAcq *imAcq)
  77. {
  78. if((imAcq->method == IMACQ_CAM) || (imAcq->method == IMACQ_VID))
  79. {
  80. cvReleaseCapture(&imAcq->capture);
  81. }
  82. free(imAcq);
  83. }
  84. IplImage *imAcqLoadImg(ImAcq *imAcq, char *path)
  85. {
  86. IplImage *image = cvLoadImage(path);
  87. if(image == NULL)
  88. {
  89. printf("Error: %s does not exist or is not an image.\n", path);
  90. }
  91. return image;
  92. }
  93. IplImage *imAcqLoadFrame(ImAcq *imAcq, int fNo)
  94. {
  95. char path[255];
  96. sprintf(path, imAcq->imgPath, fNo);
  97. printf("load path %s\n", path);
  98. return cvLoadImage(path);
  99. }
  100. IplImage *imAcqLoadCurrentFrame(ImAcq *imAcq)
  101. {
  102. return imAcqLoadFrame(imAcq, imAcq->currentFrame);
  103. }
  104. IplImage *imAcqGetImgByCurrentTime(ImAcq *imAcq)
  105. {
  106. //Calculate current image number
  107. if(imAcq->method == IMACQ_CAM)
  108. {
  109. //printf("grabbing image from sensor");
  110. return imAcqGrab(imAcq->capture);
  111. }
  112. float secondsPassed = (cvGetTickCount() - imAcq->startTime) / cvGetTickFrequency();
  113. secondsPassed = secondsPassed / 1000000;
  114. int framesPassed = secondsPassed * imAcq->fps;
  115. int currentFrame = imAcq->startFrame + framesPassed;
  116. if(imAcq->lastFrame > 0 && currentFrame > imAcq->lastFrame) return NULL;
  117. return imAcqLoadFrame(imAcq, currentFrame);
  118. }
  119. IplImage *imAcqGetImg(ImAcq *imAcq)
  120. {
  121. IplImage *img = NULL;
  122. if(imAcq->method == IMACQ_CAM || imAcq->method == IMACQ_VID)
  123. {
  124. img = imAcqGrab(imAcq->capture);
  125. }
  126. if(imAcq->method == IMACQ_IMGS)
  127. {
  128. img = imAcqLoadCurrentFrame(imAcq);
  129. }
  130. if(imAcq->method == IMACQ_LIVESIM)
  131. {
  132. img = imAcqGetImgByCurrentTime(imAcq);
  133. }
  134. imAcqAdvance(imAcq);
  135. return img;
  136. }
  137. IplImage *imAcqGrab(CvCapture *capture)
  138. {
  139. IplImage *frame;
  140. frame = cvQueryFrame(capture);
  141. if(frame == NULL)
  142. {
  143. printf("Error: Unable to grab image from video\n");
  144. return NULL;
  145. }
  146. return cvCloneImage(frame);
  147. }
  148. IplImage *imAcqGetImgByFrame(ImAcq *imAcq, int fNo)
  149. {
  150. int oldFNo = imAcq->currentFrame;
  151. imAcq->currentFrame = fNo;
  152. IplImage *img = imAcqGetImg(imAcq);
  153. imAcq->currentFrame = oldFNo;
  154. return img;
  155. }
  156. IplImage *imAcqGetImgAndAdvance(ImAcq *imAcq)
  157. {
  158. IplImage *img = imAcqGetImg(imAcq);
  159. imAcq->currentFrame++;
  160. return img;
  161. }
  162. void imAcqAdvance(ImAcq *imAcq)
  163. {
  164. imAcq->currentFrame++;
  165. }
  166. int imAcqHasMoreFrames(ImAcq *imAcq)
  167. {
  168. if(imAcq->lastFrame < 1) return 1;
  169. if(imAcq->currentFrame > imAcq->lastFrame)
  170. {
  171. return 0;
  172. }
  173. else
  174. {
  175. return 1;
  176. }
  177. }
  178. int imAcqVidGetNextFrameNumber(ImAcq *imAcq)
  179. {
  180. // OpenCV index starts with 0
  181. // maybe a OpenCV bug: cvGetCaptureProperty with CV_CAP_PROP_POS_FRAMES returns the LAST
  182. // frame number to be encoded not the NEXT
  183. return ((int) cvGetCaptureProperty(imAcq->capture , CV_CAP_PROP_POS_FRAMES)) + 2;
  184. }
  185. void imAcqVidSetNextFrameNumber(ImAcq *imAcq, int nFrame)
  186. {
  187. // OpenCV index starts with 0
  188. cvSetCaptureProperty(imAcq->capture , CV_CAP_PROP_POS_FRAMES, nFrame - 2.0);
  189. }
  190. int imAcqVidGetNumberOfFrames(ImAcq *imAcq)
  191. {
  192. return ((int) cvGetCaptureProperty(imAcq->capture , CV_CAP_PROP_FRAME_COUNT));
  193. }