43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
|
|
import cv2
|
|
import numpy as np
|
|
import os
|
|
|
|
def manual_unwarp():
|
|
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_test"
|
|
if not os.path.exists(output_dir): os.makedirs(output_dir)
|
|
|
|
angles = [0, 90, 180, 270]
|
|
factors = [2.0, 3.0]
|
|
flips = [("none", None), ("h", 1), ("v", 0), ("vh", -1)]
|
|
|
|
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)
|
|
|
|
# Annulus for the outer text
|
|
strip = unwarped[int(out_h * 0.7):out_h, :]
|
|
|
|
for f_name, f_code in flips:
|
|
final = strip
|
|
if f_code is not None:
|
|
final = cv2.flip(strip, f_code)
|
|
|
|
fname = f"angle{angle}_f{factor}_{f_name}.png"
|
|
cv2.imwrite(os.path.join(output_dir, fname), final)
|
|
print(f"Saved {fname}")
|
|
|
|
if __name__ == "__main__":
|
|
manual_unwarp()
|