正如@fmw42所指出的那样,您可以使用cv2.warpPolar
。以下是一个例子。
import math
import cv2
ring = cv2.imread("so71416458-crop.png")
size = ring.shape[0] # assumes square image
outer_radius = size // 2
inner_radius_factor = 0.7 # 0.70 measured empirically from image
# Unwarp ring
warped = cv2.warpPolar(ring, (size, int(size * math.pi)), (outer_radius, outer_radius), outer_radius, 0)
# Rotate 90 degrees
straightened = cv2.rotate(warped, cv2.ROTATE_90_COUNTERCLOCKWISE)
# Crop to ring only
cropped = straightened[: int(straightened.shape[0] * (1 - inner_radius_factor)), :]
cv2.imwrite("so71416458-straight.png", cropped)
这里出现的“摆动”是由于我的裁剪不精确 - 环的中心点并不完全在图像的中点。