calibPipeline.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "calibPipeline.hpp"
  5. #include <opencv2/highgui.hpp>
  6. #include <opencv2/imgproc.hpp>
  7. #include <opencv2/core/utils/logger.hpp>
  8. #include <stdexcept>
  9. using namespace calib;
  10. #define CAP_DELAY 10
  11. cv::Size CalibPipeline::getCameraResolution()
  12. {
  13. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, 10000);
  14. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, 10000);
  15. int w = (int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH);
  16. int h = (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT);
  17. return cv::Size(w,h);
  18. }
  19. CalibPipeline::CalibPipeline(captureParameters params) :
  20. mCaptureParams(params)
  21. {
  22. }
  23. PipelineExitStatus CalibPipeline::start(std::vector<cv::Ptr<FrameProcessor> > processors)
  24. {
  25. const int allowedEmptyFrames = 5;
  26. int emptyFrames = 0;
  27. auto open_camera = [this] () {
  28. if(mCaptureParams.source == Camera)
  29. {
  30. mCapture.open(mCaptureParams.camID, mCaptureParams.camBackend);
  31. cv::Size maxRes = getCameraResolution();
  32. cv::Size neededRes = mCaptureParams.cameraResolution;
  33. if(maxRes.width < neededRes.width) {
  34. double aR = (double)maxRes.width / maxRes.height;
  35. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.width);
  36. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.width/aR);
  37. }
  38. else if(maxRes.height < neededRes.height) {
  39. double aR = (double)maxRes.width / maxRes.height;
  40. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.height);
  41. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.height*aR);
  42. }
  43. else {
  44. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.height);
  45. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.width);
  46. }
  47. mCapture.set(cv::CAP_PROP_AUTOFOCUS, 0);
  48. }
  49. else if (mCaptureParams.source == File)
  50. mCapture.open(mCaptureParams.videoFileName, mCaptureParams.camBackend);
  51. };
  52. if(!mCapture.isOpened()) {
  53. open_camera();
  54. }
  55. mImageSize = cv::Size((int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH), (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
  56. if(!mCapture.isOpened())
  57. throw std::runtime_error("Unable to open video source");
  58. cv::Mat frame, processedFrame, resizedFrame;
  59. while (true) {
  60. if (!mCapture.grab())
  61. {
  62. if (!mCaptureParams.forceReopen)
  63. {
  64. CV_LOG_ERROR(NULL, "VideoCapture error: could not grab the frame.");
  65. break;
  66. }
  67. CV_LOG_INFO(NULL, "VideoCapture error: trying to reopen...");
  68. do
  69. {
  70. open_camera();
  71. } while (!mCapture.isOpened() || !mCapture.grab());
  72. CV_LOG_INFO(NULL, "VideoCapture error: reopened successfully.");
  73. auto newSize = cv::Size((int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH), (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
  74. CV_CheckEQ(mImageSize, newSize, "Camera image size changed after reopening.");
  75. }
  76. mCapture.retrieve(frame);
  77. if (frame.empty()) {
  78. emptyFrames++;
  79. if (emptyFrames >= allowedEmptyFrames) {
  80. CV_LOG_ERROR(NULL, "VideoCapture error: grabbed sequence of empty frames. VideoCapture is not ready or broken.");
  81. return Finished;
  82. }
  83. continue;
  84. } else {
  85. emptyFrames = 0;
  86. if (mImageSize.width == 0 || mImageSize.height == 0) { // looks like VideoCapture does not support required properties
  87. mImageSize = frame.size();
  88. }
  89. }
  90. if(mCaptureParams.flipVertical)
  91. cv::flip(frame, frame, -1);
  92. frame.copyTo(processedFrame);
  93. for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
  94. processedFrame = (*it)->processFrame(processedFrame);
  95. if (std::fabs(mCaptureParams.zoom - 1.) > 0.001f)
  96. {
  97. cv::resize(processedFrame, resizedFrame, cv::Size(), mCaptureParams.zoom, mCaptureParams.zoom);
  98. }
  99. else
  100. {
  101. resizedFrame = std::move(processedFrame);
  102. }
  103. cv::imshow(mainWindowName, resizedFrame);
  104. char key = (char)cv::waitKey(CAP_DELAY);
  105. if(key == 27) // esc
  106. return Finished;
  107. else if (key == 114) // r
  108. return DeleteLastFrame;
  109. else if (key == 100) // d
  110. return DeleteAllFrames;
  111. else if (key == 115) // s
  112. return SaveCurrentData;
  113. else if (key == 117) // u
  114. return SwitchUndistort;
  115. else if (key == 118) // v
  116. return SwitchVisualisation;
  117. for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
  118. if((*it)->isProcessed())
  119. return Calibrate;
  120. }
  121. return Finished;
  122. }
  123. cv::Size CalibPipeline::getImageSize() const
  124. {
  125. return mImageSize;
  126. }