gpt4 book ai didi

python - 如何将圆弧延伸成完整的圆?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:03 27 4
gpt4 key购买 nike

给定一个固定大小的二进制方形数组,如下图所示。预先假设该数组包含一个圆或圆的一部分的图像。重要的是这个圆圈始终位于图像的中心。

Example

如果可能的话,有必要找到一种有效的方法将圆弧补充到整圆。

我尝试统计计算从中心到白点的平均距离并完成圆。它有效。我还尝试了霍夫变换来拟合椭圆并确定其大小。但这两种方法都非常耗费资源。

1 方法示意图:

points = np.transpose(np.array(np.nonzero(array))).tolist() # array of one-value points
random.shuffle(points)
points = np.array(points[:500]).astype('uint8') # take into account only 500 random points

distances = np.zeros(points.shape[0], dtype='int32') # array of distances from the centre of image (40, 40) to some point
for i in xrange(points.shape[0]):
distances[i] = int(np.sqrt((points[i][0] - 40) ** 2 + (points[i][1] - 40) ** 2))

u, indices = np.unique(distances, return_inverse=True)
mean_dist = u[np.argmax(np.bincount(indices))] # most probable distance
# use this mean_dist in order to draw a complete circle

1 method result

2 方法示意图:

from skimage.transform import hough_ellipse

result = hough_ellipse(array, min_size=..., max_size=...)
result.sort(order='accumulator')
# ... extract the necessary info from result variable if it's not empty

有人可以建议另一种有效的解决方案吗?谢谢!

最佳答案

I've tried to statistically calculate the average distance from the centre to the white points and complete the circle.

这似乎是一个好的开始。给定一张带有 n 的图像像素,这个算法是O(n)这已经非常有效了。

如果您想要更快的实现,请尝试使用随机化:

m从图像中随机采样点并使用它们来计算白点的平均半径。然后使用此半径完成圆。

此算法将有 O(m)这意味着它对所有人来说都更快 m < n .为 m 选择一个好的值可能会很棘手,因为您必须在运行时间和输出质量之间做出妥协。

关于python - 如何将圆弧延伸成完整的圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com