如何用极坐标将线转圆?

问题描述 投票:0回答:0

我目前正在使用极坐标系编写将直线转换为圆的代码。与OpenCV上的

cv2.linearPolar
相比,图像不完整,出现畸变。有办法解决这个问题吗?

我的代码输出如下: My code

OpenCV 的

linearPolar
输出如下: Opencv linearPolar

输入图像:

import numpy as np
import cv2
from scipy.ndimage import map_coordinates
import matplotlib.pyplot as plt
from skimage.transform import AffineTransform


def linear_polar(image, center):
    # Get the dimensions of the image
    height, width = image.shape[:2]

    # Create an empty output image
    polar_image = np.zeros_like(image)

    # Calculate the maximum radius
    max_radius = 450

    # Create arrays to hold the Cartesian coordinates
    x, y = np.meshgrid(np.arange(width), np.arange(height))

    # Convert the Cartesian coordinates to polar coordinates
    radius = np.sqrt((x - center[0])**2 + (y - center[1])**2)
    theta = np.arctan2(y - center[1], x - center[0])

    # Map the polar coordinates to just below the range of the image dimensions
    radius = np.interp(radius, [0, max_radius], [0, width - 1])
    theta = np.interp(theta, [-np.pi, np.pi], [0, height - 1])

    # Use map_coordinates to map the input image to the output image
    for i in range(image.shape[2]):
        map_coordinates(image[..., i], [theta, radius], output=polar_image[..., i])

    return polar_image



src = cv2.imread("C:/Users/joong/OneDrive/Documents/code_projects/RatelSoft/data2/Test_img/rotate-3mm.BMP")
# get width and height of input image
h, w = src.shape[:2]

# get dimenions and compute half (for center/radius)
dims = src.shape[:-1]
cols, rows = dims
half_x = rows // 2
half_y = cols // 2
#src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# get dimenions and compute half (for center/radius)


lp = linear_polar(src, (800, 500))
cv2.imshow("lp", lp)
cv2.waitKey(0)
cv2.destroyAllWindows()

编辑解决失真问题的代码

python opencv image-processing scikit-image polar-coordinates
© www.soinside.com 2019 - 2024. All rights reserved.