76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
Seal Text Unwarping with Tunable Parameters
|
||
|
|
Generates comparison of different unwarping schemes (Polar to Cartesian)
|
||
|
|
"""
|
||
|
|
import sys
|
||
|
|
import cv2
|
||
|
|
import numpy as np
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
|
||
|
|
def unwarp_seal(image_path, output_path):
|
||
|
|
print(f"Loading {image_path}...", file=sys.stderr)
|
||
|
|
img = cv2.imread(image_path)
|
||
|
|
if img is None:
|
||
|
|
print(f"Error: Could not load {image_path}", file=sys.stderr)
|
||
|
|
return
|
||
|
|
|
||
|
|
h, w = img.shape[:2]
|
||
|
|
cx, cy = w // 2, h // 2
|
||
|
|
radius_base = min(cx, cy)
|
||
|
|
|
||
|
|
# Debug Grid: Rotation (Rows) x Variation (Cols)
|
||
|
|
angles = [0, 90, 180, 270]
|
||
|
|
flips = [
|
||
|
|
("Base", None),
|
||
|
|
("FlipV", 0),
|
||
|
|
("FlipH", 1),
|
||
|
|
("FlipVH", -1)
|
||
|
|
]
|
||
|
|
|
||
|
|
# We'll use a fixed factor and r_scale for the grid
|
||
|
|
factor = 2.0
|
||
|
|
r_scale = 1.0
|
||
|
|
|
||
|
|
rows = []
|
||
|
|
for angle in angles:
|
||
|
|
M = cv2.getRotationMatrix2D((cx, cy), angle, 1.0)
|
||
|
|
rotated_img = cv2.warpAffine(img, M, (w, h), borderValue=(255, 255, 255))
|
||
|
|
|
||
|
|
row_imgs = []
|
||
|
|
for label, flip_code in flips:
|
||
|
|
out_w = int(radius_base * 2 * np.pi * factor)
|
||
|
|
out_h = radius_base
|
||
|
|
|
||
|
|
unwarped = cv2.warpPolar(rotated_img, (out_w, out_h), (cx, cy), radius_base, cv2.INTER_LINEAR + cv2.WARP_FILL_OUTLIERS)
|
||
|
|
|
||
|
|
# Show both outer and inner just in case text is inside
|
||
|
|
# Outer 40%
|
||
|
|
strip = unwarped[int(out_h * 0.6):out_h, :]
|
||
|
|
|
||
|
|
if flip_code is not None:
|
||
|
|
strip = cv2.flip(strip, flip_code)
|
||
|
|
|
||
|
|
# Resize for grid
|
||
|
|
disp_h = 150
|
||
|
|
disp_w = 800
|
||
|
|
strip_disp = cv2.resize(strip, (disp_w, disp_h))
|
||
|
|
|
||
|
|
# Add label
|
||
|
|
cv2.putText(strip_disp, f"Ang:{angle} {label}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
|
||
|
|
row_imgs.append(strip_disp)
|
||
|
|
|
||
|
|
rows.append(np.hstack(row_imgs))
|
||
|
|
|
||
|
|
grid = np.vstack(rows)
|
||
|
|
cv2.imwrite(output_path, grid)
|
||
|
|
print(f"Saved debug grid to {output_path}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
if len(sys.argv) < 3:
|
||
|
|
print("Usage: python unwarp_seal.py <input> <output>")
|
||
|
|
sys.exit(1)
|
||
|
|
unwarp_seal(sys.argv[1], sys.argv[2])
|