frameProcessor.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 "frameProcessor.hpp"
  5. #include "rotationConverters.hpp"
  6. #include <opencv2/calib3d.hpp>
  7. #include <opencv2/imgproc.hpp>
  8. #include <opencv2/highgui.hpp>
  9. #include <vector>
  10. #include <string>
  11. #include <algorithm>
  12. #include <limits>
  13. using namespace calib;
  14. #define VIDEO_TEXT_SIZE 4
  15. #define POINT_SIZE 5
  16. static cv::SimpleBlobDetector::Params getDetectorParams()
  17. {
  18. cv::SimpleBlobDetector::Params detectorParams;
  19. detectorParams.thresholdStep = 40;
  20. detectorParams.minThreshold = 20;
  21. detectorParams.maxThreshold = 500;
  22. detectorParams.minRepeatability = 2;
  23. detectorParams.minDistBetweenBlobs = 5;
  24. detectorParams.filterByColor = true;
  25. detectorParams.blobColor = 0;
  26. detectorParams.filterByArea = true;
  27. detectorParams.minArea = 5;
  28. detectorParams.maxArea = 5000;
  29. detectorParams.filterByCircularity = false;
  30. detectorParams.minCircularity = 0.8f;
  31. detectorParams.maxCircularity = std::numeric_limits<float>::max();
  32. detectorParams.filterByInertia = true;
  33. detectorParams.minInertiaRatio = 0.1f;
  34. detectorParams.maxInertiaRatio = std::numeric_limits<float>::max();
  35. detectorParams.filterByConvexity = true;
  36. detectorParams.minConvexity = 0.8f;
  37. detectorParams.maxConvexity = std::numeric_limits<float>::max();
  38. return detectorParams;
  39. }
  40. FrameProcessor::~FrameProcessor()
  41. {
  42. }
  43. bool CalibProcessor::detectAndParseChessboard(const cv::Mat &frame)
  44. {
  45. int chessBoardFlags = cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE | cv::CALIB_CB_FAST_CHECK;
  46. bool isTemplateFound = cv::findChessboardCorners(frame, mBoardSize, mCurrentImagePoints, chessBoardFlags);
  47. if (isTemplateFound) {
  48. cv::Mat viewGray;
  49. cv::cvtColor(frame, viewGray, cv::COLOR_BGR2GRAY);
  50. cv::cornerSubPix(viewGray, mCurrentImagePoints, cv::Size(11,11),
  51. cv::Size(-1,-1), cv::TermCriteria( cv::TermCriteria::EPS+cv::TermCriteria::COUNT, 30, 0.1 ));
  52. cv::drawChessboardCorners(frame, mBoardSize, cv::Mat(mCurrentImagePoints), isTemplateFound);
  53. mTemplateLocations.insert(mTemplateLocations.begin(), mCurrentImagePoints[0]);
  54. }
  55. return isTemplateFound;
  56. }
  57. bool CalibProcessor::detectAndParseChAruco(const cv::Mat &frame)
  58. {
  59. #ifdef HAVE_OPENCV_ARUCO
  60. cv::Ptr<cv::aruco::Board> board = mCharucoBoard.staticCast<cv::aruco::Board>();
  61. std::vector<std::vector<cv::Point2f> > corners, rejected;
  62. std::vector<int> ids;
  63. cv::aruco::detectMarkers(frame, mArucoDictionary, corners, ids, cv::aruco::DetectorParameters::create(), rejected);
  64. cv::aruco::refineDetectedMarkers(frame, board, corners, ids, rejected);
  65. cv::Mat currentCharucoCorners, currentCharucoIds;
  66. if(ids.size() > 0)
  67. cv::aruco::interpolateCornersCharuco(corners, ids, frame, mCharucoBoard, currentCharucoCorners,
  68. currentCharucoIds);
  69. if(ids.size() > 0) cv::aruco::drawDetectedMarkers(frame, corners);
  70. if(currentCharucoCorners.total() > 3) {
  71. float centerX = 0, centerY = 0;
  72. for (int i = 0; i < currentCharucoCorners.size[0]; i++) {
  73. centerX += currentCharucoCorners.at<float>(i, 0);
  74. centerY += currentCharucoCorners.at<float>(i, 1);
  75. }
  76. centerX /= currentCharucoCorners.size[0];
  77. centerY /= currentCharucoCorners.size[0];
  78. mTemplateLocations.insert(mTemplateLocations.begin(), cv::Point2f(centerX, centerY));
  79. cv::aruco::drawDetectedCornersCharuco(frame, currentCharucoCorners, currentCharucoIds);
  80. mCurrentCharucoCorners = currentCharucoCorners;
  81. mCurrentCharucoIds = currentCharucoIds;
  82. return true;
  83. }
  84. #else
  85. CV_UNUSED(frame);
  86. #endif
  87. return false;
  88. }
  89. bool CalibProcessor::detectAndParseACircles(const cv::Mat &frame)
  90. {
  91. bool isTemplateFound = findCirclesGrid(frame, mBoardSize, mCurrentImagePoints, cv::CALIB_CB_ASYMMETRIC_GRID, mBlobDetectorPtr);
  92. if(isTemplateFound) {
  93. mTemplateLocations.insert(mTemplateLocations.begin(), mCurrentImagePoints[0]);
  94. cv::drawChessboardCorners(frame, mBoardSize, cv::Mat(mCurrentImagePoints), isTemplateFound);
  95. }
  96. return isTemplateFound;
  97. }
  98. bool CalibProcessor::detectAndParseDualACircles(const cv::Mat &frame)
  99. {
  100. std::vector<cv::Point2f> blackPointbuf;
  101. cv::Mat invertedView;
  102. cv::bitwise_not(frame, invertedView);
  103. bool isWhiteGridFound = cv::findCirclesGrid(frame, mBoardSize, mCurrentImagePoints, cv::CALIB_CB_ASYMMETRIC_GRID, mBlobDetectorPtr);
  104. if(!isWhiteGridFound)
  105. return false;
  106. bool isBlackGridFound = cv::findCirclesGrid(invertedView, mBoardSize, blackPointbuf, cv::CALIB_CB_ASYMMETRIC_GRID, mBlobDetectorPtr);
  107. if(!isBlackGridFound)
  108. {
  109. mCurrentImagePoints.clear();
  110. return false;
  111. }
  112. cv::drawChessboardCorners(frame, mBoardSize, cv::Mat(mCurrentImagePoints), isWhiteGridFound);
  113. cv::drawChessboardCorners(frame, mBoardSize, cv::Mat(blackPointbuf), isBlackGridFound);
  114. mCurrentImagePoints.insert(mCurrentImagePoints.end(), blackPointbuf.begin(), blackPointbuf.end());
  115. mTemplateLocations.insert(mTemplateLocations.begin(), mCurrentImagePoints[0]);
  116. return true;
  117. }
  118. void CalibProcessor::saveFrameData()
  119. {
  120. std::vector<cv::Point3f> objectPoints;
  121. switch(mBoardType)
  122. {
  123. case Chessboard:
  124. objectPoints.reserve(mBoardSize.height*mBoardSize.width);
  125. for( int i = 0; i < mBoardSize.height; ++i )
  126. for( int j = 0; j < mBoardSize.width; ++j )
  127. objectPoints.push_back(cv::Point3f(j*mSquareSize, i*mSquareSize, 0));
  128. mCalibData->imagePoints.push_back(mCurrentImagePoints);
  129. mCalibData->objectPoints.push_back(objectPoints);
  130. break;
  131. case chAruco:
  132. mCalibData->allCharucoCorners.push_back(mCurrentCharucoCorners);
  133. mCalibData->allCharucoIds.push_back(mCurrentCharucoIds);
  134. break;
  135. case AcirclesGrid:
  136. objectPoints.reserve(mBoardSize.height*mBoardSize.width);
  137. for( int i = 0; i < mBoardSize.height; i++ )
  138. for( int j = 0; j < mBoardSize.width; j++ )
  139. objectPoints.push_back(cv::Point3f((2*j + i % 2)*mSquareSize, i*mSquareSize, 0));
  140. mCalibData->imagePoints.push_back(mCurrentImagePoints);
  141. mCalibData->objectPoints.push_back(objectPoints);
  142. break;
  143. case DoubleAcirclesGrid:
  144. {
  145. float gridCenterX = (2*((float)mBoardSize.width - 1) + 1)*mSquareSize + mTemplDist / 2;
  146. float gridCenterY = (mBoardSize.height - 1)*mSquareSize / 2;
  147. objectPoints.reserve(2*mBoardSize.height*mBoardSize.width);
  148. //white part
  149. for( int i = 0; i < mBoardSize.height; i++ )
  150. for( int j = 0; j < mBoardSize.width; j++ )
  151. objectPoints.push_back(
  152. cv::Point3f(-float((2*j + i % 2)*mSquareSize + mTemplDist +
  153. (2*(mBoardSize.width - 1) + 1)*mSquareSize - gridCenterX),
  154. -float(i*mSquareSize) - gridCenterY,
  155. 0));
  156. //black part
  157. for( int i = 0; i < mBoardSize.height; i++ )
  158. for( int j = 0; j < mBoardSize.width; j++ )
  159. objectPoints.push_back(cv::Point3f(-float((2*j + i % 2)*mSquareSize - gridCenterX),
  160. -float(i*mSquareSize) - gridCenterY, 0));
  161. mCalibData->imagePoints.push_back(mCurrentImagePoints);
  162. mCalibData->objectPoints.push_back(objectPoints);
  163. }
  164. break;
  165. }
  166. }
  167. void CalibProcessor::showCaptureMessage(const cv::Mat& frame, const std::string &message)
  168. {
  169. cv::Point textOrigin(100, 100);
  170. double textSize = VIDEO_TEXT_SIZE * frame.cols / (double) IMAGE_MAX_WIDTH;
  171. cv::bitwise_not(frame, frame);
  172. cv::putText(frame, message, textOrigin, 1, textSize, cv::Scalar(0,0,255), 2, cv::LINE_AA);
  173. cv::imshow(mainWindowName, frame);
  174. cv::waitKey(300);
  175. }
  176. bool CalibProcessor::checkLastFrame()
  177. {
  178. bool isFrameBad = false;
  179. cv::Mat tmpCamMatrix;
  180. const double badAngleThresh = 40;
  181. if(!mCalibData->cameraMatrix.total()) {
  182. tmpCamMatrix = cv::Mat::eye(3, 3, CV_64F);
  183. tmpCamMatrix.at<double>(0,0) = 20000;
  184. tmpCamMatrix.at<double>(1,1) = 20000;
  185. tmpCamMatrix.at<double>(0,2) = mCalibData->imageSize.height/2;
  186. tmpCamMatrix.at<double>(1,2) = mCalibData->imageSize.width/2;
  187. }
  188. else
  189. mCalibData->cameraMatrix.copyTo(tmpCamMatrix);
  190. if(mBoardType != chAruco) {
  191. cv::Mat r, t, angles;
  192. cv::solvePnP(mCalibData->objectPoints.back(), mCurrentImagePoints, tmpCamMatrix, mCalibData->distCoeffs, r, t);
  193. RodriguesToEuler(r, angles, CALIB_DEGREES);
  194. if(fabs(angles.at<double>(0)) > badAngleThresh || fabs(angles.at<double>(1)) > badAngleThresh) {
  195. mCalibData->objectPoints.pop_back();
  196. mCalibData->imagePoints.pop_back();
  197. isFrameBad = true;
  198. }
  199. }
  200. else {
  201. #ifdef HAVE_OPENCV_ARUCO
  202. cv::Mat r, t, angles;
  203. std::vector<cv::Point3f> allObjPoints;
  204. allObjPoints.reserve(mCurrentCharucoIds.total());
  205. for(size_t i = 0; i < mCurrentCharucoIds.total(); i++) {
  206. int pointID = mCurrentCharucoIds.at<int>((int)i);
  207. CV_Assert(pointID >= 0 && pointID < (int)mCharucoBoard->chessboardCorners.size());
  208. allObjPoints.push_back(mCharucoBoard->chessboardCorners[pointID]);
  209. }
  210. cv::solvePnP(allObjPoints, mCurrentCharucoCorners, tmpCamMatrix, mCalibData->distCoeffs, r, t);
  211. RodriguesToEuler(r, angles, CALIB_DEGREES);
  212. if(180.0 - fabs(angles.at<double>(0)) > badAngleThresh || fabs(angles.at<double>(1)) > badAngleThresh) {
  213. isFrameBad = true;
  214. mCalibData->allCharucoCorners.pop_back();
  215. mCalibData->allCharucoIds.pop_back();
  216. }
  217. #endif
  218. }
  219. return isFrameBad;
  220. }
  221. CalibProcessor::CalibProcessor(cv::Ptr<calibrationData> data, captureParameters &capParams) :
  222. mCalibData(data), mBoardType(capParams.board), mBoardSize(capParams.boardSize)
  223. {
  224. mCapuredFrames = 0;
  225. mNeededFramesNum = capParams.calibrationStep;
  226. mDelayBetweenCaptures = static_cast<int>(capParams.captureDelay * capParams.fps);
  227. mMaxTemplateOffset = std::sqrt(static_cast<float>(mCalibData->imageSize.height * mCalibData->imageSize.height) +
  228. static_cast<float>(mCalibData->imageSize.width * mCalibData->imageSize.width)) / 20.0;
  229. mSquareSize = capParams.squareSize;
  230. mTemplDist = capParams.templDst;
  231. switch(mBoardType)
  232. {
  233. case chAruco:
  234. #ifdef HAVE_OPENCV_ARUCO
  235. mArucoDictionary = cv::aruco::getPredefinedDictionary(
  236. cv::aruco::PREDEFINED_DICTIONARY_NAME(capParams.charucoDictName));
  237. mCharucoBoard = cv::aruco::CharucoBoard::create(mBoardSize.width, mBoardSize.height, capParams.charucoSquareLength,
  238. capParams.charucoMarkerSize, mArucoDictionary);
  239. #endif
  240. break;
  241. case AcirclesGrid:
  242. mBlobDetectorPtr = cv::SimpleBlobDetector::create();
  243. break;
  244. case DoubleAcirclesGrid:
  245. mBlobDetectorPtr = cv::SimpleBlobDetector::create(getDetectorParams());
  246. break;
  247. case Chessboard:
  248. break;
  249. }
  250. }
  251. cv::Mat CalibProcessor::processFrame(const cv::Mat &frame)
  252. {
  253. cv::Mat frameCopy;
  254. frame.copyTo(frameCopy);
  255. bool isTemplateFound = false;
  256. mCurrentImagePoints.clear();
  257. switch(mBoardType)
  258. {
  259. case Chessboard:
  260. isTemplateFound = detectAndParseChessboard(frameCopy);
  261. break;
  262. case chAruco:
  263. isTemplateFound = detectAndParseChAruco(frameCopy);
  264. break;
  265. case AcirclesGrid:
  266. isTemplateFound = detectAndParseACircles(frameCopy);
  267. break;
  268. case DoubleAcirclesGrid:
  269. isTemplateFound = detectAndParseDualACircles(frameCopy);
  270. break;
  271. }
  272. if(mTemplateLocations.size() > mDelayBetweenCaptures)
  273. mTemplateLocations.pop_back();
  274. if(mTemplateLocations.size() == mDelayBetweenCaptures && isTemplateFound) {
  275. if(cv::norm(mTemplateLocations.front() - mTemplateLocations.back()) < mMaxTemplateOffset) {
  276. saveFrameData();
  277. bool isFrameBad = checkLastFrame();
  278. if (!isFrameBad) {
  279. std::string displayMessage = cv::format("Frame # %zu captured", std::max(mCalibData->imagePoints.size(),
  280. mCalibData->allCharucoCorners.size()));
  281. if(!showOverlayMessage(displayMessage))
  282. showCaptureMessage(frame, displayMessage);
  283. mCapuredFrames++;
  284. }
  285. else {
  286. std::string displayMessage = "Frame rejected";
  287. if(!showOverlayMessage(displayMessage))
  288. showCaptureMessage(frame, displayMessage);
  289. }
  290. mTemplateLocations.clear();
  291. mTemplateLocations.reserve(mDelayBetweenCaptures);
  292. }
  293. }
  294. return frameCopy;
  295. }
  296. bool CalibProcessor::isProcessed() const
  297. {
  298. if(mCapuredFrames < mNeededFramesNum)
  299. return false;
  300. else
  301. return true;
  302. }
  303. void CalibProcessor::resetState()
  304. {
  305. mCapuredFrames = 0;
  306. mTemplateLocations.clear();
  307. }
  308. CalibProcessor::~CalibProcessor()
  309. {
  310. }
  311. ////////////////////////////////////////////
  312. void ShowProcessor::drawBoard(cv::Mat &img, cv::InputArray points)
  313. {
  314. cv::Mat tmpView = cv::Mat::zeros(img.rows, img.cols, CV_8UC3);
  315. std::vector<cv::Point2f> templateHull;
  316. std::vector<cv::Point> poly;
  317. cv::convexHull(points, templateHull);
  318. poly.resize(templateHull.size());
  319. for(size_t i=0; i<templateHull.size();i++)
  320. poly[i] = cv::Point((int)(templateHull[i].x*mGridViewScale), (int)(templateHull[i].y*mGridViewScale));
  321. cv::fillConvexPoly(tmpView, poly, cv::Scalar(0, 255, 0), cv::LINE_AA);
  322. cv::addWeighted(tmpView, .2, img, 1, 0, img);
  323. }
  324. void ShowProcessor::drawGridPoints(const cv::Mat &frame)
  325. {
  326. if(mBoardType != chAruco)
  327. for(std::vector<std::vector<cv::Point2f> >::iterator it = mCalibdata->imagePoints.begin(); it != mCalibdata->imagePoints.end(); ++it)
  328. for(std::vector<cv::Point2f>::iterator pointIt = (*it).begin(); pointIt != (*it).end(); ++pointIt)
  329. cv::circle(frame, *pointIt, POINT_SIZE, cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
  330. else
  331. for(std::vector<cv::Mat>::iterator it = mCalibdata->allCharucoCorners.begin(); it != mCalibdata->allCharucoCorners.end(); ++it)
  332. for(int i = 0; i < (*it).size[0]; i++)
  333. cv::circle(frame, cv::Point((int)(*it).at<float>(i, 0), (int)(*it).at<float>(i, 1)),
  334. POINT_SIZE, cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
  335. }
  336. ShowProcessor::ShowProcessor(cv::Ptr<calibrationData> data, cv::Ptr<calibController> controller, TemplateType board) :
  337. mCalibdata(data), mController(controller), mBoardType(board)
  338. {
  339. mNeedUndistort = true;
  340. mVisMode = Grid;
  341. mGridViewScale = 0.5;
  342. mTextSize = VIDEO_TEXT_SIZE;
  343. }
  344. cv::Mat ShowProcessor::processFrame(const cv::Mat &frame)
  345. {
  346. if (!mCalibdata->cameraMatrix.empty() && !mCalibdata->distCoeffs.empty())
  347. {
  348. mTextSize = VIDEO_TEXT_SIZE * (double) frame.cols / IMAGE_MAX_WIDTH;
  349. cv::Scalar textColor = cv::Scalar(0,0,255);
  350. cv::Mat frameCopy;
  351. if (mNeedUndistort && mController->getFramesNumberState()) {
  352. if(mVisMode == Grid)
  353. drawGridPoints(frame);
  354. cv::remap(frame, frameCopy, mCalibdata->undistMap1, mCalibdata->undistMap2, cv::INTER_LINEAR);
  355. int baseLine = 100;
  356. cv::Size textSize = cv::getTextSize("Undistorted view", 1, mTextSize, 2, &baseLine);
  357. cv::Point textOrigin(baseLine, frame.rows - (int)(2.5*textSize.height));
  358. cv::putText(frameCopy, "Undistorted view", textOrigin, 1, mTextSize, textColor, 2, cv::LINE_AA);
  359. }
  360. else {
  361. frame.copyTo(frameCopy);
  362. if(mVisMode == Grid)
  363. drawGridPoints(frameCopy);
  364. }
  365. std::string displayMessage;
  366. if(mCalibdata->stdDeviations.at<double>(0) == 0)
  367. displayMessage = cv::format("F = %d RMS = %.3f", (int)mCalibdata->cameraMatrix.at<double>(0,0), mCalibdata->totalAvgErr);
  368. else
  369. displayMessage = cv::format("Fx = %d Fy = %d RMS = %.3f", (int)mCalibdata->cameraMatrix.at<double>(0,0),
  370. (int)mCalibdata->cameraMatrix.at<double>(1,1), mCalibdata->totalAvgErr);
  371. if(mController->getRMSState() && mController->getFramesNumberState())
  372. displayMessage.append(" OK");
  373. int baseLine = 100;
  374. cv::Size textSize = cv::getTextSize(displayMessage, 1, mTextSize - 1, 2, &baseLine);
  375. cv::Point textOrigin = cv::Point(baseLine, 2*textSize.height);
  376. cv::putText(frameCopy, displayMessage, textOrigin, 1, mTextSize - 1, textColor, 2, cv::LINE_AA);
  377. if(mCalibdata->stdDeviations.at<double>(0) == 0)
  378. displayMessage = cv::format("DF = %.2f", mCalibdata->stdDeviations.at<double>(1)*sigmaMult);
  379. else
  380. displayMessage = cv::format("DFx = %.2f DFy = %.2f", mCalibdata->stdDeviations.at<double>(0)*sigmaMult,
  381. mCalibdata->stdDeviations.at<double>(1)*sigmaMult);
  382. if(mController->getConfidenceIntrervalsState() && mController->getFramesNumberState())
  383. displayMessage.append(" OK");
  384. cv::putText(frameCopy, displayMessage, cv::Point(baseLine, 4*textSize.height), 1, mTextSize - 1, textColor, 2, cv::LINE_AA);
  385. if(mController->getCommonCalibrationState()) {
  386. displayMessage = cv::format("Calibration is done");
  387. cv::putText(frameCopy, displayMessage, cv::Point(baseLine, 6*textSize.height), 1, mTextSize - 1, textColor, 2, cv::LINE_AA);
  388. }
  389. int calibFlags = mController->getNewFlags();
  390. displayMessage = "";
  391. if(!(calibFlags & cv::CALIB_FIX_ASPECT_RATIO))
  392. displayMessage.append(cv::format("AR=%.3f ", mCalibdata->cameraMatrix.at<double>(0,0)/mCalibdata->cameraMatrix.at<double>(1,1)));
  393. if(calibFlags & cv::CALIB_ZERO_TANGENT_DIST)
  394. displayMessage.append("TD=0 ");
  395. displayMessage.append(cv::format("K1=%.2f K2=%.2f K3=%.2f", mCalibdata->distCoeffs.at<double>(0), mCalibdata->distCoeffs.at<double>(1),
  396. mCalibdata->distCoeffs.at<double>(4)));
  397. cv::putText(frameCopy, displayMessage, cv::Point(baseLine, frameCopy.rows - (int)(1.5*textSize.height)),
  398. 1, mTextSize - 1, textColor, 2, cv::LINE_AA);
  399. return frameCopy;
  400. }
  401. return frame;
  402. }
  403. bool ShowProcessor::isProcessed() const
  404. {
  405. return false;
  406. }
  407. void ShowProcessor::resetState()
  408. {
  409. }
  410. void ShowProcessor::setVisualizationMode(visualisationMode mode)
  411. {
  412. mVisMode = mode;
  413. }
  414. void ShowProcessor::switchVisualizationMode()
  415. {
  416. if(mVisMode == Grid) {
  417. mVisMode = Window;
  418. updateBoardsView();
  419. }
  420. else {
  421. mVisMode = Grid;
  422. cv::destroyWindow(gridWindowName);
  423. }
  424. }
  425. void ShowProcessor::clearBoardsView()
  426. {
  427. cv::imshow(gridWindowName, cv::Mat());
  428. }
  429. void ShowProcessor::updateBoardsView()
  430. {
  431. if(mVisMode == Window) {
  432. cv::Size originSize = mCalibdata->imageSize;
  433. cv::Mat altGridView = cv::Mat::zeros((int)(originSize.height*mGridViewScale), (int)(originSize.width*mGridViewScale), CV_8UC3);
  434. if(mBoardType != chAruco)
  435. for(std::vector<std::vector<cv::Point2f> >::iterator it = mCalibdata->imagePoints.begin(); it != mCalibdata->imagePoints.end(); ++it)
  436. if(mBoardType != DoubleAcirclesGrid)
  437. drawBoard(altGridView, *it);
  438. else {
  439. size_t pointsNum = (*it).size()/2;
  440. std::vector<cv::Point2f> points(pointsNum);
  441. std::copy((*it).begin(), (*it).begin() + pointsNum, points.begin());
  442. drawBoard(altGridView, points);
  443. std::copy((*it).begin() + pointsNum, (*it).begin() + 2*pointsNum, points.begin());
  444. drawBoard(altGridView, points);
  445. }
  446. else
  447. for(std::vector<cv::Mat>::iterator it = mCalibdata->allCharucoCorners.begin(); it != mCalibdata->allCharucoCorners.end(); ++it)
  448. drawBoard(altGridView, *it);
  449. cv::imshow(gridWindowName, altGridView);
  450. }
  451. }
  452. void ShowProcessor::switchUndistort()
  453. {
  454. mNeedUndistort = !mNeedUndistort;
  455. }
  456. void ShowProcessor::setUndistort(bool isEnabled)
  457. {
  458. mNeedUndistort = isEnabled;
  459. }
  460. ShowProcessor::~ShowProcessor()
  461. {
  462. }