【发布时间】:2017-12-05 12:13:45
【问题描述】:
我正在尝试在 Python 中对图像进行圆形遮罩。我在网上找到了一些示例代码,但我不确定如何更改数学以使我的圈子出现在正确的位置。
我有一张image_data 类型为numpy.ndarray 的图片,形状为(3725, 4797, 3):
total_rows, total_cols, total_layers = image_data.shape
X, Y = np.ogrid[:total_rows, :total_cols]
center_row, center_col = total_rows/2, total_cols/2
dist_from_center = (X - total_rows)**2 + (Y - total_cols)**2
radius = (total_rows/2)**2
circular_mask = (dist_from_center > radius)
我看到这段代码应用 欧式距离 来计算 dist_from_center,但我不明白 X - total_rows 和 Y - total_cols 部分。这会生成一个四分之一圆的蒙版,以图像的左上角为中心。
X 和Y 在圈子里扮演什么角色?以及如何修改此代码以生成以图像其他位置为中心的蒙版?
【问题讨论】:
-
这实际上不是欧几里得距离。应该是那个总和的平方根。是的,我认为你是对的,应该是
center_row和center_col而不是total...我认为这段代码会产生一个以左上角为中心的四分之一圆蒙版,而不是图像中的居中圆蒙版(注意 @987654333 @ 在这种情况下仍然是错误的)。 -
@AlexanderReynolds 是的,它正在形成一个四分之一圆形掩码,您能否解释一下您是如何确定的?请您用更简单的术语解释一下
np.ogrid,网络上所有关于np.ogrid的解释都在进行中远在我头顶。 -
当然,当我看到这个时我正在使用手机,所以没有写完整的答案,我想当我回到家时会有其他人有,但我想不会。我会去的。
-
@AlexanderReynolds 谢谢,我将等待您的回复。
标签: python arrays numpy image-processing