我想采取一个不规则形状的png图像与透明的背景,并将其扭曲成一个圆圈。
内容:图像将不得不被投射到一个半球的外部,通过拉伸它来完全填满一个圆似乎是一个很好的第一步。
下面的图片显示了我为此采取的两个步骤:
我的进攻计划如下:
1.将图像调整为正方形
1.获取图像的轮廓作为np数组
1.创建一个与图像直径相同、点数与轮廓相同的圆
1.使用findHomography获取变换矩阵
1.使用此矩阵扭曲源图像
def image_to_circle(filename: str):
source_image = cv2.imread(filename)
source_gray = cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY)
# resize image and grayscale image to a square
height, width = source_gray.shape
dimension = min(width, height)
source_image = cv2.resize(source_image, (dimension, dimension))
source_gray = cv2.resize(source_gray, (dimension, dimension))
# find the source contour
contours, _ = cv2.findContours(
source_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
source_contour = max(contours, key=cv2.contourArea)
radius = dimension / 2
target_contour = generate_circle_points(radius, len(source_contour))
# compute the perspective transformation matrix
homography_matrix, _ = cv2.findHomography(source_contour, target_contour)
# warp the source image to the destination based on the homography matrix
warped_image = cv2.warpPerspective(
source_image, homography_matrix, (dimension, dimension)
)
cv2.imwrite("warped-" + filename, warped_image)
def generate_circle_points(radius, num_points):
angles = np.linspace(0, 2 * np.pi, num_points)
x = radius * np.cos(angles)
y = radius * np.sin(angles)
return np.column_stack((x, y))
然而,这并不起作用,将其视为输入图像:
导致以下输出:
我猜warpPerspective
的工作方式与我预期的不一样?
1条答案
按热度按时间9fkzdhlc1#
下面是如何在Python/OpenCV中将一个正方形图像变形到一个球体上。
输入:
设盲前结果:
掩蔽后的结果:
注:增加增益,增加球面失真