#include <opencv2/opencv.hpp>
#include <iostream>
/** 输出指定格式的棋盘图
@param width 棋盘的宽度,不要超过10000.
@param height 棋盘的高度,不要超过10000.
@param pixel 每个格子的大小,建议行和列的格子数一个为奇数一个为偶数
@param fileName 输出的文件名,支持相对路径和绝对路径,支持png、jpg、bmp等.
*/
static int checkerboard(int width, int height, int pixel, const char* fileName)
{
if (width > 0 && height > 0 && pixel > 0 && width <= 10000 && height <= 10000 && pixel <= width && pixel <= height && fileName)
{
cv::Mat image = cv::Mat::zeros(cv::Size(width, height), CV_8U);
uchar* uc = image.data;
for (size_t j = 0; j < height; j++)
{
for (size_t i = 0; i < width; i++)
{
if ((i / pixel + j / pixel) % 2)
{
uc[j * width + i] = 255;
}
}
}
imwrite(fileName, image);
return 0;
}
return -1;
}
int main(int argc, char** argv)
{
int result = -1;
int width = 0;
int height = 0;
int pixel = 0;
const char* fileName = nullptr;
if (argc == 5)
{
try
{
width = std::stoi(argv[1]);
height = std::stoi(argv[2]);
pixel = std::stoi(argv[3]);
fileName = argv[4];
result = checkerboard(width, height, pixel, fileName);
}
catch (const std::exception&)
{
std::cout << "输入格式错误" << std::endl;
}
}
if (result == 0)
{
std::cout << "生成棋盘图并保存成功" << std::endl;
}
else
{
std::cout << "保存棋盘图失败" << std::endl;
std::cout << "用法:" << std::endl;
std::cout << "CheckerBoard.exe width height pixel fileName" << std::endl;
}
return result;
}
使用这个程序生成一张png格式的图片,不要直接打印,效果不好。用matlab读入并显示,然后另存为PDF文件再打印效果会比较好。
注意:
1.棋盘格行和列的数量不要同时为奇数或偶数,否则标定的时候matlab会给出警告并且标定结果可能错误。一个合法的参数:.\CheckerBoard.exe 500 450 50 10x9.png
2.打印后用尺子量一下格子的大小,算出来的结果可能不准,标定的时候要用到。
3.用matlab标定。
需要自己下载OpenCV3.4.7,安装后将opencv_world347.dll(vc15版,x64)拷贝到同一个目录下,同时还要安装vc15的运行库。