report-detect/scripts/manual_unwarp_v2.py

50 lines
1.7 KiB
Python
Raw Permalink Normal View History

2026-02-05 13:57:22 +08:00
import cv2
import numpy as np
import os
def manual_unwarp_v2():
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)
output_dir = "manual_unwarp_v2"
if not os.path.exists(output_dir): os.makedirs(output_dir)
angles = [0, 45, 90, 135, 180, 225, 270, 315]
factors = [0.8, 1.0, 1.2]
flips = [("none", None), ("h", 1)]
# We define the annulus [start_r_ratio, end_r_ratio]
# For the outer text "威凯检测技术有限公司"
annuli = [("outer", 0.75, 0.95), ("inner", 0.4, 0.7)]
for angle in angles:
M_rot = cv2.getRotationMatrix2D((cx, cy), angle, 1.0)
rotated = cv2.warpAffine(img, M_rot, (w, h), borderValue=(255, 255, 255))
for factor in factors:
out_w = int(radius * 2 * np.pi * factor)
out_h = radius
unwarped = cv2.warpPolar(rotated, (out_w, out_h), (cx, cy), radius, cv2.WARP_POLAR_LINEAR)
for a_name, r_start, r_end in annuli:
strip = unwarped[int(out_h * r_start):int(out_h * r_end), :]
for f_name, f_code in flips:
final = strip
if f_code is not None:
final = cv2.flip(strip, f_code)
# Also try color enhancement?
# For now just original color.
fname = f"a{angle}_f{factor}_{a_name}_{f_name}.png"
cv2.imwrite(os.path.join(output_dir, fname), final)
# print(f"Saved {fname} shape {final.shape}")
if __name__ == "__main__":
manual_unwarp_v2()