相機標定棋盤格制作


#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標定。

 

CheckerBoard.exe下載


需要自己下載OpenCV3.4.7,安裝后將opencv_world347.dll(vc15版,x64)拷貝到同一個目錄下,同時還要安裝vc15的運行庫。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM