代码:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
//单位转换
int dot_per_inch = 100; //根据自己的电脑设置,DPI:每英寸的像素点数
double cm_to_inch = 0.3937; //1cm=0.3937inch
double inch_to_cm = 2.54; //1inch = 2.54cm
double inch_per_dot = 1.0 / 96.0;
//自定义标定板
double blockSize_cm = 1.5*1.25; //方格尺寸:边长1.5cm的正方形
//设置横列方框数目
int blockcol = 15;
int blockrow = 10;
int blockSize = (int)(blockSize_cm / inch_to_cm * dot_per_inch);
cout << blockSize << endl;
//int imageSize = blockSize * blockNum;
int imagesizecol = blockSize * blockrow;
int imagesizerow = blockSize * blockcol;
Mat chessBoard(imagesizecol, imagesizerow, CV_8UC3, Scalar::all(0));
unsigned char color = 0;
for (int i = 0; i < imagesizerow; i = i + blockSize) {
color = ~color;
for (int j = 0; j < imagesizecol; j = j + blockSize) {
Mat ROI = chessBoard(Rect(i, j, blockSize, blockSize));
ROI.setTo(Scalar::all(color));
color = ~color;
}
}
imshow("Chess board", chessBoard);
imwrite("chessBoard.jpg", chessBoard);
waitKey(0);
return 0;
}
说明:这是在博客上面找的一份代码,忘记出自那里了。仅作笔记,如有冒犯立删。