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

/src/MyCapture.cpp

https://github.com/p1r4nh4/CellTrack
C++ | 133 lines | 124 code | 7 blank | 2 comment | 18 complexity | ee35c3d6938216b74991c92a56c3537c MD5 | raw file
  1. #include "MyCapture.h"
  2. #include <fstream>
  3. MyCapture_Files::MyCapture_Files(const wxArrayString& files_)
  4. : MyCapture()
  5. {
  6. files = files_;
  7. if(!(frame = cvLoadImage(files[0].mb_str()))){
  8. wxLogError(_T("Failed to read image file [ %s ]. Corrupt file or unsupported codec."), files[0].c_str());
  9. return;
  10. }
  11. width = frame->width;
  12. height = frame->height;
  13. size = cvSize(width, height);
  14. frameCount = (int) files.GetCount();
  15. fps = 0;
  16. frame_flip = cvCreateImage(size,8,3);
  17. }
  18. MyCapture_Movie::MyCapture_Movie(const char* avi)
  19. : MyCapture(), capture(NULL)
  20. {
  21. frame_flip = NULL;
  22. if(!( capture = cvCaptureFromAVI(avi) )) {
  23. wxLogError(_T("Failed to read movie file [ %s ]. Corrupt file or unsupported codec."), avi);
  24. return;
  25. }
  26. size = cvSize(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
  27. cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
  28. width = size.width;
  29. height = size.height;
  30. frameCount = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
  31. fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
  32. frame_flip = cvCreateImage(size,8,3);
  33. }
  34. MyCapture_Confocal::MyCapture_Confocal(const wxArrayString& files_, int zslides, bool fluorescence)
  35. : MyCapture_Files(files_)
  36. {
  37. zSlides = zslides;
  38. hasFluorescence = fluorescence;
  39. }
  40. MyCapture::~MyCapture()
  41. {
  42. if ( frame_flip )
  43. cvReleaseImage( &frame_flip );
  44. }
  45. MyCapture_Files::~MyCapture_Files(void)
  46. {
  47. if (frame)
  48. cvReleaseImage(&frame);
  49. }
  50. MyCapture_Movie::~MyCapture_Movie()
  51. {
  52. if ( capture )
  53. cvReleaseCapture( &capture );
  54. }
  55. MyCapture_Confocal::~MyCapture_Confocal()
  56. {
  57. }
  58. void MyCapture::setPos(int pos_)
  59. {
  60. if ( pos_ < 0 || pos_ >= frameCount )
  61. throw "invalid frame position";
  62. pos=pos_;
  63. }
  64. void MyCapture_Movie::setPos(int pos_)
  65. {
  66. MyCapture::setPos(pos_);
  67. cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, pos);
  68. }
  69. // The frame is already queried in the derived classes. Here, we just set the
  70. // image parameters from frame obtained upon the first queryFrame call.
  71. IplImage* MyCapture::queryFrame(int pos_){
  72. if (!step) {
  73. unsigned char *raw;
  74. cvGetRawData(frame, &raw, &step);
  75. }
  76. return frame;
  77. }
  78. IplImage* MyCapture_Files::queryFrame(int pos_)
  79. {
  80. if ( pos_ >= 0 )
  81. setPos(pos_);
  82. if ( pos >= frameCount )
  83. throw "no more frames left to query. position passed end of movie.";
  84. if (frame)
  85. {
  86. cvReleaseImage(&frame);
  87. frame = NULL;
  88. }
  89. if (!(frame = cvLoadImage(files[pos].mb_str()))) {
  90. wxLogError(_T("Failed to read image file [ %s ]. Corrupt file or unsupported codec. Frame %d is replaced with a blank image."), files[0].c_str(), pos);
  91. if (size.width)
  92. frame = cvCreateImage(size,8,3);
  93. }
  94. return MyCapture::queryFrame();
  95. }
  96. IplImage* MyCapture_Movie::queryFrame(int pos_){
  97. if ( pos_ >= 0 )
  98. setPos(pos_);
  99. if ( pos >= frameCount )
  100. throw "no more frames left to query. position passed end of movie.";
  101. frame = cvQueryFrame(capture);
  102. if ( frame->origin ){
  103. cvConvertImage( frame, frame_flip, CV_CVTIMG_FLIP | CV_CVTIMG_SWAP_RB );
  104. frame = frame_flip;
  105. }
  106. return MyCapture::queryFrame();
  107. }
  108. IplImage* MyCapture_Confocal::queryFrame(int pos_)
  109. {
  110. return NULL; // because we load dynamically now
  111. }
  112. int MyCapture_Confocal::getSlideNumber()
  113. {
  114. return zSlides;
  115. }
  116. bool MyCapture_Confocal::getFluorescence()
  117. {
  118. return hasFluorescence;
  119. }
  120. wxString MyCapture_Confocal::getFilename(int pos_)
  121. {
  122. if ( pos_ < 0 || pos_ >= frameCount )
  123. return _T("");
  124. else
  125. return files[pos_];
  126. }