15
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

任意のパターン数のカメラキャリブレーション用ボード画像を作成する

Last updated at Posted at 2017-07-02

OpenCVのサンプルにあったような記憶があるんだけど、見つけられなかったので書いてしまった。こうやって世の中には車輪の再発明が増えていくのか。

(追記)やっぱりサークルグリッドのほうが精度が良いらしいので、サークルグリッド版も追加

チェッカーボード

import argparse
import cv2
import numpy as np


def get_args():
    parser = argparse.ArgumentParser(description="This script creates a checkerboard image for calibration")
    parser.add_argument("--width", type=int, default=7)
    parser.add_argument("--height", type=int, default=10)
    parser.add_argument("--margin_size", type=int, default=50)
    parser.add_argument("--block_size", type=int, default=100)
    args = parser.parse_args()
    return args


def main():
    args = get_args()
    w = args.width + 1
    h = args.height + 1
    margin = args.margin_size
    block_size = args.block_size
    chessboard = np.ones((block_size * h + margin * 2, block_size * w + margin * 2), dtype=np.uint8) * 255

    for y in range(h):
        for x in range(w):
            if (x + y) % 2 == 0:
                sx = x * block_size + margin
                sy = y * block_size + margin
                chessboard[sy:sy + block_size, sx:sx + block_size] = 0

    cv2.imwrite("chessboard{}x{}.png".format(args.width, args.height), chessboard)
    cv2.imshow("chessboard", chessboard)
    cv2.waitKey(-1)


if __name__ == '__main__':
    main()

サークルグリッド

import argparse
import cv2
import numpy as np


def get_args():
    parser = argparse.ArgumentParser(description="This script creates a circleboard image for calibration")
    parser.add_argument("--width", type=int, default=7)
    parser.add_argument("--height", type=int, default=10)
    parser.add_argument("--margin_size", type=int, default=50)
    parser.add_argument("--block_size", type=int, default=100)
    parser.add_argument("--radius", type=int, default=30)
    args = parser.parse_args()
    return args


def main():
    args = get_args()
    w = args.width
    h = args.height
    margin = args.margin_size
    block_size = args.block_size
    radius = args.radius
    chessboard = np.ones((block_size * h + margin * 2, block_size * w + margin * 2), dtype=np.uint8) * 255

    for y in range(h):
        for x in range(w):
            cx = int((x + 0.5) * block_size + margin)
            cy = int((y + 0.5) * block_size + margin)
            cv2.circle(chessboard, (cx, cy), radius, 0, thickness=-1)

    cv2.imwrite("circleboard{}x{}.png".format(w, h), chessboard)
    cv2.imshow("circleboard", chessboard)
    cv2.waitKey(-1)


if __name__ == '__main__':
    main()

chessboard7x10.png

circleboard7x10.png

15
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?