As shown in the figure, a chessboard with a resolution of 489 * 643 and a grid size of 10 × 9 is generated. Like the chessboard in the figure below, some places like to be a 9 × 8 chessboard, because the calculation is not a grid, but a corner. Okay, look at the code:
1 #include <opencv2 / opencv.hpp> 2 using namespace cv; 3 4 #include <iostream> 5 using namespace std; 6 7 8 / * ****************** ************************************************** ************************************************** ************************************************** **************************** 9 10 function: generate a checkerboard map 11 12 input: (rows, cols): checkerboard map discrimination Rate, (m, n): Checkerboard with m + 1 column grid, n + 1 row grid 13 14 Output: Checkerboard map 15 16 Return: Ibid 17 18 Remarks: The grid on the chessboard is best satisfied: odd numbers on one side and even numbers on the other side; it is a little inefficient to directly return a Mat ... 19 20 ********************* ************************************************** ************************************************** ************************************************** ************************* * / 21 Mat getChessBoard ( const int & rows, const int & cols, const size_t & n, const size_t & m) 22 { 23 Mat backGround = Mat :: ones (rows, cols, CV_8UC1) * 255 ; 24 25 // size of a cell 26 int r = rows / (int)(n + 1); // 格子高度 27 int c = cols / (int)(m + 1); // 格子宽度 28 29 // cell 30 Mat whiteCell = Mat::ones(r, c, CV_8UC1) * 255; 31 Mat blackCell = Mat::zeros(r, c, CV_8UC1); 32 33 Mat t1, t2; 34 t1 = blackCell.clone(); 35 t2 = whiteCell.clone(); 36 for (size_t i = 0; i < m; i++) 37 { 38 if (i% 2 == 1 ) 39 { 40 hconcat (t1, blackCell, t1); // Splice a blackCell on the right side of t1 41 hconcat (t2, whiteCell, t2); // Tip: use it from below vconcat 42 } 43 if (i% 2 == 0 ) 44 { 45 hconcat (t1, whiteCell, t1); 46 hconcat (t2, blackCell, t2); 47 } 48 } 49 50 Mat view; 51 view = t1.clone(); 52 for (size_t i = 0; i < n; i++) 53 { 54 if (i % 2 == 1) 55 { 56 view.push_back(t1); 57 } 58 if (i % 2 == 0) 59 { 60 view.push_back(t2); 61 } 62 } 63 view.copyTo(backGround(Rect(0, 0 , view.cols, view.rows))); 64 return backGround; 65 } 66 67 int main () 68 { 69 // 70 int rows = 643 ; 71 int cols = 489 ; 72 73 // num of cell 74 // odd on one side and even on one side 75 size_t n = 8 ; // n + 1 row grid 76 size_t m = 9 ; // m + 1 column grid 77 78 // 获取一个棋盘图 79 Mat chessBoard = getChessBoard(rows, cols, n, m); 80 81 Mat chessBoards[3]; 82 chessBoards[0] = chessBoard; 83 chessBoards[1] = chessBoard; 84 chessBoards[2] = chessBoard; 85 Mat chessBoardRGB; 86 merge(chessBoards, 3, chessBoardRGB); 87 88 89 vector<Point2f> imagPoints; 90 Size boardSize((int)m, (int)n); 91 92 bool found = findChessboardCorners(chessBoard, boardSize, imagPoints, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE); 93 if (found) 94 { 95 cornerSubPix(chessBoard, imagPoints, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1)); 96 97 drawChessboardCorners(chessBoardRGB, boardSize, Mat(imagPoints), found); 98 } 99 100 namedWindow("...", WINDOW_NORMAL); 101 imshow("...", chessBoardRGB); 102 waitKey(0); 103 return -1; 104 }