制作棋盘格标定板(固定分辨率解决尺度问题)

分辨率设置为254dpi

// make_chessboard.cpp
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

// method from https://blog.csdn.net/shirly_xr/article/details/83792028
bool SetResolution(const char* path, int iResolution)
{
	FILE * file = fopen(path, "rb+");// - 打开图片文件 
	if (!file)return false;
	int len = _filelength(_fileno(file));// - 获取文件大小 
	char* buf = new char[len];
	fread(buf, sizeof(char), len, file);// - 将图片数据读入缓存 
	char * pResolution = (char*)&iResolution;// - iResolution为要设置的分辨率的数值,如72dpi 										
	buf[0x0D] = 1;// - 设置使用图片密度单位 
	// - 水平密度,水平分辨率 
	buf[0x0E] = pResolution[1];
	buf[0x0F] = pResolution[0];
	// - 垂直密度,垂直分辨率 
	buf[0x10] = pResolution[1];
	buf[0x11] = pResolution[0];

	// - 将文件指针移动回到文件开头 
	fseek(file, 0, SEEK_SET);
	// - 将数据写入文件,覆盖原始的数据,让修改生效 
	fwrite(buf, sizeof(char), len, file);
	fclose(file);
	return true;
}

int main(){

	//自定义标定板
	int blockSize_mm = 60;    // block边长,单位:mm
	int blockNum = 1;          // 行
	int blockNum_col = 1;      // 列


	int resolution_ratio = 10;
	int blockSize_pixel = blockSize_mm * resolution_ratio;
	int edge_size_pixel = blockSize_pixel / 2;

	int imageSize = blockSize_pixel * blockNum;

	int imageSize_col = blockSize_pixel * blockNum_col;
	cout << " imageSize = " << imageSize << endl;
	Mat chessBoard = cv::Mat::zeros(imageSize, imageSize_col, CV_8UC1);

	cout << "chessBoard.size() = " << chessBoard.size() << endl; 

	for (int i = 0; i < imageSize_col; i = i + blockSize_pixel) {  // 列

		for (int j = 0; j < imageSize; j = j + blockSize_pixel) {
			Mat ROI = chessBoard(Rect(i, j, blockSize_pixel, blockSize_pixel));
			if (((i + j) / blockSize_pixel) % 2 == 0)
				ROI.setTo(Scalar::all(0));
			else
				ROI.setTo(Scalar::all(255));
		}
	}

	Mat chessBoard_final = cv::Mat::zeros((imageSize + blockSize_pixel), (imageSize_col + blockSize_pixel), CV_8UC1);
	
	chessBoard_final.setTo(Scalar::all(255));

	Rect chessBoard_rect = Rect(edge_size_pixel, edge_size_pixel, imageSize_col, imageSize);

	cout << "chessBoard_final.size() = " << chessBoard_final.size() << endl;

	//chessBoard_final(chessBoard_rect) = chessBoard;

	chessBoard.copyTo(chessBoard_final(chessBoard_rect));
	imshow("Chess board", chessBoard_final);
	imwrite("chessBoard.jpg", chessBoard_final);

	int iResolution = 254;      // 图像分辨率
	char path[20] = "./chessBoard.jpg";
	SetResolution(path, iResolution);

	waitKey(3000);
	return 0;
}

栗子 9*13 60mm:
制作棋盘格标定板(固定分辨率解决尺度问题)_第1张图片

你可能感兴趣的:(计算机视觉)