/src/Camera.cpp

https://github.com/CognitiveRobotics/object_tracking_2D · C++ · 179 lines · 143 code · 23 blank · 13 comment · 41 complexity · cd743a58f17ca46a49d6f93b773f6fef MD5 · raw file

  1. #include "object_tracking_2D/Camera.h"
  2. #include <iomanip>
  3. CCamera::CCamera(std::string &img_path, bool color, int imgIdx, std::string &intrinsic, std::string &distortion, std::string &imgext)
  4. : verbose_(false)
  5. , cam_type_(CAM_SEQ)
  6. {
  7. // load saved images from disk
  8. m_strImgPath = img_path;
  9. color_ = color;
  10. m_nImgIdx = imgIdx;
  11. intrinsic_ = (CvMat*)cvLoad(intrinsic.c_str());
  12. distortion_ = (CvMat*)cvLoad(distortion.c_str());
  13. img_ext_ = "png"; //imgext;
  14. // Check the image resolution and save it
  15. std::stringstream ss;
  16. ss << m_strImgPath << /*"/" << /*"img" << */std::setw(5) << std::setfill('0') << m_nImgIdx << "." << img_ext_;
  17. std::cout<<ss.str().c_str()<<std::endl;
  18. IplImage* image = cvLoadImage(ss.str().c_str(), color_ ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
  19. width_ = image->width;
  20. height_ = image->height;
  21. std::cout << "image resolution: (" << width_ << ", " << height_ << ")" << std::endl;
  22. img_mapx_ = NULL;
  23. img_mapy_ = NULL;
  24. img_input_ = NULL;
  25. }
  26. CCamera::CCamera(std::string &cam_name, std::string &intrinsic, std::string &distortion, int width, int height)
  27. {
  28. // capture images from camera
  29. if(cam_name.compare("normal") == 0)
  30. cam_type_ = CAM_NORMAL;
  31. else if(cam_name.compare("fire-i") == 0)
  32. cam_type_ = CAM_FIREI;
  33. else if(cam_name.compare("flea") == 0)
  34. cam_type_ = CAM_FLEA;
  35. else if(cam_name.compare("openni") == 0)
  36. cam_type_ = CAM_OPENNI;
  37. else if(cam_name.compare("ach") == 0)
  38. cam_type_ = CAM_ACH;
  39. else
  40. {
  41. std::cerr << "Unknown camera type. Force to use normal camera type" << std::endl;
  42. cam_type_ = CAM_NORMAL;
  43. }
  44. intrinsic_ = (CvMat*)cvLoad(intrinsic.c_str());
  45. distortion_ = (CvMat*)cvLoad(distortion.c_str());
  46. if(cam_type_ == CAM_OPENNI)
  47. {
  48. video_capture_.open(CV_CAP_OPENNI);
  49. if(!video_capture_.isOpened())
  50. {
  51. std::cerr << "Openni sensor cannot be openned" << std::endl;
  52. return;
  53. }
  54. video_capture_.set(CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_VGA_30HZ);
  55. if(video_capture_.get(CV_CAP_OPENNI_IMAGE_GENERATOR_PRESENT))
  56. {
  57. int w = static_cast<int>(video_capture_.get(CV_CAP_OPENNI_IMAGE_GENERATOR + CV_CAP_PROP_FRAME_WIDTH));
  58. int h = static_cast<int>(video_capture_.get(CV_CAP_OPENNI_IMAGE_GENERATOR + CV_CAP_PROP_FRAME_HEIGHT));
  59. assert(w == width);
  60. assert(h == height);
  61. }
  62. else
  63. {
  64. std::cerr << "Device doesn't contain image generator." << std::endl;
  65. return;
  66. }
  67. }
  68. else if (cam_type_ != CAM_ACH)
  69. {
  70. capture_ = cvCaptureFromCAM(0);
  71. assert(capture_);
  72. double fps = cvGetCaptureProperty(capture_, CV_CAP_PROP_FPS);
  73. std::cout << "Camera FPS: " << (int)fps << std::endl;
  74. double msec = (double)cvGetCaptureProperty(capture_, CV_CAP_PROP_FOURCC);
  75. // Force to (width x height)
  76. cvSetCaptureProperty(capture_, CV_CAP_PROP_FRAME_WIDTH, width);
  77. cvSetCaptureProperty(capture_, CV_CAP_PROP_FRAME_HEIGHT, height);
  78. CvSize size = cvSize(
  79. (int)cvGetCaptureProperty(capture_, CV_CAP_PROP_FRAME_WIDTH),
  80. (int)cvGetCaptureProperty(capture_, CV_CAP_PROP_FRAME_HEIGHT)
  81. );
  82. assert(size.width == width);
  83. assert(size.height == height);
  84. }
  85. CvSize size = cvSize(width, height);
  86. // create the undistort map images
  87. img_mapx_ = cvCreateImage(size, IPL_DEPTH_32F, 1);
  88. img_mapy_ = cvCreateImage(size, IPL_DEPTH_32F, 1);
  89. // innit the undistort map
  90. cvInitUndistortMap(intrinsic_, distortion_, img_mapx_, img_mapy_);
  91. // create the main image
  92. img_input_ = cvCreateImage(size, IPL_DEPTH_8U, 3);
  93. img_edge_ = NULL;
  94. width_ = width;
  95. height_ = height;
  96. std::cout << "image resolution: (" << width_ << ", " << height_ << ")" << std::endl;
  97. color_ = true;
  98. }
  99. CCamera::~CCamera(void)
  100. {
  101. if(cam_type_ != CAM_SEQ && cam_type_ != CAM_OPENNI)
  102. cvReleaseCapture(&capture_);
  103. if(img_mapx_) cvReleaseImage(&img_mapx_);
  104. if(img_mapy_) cvReleaseImage(&img_mapy_);
  105. if(img_input_) cvReleaseImage(&img_input_);
  106. }
  107. IplImage* CCamera::getImage()
  108. {
  109. if(cam_type_ != CAM_SEQ)
  110. {
  111. if(cam_type_ == CAM_OPENNI)
  112. {
  113. cv::Mat bgrImage;
  114. if(!video_capture_.grab())
  115. {
  116. std::cerr << "Can not grab images." << std::endl;
  117. return NULL;
  118. }
  119. video_capture_.retrieve(bgrImage, CV_CAP_OPENNI_BGR_IMAGE);
  120. IplImage* frame = new IplImage(bgrImage);
  121. cvCopy(frame, img_input_);
  122. }
  123. else
  124. {
  125. // grab image from camera
  126. int res=cvGrabFrame (capture_);
  127. IplImage* frame = cvRetrieveFrame(capture_);
  128. assert(frame);
  129. cvCopy(frame, img_input_);
  130. }
  131. // check whether its a flea cam
  132. if(cam_type_ == CAM_FLEA)
  133. cvFlip(img_input_); // input from flea camera is upside down
  134. IplImage* t = cvCloneImage(img_input_);
  135. cvRemap(t, img_input_, img_mapx_, img_mapy_); // rectify
  136. cvReleaseImage(&t);
  137. return img_input_;
  138. }
  139. else // image sequence
  140. {
  141. std::stringstream ss;
  142. verbose_= -0;
  143. ss << m_strImgPath << /*"img" <<*/ std::setw(5) << std::setfill('0') << m_nImgIdx << "." << img_ext_;
  144. if(verbose_) std::cout << "Load image: " << ss.str() << std::endl;
  145. // Read an image from the saved image sequence
  146. IplImage* image = cvLoadImage(ss.str().c_str(), color_ ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
  147. // Read edge image if there are edge images in the same folder. (e.g. Berkeley edges, scale space edges)
  148. // If there are no edge images, 'img_edge_' would be NULL
  149. ss.str(std::string());
  150. ss << m_strImgPath << "/" << /*"img" <<*/ std::setw(4) << std::setfill('0') << m_nImgIdx << "bedges." << "png";
  151. img_edge_ = cvLoadImage(ss.str().c_str(), CV_LOAD_IMAGE_GRAYSCALE);
  152. // Increase the image index
  153. m_nImgIdx++;
  154. return image;
  155. }
  156. }