report-detect/scripts/debug_unwarp_viz.py

51 lines
1.5 KiB
Python
Raw Permalink Normal View History

2026-02-05 13:57:22 +08:00
import cv2
import os
import numpy as np
def debug_unwarp_viz():
img_path = "seal_cropped.png"
img = cv2.imread(img_path)
if img is None: return
h, w = img.shape[:2]
cx, cy = w // 2, h // 2
radius = min(cx, cy)
# We'll save a few unwarps with different radius ranges
ranges = [
(0.6, 0.8),
(0.7, 0.9),
(0.8, 1.0),
(0.5, 0.95),
(0.0, 1.0) # Full unwarp
]
output_dir = "debug_unwarp_viz"
if not os.path.exists(output_dir): os.makedirs(output_dir)
# Try different angles too
angles = [0, 90, 180, 270]
for angle in angles:
M = cv2.getRotationMatrix2D((cx, cy), angle, 1.0)
rotated = cv2.warpAffine(img, M, (w, h), borderValue=(255, 255, 255))
# Use a fixed factor of 1.0 for now
out_w = int(radius * 2 * np.pi)
out_h = radius
unwarped = cv2.warpPolar(rotated, (out_w, out_h), (cx, cy), radius, cv2.WARP_POLAR_LINEAR)
for r_start, r_end in ranges:
strip = unwarped[int(out_h * r_start):int(out_h * r_end), :]
if strip.size == 0: continue
fname = f"a{angle}_r{r_start}-{r_end}.png"
cv2.imwrite(os.path.join(output_dir, fname), strip)
# Also try a flipped version
cv2.imwrite(os.path.join(output_dir, f"a{angle}_r{r_start}-{r_end}_hflip.png"), cv2.flip(strip, 1))
if __name__ == "__main__":
debug_unwarp_viz()