自定义棋盘格尺寸遇到的代码问题?

ericzhai不忘初心 上海理工大学 2020-11-26 10:00:59
我想要用自定义的棋盘格去标定,网上下载了一段代码,但是打印出来与设置的有出入,不知道问题出在哪里(救救孩子吧),望大家指教,以下是我从网上下载的代码,代码中我设置的是10mm,但是打印出来的从来不是(
#include <iostream>
#include <fcntl.h>
#include <io.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>

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 =10; // block边长,单位:mm
int blockNum =10; // 行
int blockNum_col = 7; // 列


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;
}
...全文
80 点赞 收藏 回复
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2020-11-27
打印机分辨率试300dpi或600dpi
回复
发帖
其它技术问题
创建于2007-09-28

3842

社区成员

C/C++ 其它技术问题
申请成为版主
帖子事件
创建了帖子
2020-11-26 10:00
社区公告
暂无公告