Skip to content

Instantly share code, notes, and snippets.

@udaya1223
Last active December 28, 2016 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udaya1223/fa4212f07f6aa80188d309d601084953 to your computer and use it in GitHub Desktop.
Save udaya1223/fa4212f07f6aa80188d309d601084953 to your computer and use it in GitHub Desktop.
Make a chessboard pattern using OpenCV for camera calibration
// Make a chessboard pattern given the num. of rows & cols, square size in pixels, and square colors
void crvlMakeChessboardPattern(Mat &outChessboard, int inRows, int inCols, int inSquareSize = 100, Scalar color1 = CV_RGB(0, 0, 0), Scalar color2 = CV_RGB(255, 255, 255)){
CV_Assert(inRows > 1 && inCols > 1);
CV_Assert(inSquareSize > 0);
int chessboardImgCols = inCols*inSquareSize;
int chessboardImgRows = inRows*inSquareSize;
outChessboard = Mat(chessboardImgRows, chessboardImgCols, CV_8UC3, Scalar(0, 0, 0));
for (size_t rows = 0, rowNum = 0; rows < chessboardImgRows; rows += inSquareSize, rowNum++)
{
for (size_t cols = 0, colNum = 0; cols < chessboardImgCols; cols += inSquareSize, colNum++)
{
Rect rec(cols, rows, inSquareSize, inSquareSize);
if ((rowNum + colNum) % 2 == 0)
rectangle(outChessboard, rec, color1, -1, 8);
else
rectangle(outChessboard, rec, color2, -1, 8);
}
}
}
int main(){
Mat chessboardImg;
crvlMakeChessboardPattern(chessboardImg, 6, 8);
imshow("Chessboard1", chessboardImg);
imwrite("Chessboard1.bmp", chessboardImg);
crvlMakeChessboardPattern(chessboardImg, 5, 7, 200, CV_RGB(255,0,0), CV_RGB(0,0,255));
imshow("Chessboard2", chessboardImg);
imwrite("Chessboard2.bmp", chessboardImg);
waitKey(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment