Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """Corner-calibration helper for digitization. | |
| python calibrate.py x1 y1 x2 y2 x3 y3 x4 y4 # TL TR BR BL (8x8 playing area) | |
| Draws the chosen corners on the photo and the perspective-warped board with an | |
| 8x8 grid, so we can eyeball whether the corners are right before classifying. | |
| """ | |
| import sys | |
| import cv2 as cv | |
| import numpy as np | |
| SIZE = 800 # warped board side (px) | |
| img = cv.imread("samples/board.png") | |
| H, W = img.shape[:2] | |
| if len(sys.argv) == 9: | |
| pts = list(map(float, sys.argv[1:9])) | |
| corners = np.array([[pts[0], pts[1]], [pts[2], pts[3]], | |
| [pts[4], pts[5]], [pts[6], pts[7]]], dtype=np.float32) | |
| else: | |
| # first guess (TL, TR, BR, BL) on the 1920x1080 photo | |
| corners = np.array([[518, 130], [1210, 115], [1370, 900], [455, 915]], dtype=np.float32) | |
| # --- overlay on the original --- | |
| ov = img.copy() | |
| labels = ["TL", "TR", "BR", "BL"] | |
| for (x, y), lab in zip(corners, labels): | |
| cv.circle(ov, (int(x), int(y)), 12, (0, 0, 255), -1) | |
| cv.putText(ov, lab, (int(x) + 14, int(y)), cv.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3) | |
| cv.polylines(ov, [corners.astype(int)], True, (0, 255, 0), 3) | |
| cv.imwrite("/tmp/overlay.png", ov) | |
| # --- perspective warp to a square, with 8x8 grid --- | |
| dst = np.array([[0, 0], [SIZE, 0], [SIZE, SIZE], [0, SIZE]], dtype=np.float32) | |
| M = cv.getPerspectiveTransform(corners, dst) | |
| warp = cv.warpPerspective(img, M, (SIZE, SIZE)) | |
| grid = warp.copy() | |
| for i in range(9): | |
| p = i * SIZE // 8 | |
| cv.line(grid, (p, 0), (p, SIZE), (0, 0, 255), 2) | |
| cv.line(grid, (0, p), (SIZE, p), (0, 0, 255), 2) | |
| cv.imwrite("/tmp/warped_grid.png", grid) | |
| cv.imwrite("/tmp/warped.png", warp) | |
| print("wrote /tmp/overlay.png and /tmp/warped_grid.png") | |