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
| std::vector<cv::Point2f> GetChessboardCorners(cv::Mat image, cv::Size patternSize, int index) { assert(image.data != nullptr);
std::vector<cv::Point2f> pointBuf; auto find = cv::findChessboardCorners(image, patternSize, pointBuf, cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE); if (find) { cv::cornerSubPix(image, pointBuf, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.0001));
auto IsPointReverse = [](cv::Point2f pa, cv::Point2f pb) -> bool { double dis_pa = std::pow(pa.x, 2) + std::pow(pa.y, 2); double dis_pb = std::pow(pb.x, 2) + std::pow(pb.y, 2); return dis_pa >= dis_pb; };
if (IsPointReverse(pointBuf[0], pointBuf[pointBuf.size() - 1])) { std::reverse(pointBuf.begin(), pointBuf.end()); }
if (1) { cv::Mat rgb; cv::cvtColor(image, rgb, cv::COLOR_GRAY2BGR); cv::drawChessboardCorners(rgb, patternSize, cv::Mat(pointBuf), find); cv::circle(rgb, cv::Point(pointBuf[0].x, pointBuf[0].y), 10, cv::Scalar(0, 0, 255), 10);
char buff[MAXBYTE]; sprintf_s(buff, "./Chessboard_%04d.bmp", index); cv::imwrite(buff, rgb);
std::cout << "Index = " << index << ", Num = " << pointBuf.size() << std::endl; } } return pointBuf; }
|