相機標定棋盤格製做

#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文件再打印效果會比較好。ios

注意:ui

1.棋盤格行和列的數量不要同時爲奇數或偶數,不然標定的時候matlab會給出警告而且標定結果可能錯誤。一個合法的參數:.\CheckerBoard.exe 500 450 50 10x9.pngspa

2.打印後用尺子量一下格子的大小,算出來的結果可能不許,標定的時候要用到。code

3.用matlab標定。blog

 

CheckerBoard.exe下載圖片


須要本身下載OpenCV3.4.7,安裝後將opencv_world347.dll(vc15版,x64)拷貝到同一個目錄下,同時還要安裝vc15的運行庫。
ip

相關文章
相關標籤/搜索