calibPipeline.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <stdexcept>
  7. using namespace calib;
  8. #define CAP_DELAY 10
  9. cv::Size CalibPipeline::getCameraResolution()
  10. {
  11. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, 10000);
  12. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, 10000);
  13. int w = (int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH);
  14. int h = (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT);
  15. return cv::Size(w,h);
  16. }
  17. CalibPipeline::CalibPipeline(captureParameters params) :
  18. mCaptureParams(params)
  19. {
  20. }
  21. PipelineExitStatus CalibPipeline::start(std::vector<cv::Ptr<FrameProcessor> > processors)
  22. {
  23. if(mCaptureParams.source == Camera && !mCapture.isOpened())
  24. {
  25. mCapture.open(mCaptureParams.camID);
  26. cv::Size maxRes = getCameraResolution();
  27. cv::Size neededRes = mCaptureParams.cameraResolution;
  28. if(maxRes.width < neededRes.width) {
  29. double aR = (double)maxRes.width / maxRes.height;
  30. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.width);
  31. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.width/aR);
  32. }
  33. else if(maxRes.height < neededRes.height) {
  34. double aR = (double)maxRes.width / maxRes.height;
  35. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.height);
  36. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.height*aR);
  37. }
  38. else {
  39. mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.height);
  40. mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.width);
  41. }
  42. mCapture.set(cv::CAP_PROP_AUTOFOCUS, 0);
  43. }
  44. else if (mCaptureParams.source == File && !mCapture.isOpened())
  45. mCapture.open(mCaptureParams.videoFileName);
  46. mImageSize = cv::Size((int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH), (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
  47. if(!mCapture.isOpened())
  48. throw std::runtime_error("Unable to open video source");
  49. cv::Mat frame, processedFrame;
  50. while(mCapture.grab()) {
  51. mCapture.retrieve(frame);
  52. if(mCaptureParams.flipVertical)
  53. cv::flip(frame, frame, -1);
  54. frame.copyTo(processedFrame);
  55. for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
  56. processedFrame = (*it)->processFrame(processedFrame);
  57. cv::imshow(mainWindowName, processedFrame);
  58. char key = (char)cv::waitKey(CAP_DELAY);
  59. if(key == 27) // esc
  60. return Finished;
  61. else if (key == 114) // r
  62. return DeleteLastFrame;
  63. else if (key == 100) // d
  64. return DeleteAllFrames;
  65. else if (key == 115) // s
  66. return SaveCurrentData;
  67. else if (key == 117) // u
  68. return SwitchUndistort;
  69. else if (key == 118) // v
  70. return SwitchVisualisation;
  71. for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
  72. if((*it)->isProcessed())
  73. return Calibrate;
  74. }
  75. return Finished;
  76. }
  77. cv::Size CalibPipeline::getImageSize() const
  78. {
  79. return mImageSize;
  80. }