Blame view

3rdparty/opencv-4.5.4/apps/interactive-calibration/calibPipeline.cpp 3.24 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  
  #include "calibPipeline.hpp"
  
  #include <opencv2/highgui.hpp>
  
  #include <stdexcept>
  
  using namespace calib;
  
  #define CAP_DELAY 10
  
  cv::Size CalibPipeline::getCameraResolution()
  {
      mCapture.set(cv::CAP_PROP_FRAME_WIDTH, 10000);
      mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, 10000);
      int w = (int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH);
      int h = (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT);
      return cv::Size(w,h);
  }
  
  CalibPipeline::CalibPipeline(captureParameters params) :
      mCaptureParams(params)
  {
  
  }
  
  PipelineExitStatus CalibPipeline::start(std::vector<cv::Ptr<FrameProcessor> > processors)
  {
      if(mCaptureParams.source == Camera && !mCapture.isOpened())
      {
          mCapture.open(mCaptureParams.camID);
          cv::Size maxRes = getCameraResolution();
          cv::Size neededRes = mCaptureParams.cameraResolution;
  
          if(maxRes.width < neededRes.width) {
              double aR = (double)maxRes.width / maxRes.height;
              mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.width);
              mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.width/aR);
          }
          else if(maxRes.height < neededRes.height) {
              double aR = (double)maxRes.width / maxRes.height;
              mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.height);
              mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.height*aR);
          }
          else {
              mCapture.set(cv::CAP_PROP_FRAME_HEIGHT, neededRes.height);
              mCapture.set(cv::CAP_PROP_FRAME_WIDTH, neededRes.width);
          }
          mCapture.set(cv::CAP_PROP_AUTOFOCUS, 0);
      }
      else if (mCaptureParams.source == File && !mCapture.isOpened())
          mCapture.open(mCaptureParams.videoFileName);
      mImageSize = cv::Size((int)mCapture.get(cv::CAP_PROP_FRAME_WIDTH), (int)mCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
  
      if(!mCapture.isOpened())
          throw std::runtime_error("Unable to open video source");
  
      cv::Mat frame, processedFrame;
      while(mCapture.grab()) {
          mCapture.retrieve(frame);
          if(mCaptureParams.flipVertical)
              cv::flip(frame, frame, -1);
  
          frame.copyTo(processedFrame);
          for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
              processedFrame = (*it)->processFrame(processedFrame);
          cv::imshow(mainWindowName, processedFrame);
          char key = (char)cv::waitKey(CAP_DELAY);
  
          if(key == 27) // esc
              return Finished;
          else if (key == 114) // r
              return DeleteLastFrame;
          else if (key == 100) // d
              return DeleteAllFrames;
          else if (key == 115) // s
              return SaveCurrentData;
          else if (key == 117) // u
              return SwitchUndistort;
          else if (key == 118) // v
              return SwitchVisualisation;
  
          for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
              if((*it)->isProcessed())
                  return Calibrate;
      }
  
      return Finished;
  }
  
  cv::Size CalibPipeline::getImageSize() const
  {
      return mImageSize;
  }