【发布时间】:2020-05-30 12:11:09
【问题描述】:
我想用圆圈内最接近的值填充圆形区域外的图像。效果类似于 skimage 的 mode='edge' 但适用于图像的圆形区域而不是矩形区域。
做正确事情的简单代码 - 极其缓慢:
def circle_pad(img, xc, yc, r):
img_out = img.copy()
for i in range(img.shape[0]):
for j in range(img.shape[1]):
d = math.sqrt( (i-yc)**2 + (j-xc)**2 )
if d > r:
i1, j1 = int( yc + (i-yc)*(r/d) ), int( xc + (j-xc)*(r/d) )
img_out[i,j] = img[i1,j1]
return img_out
如何使用 numpy 加快速度? (可能避免在python代码中循环遍历每个像素;典型的图像是几千万像素)
我想过使用沿着meshgrid的线的东西作为起点来计算要在每个点填充的值的坐标,但具体方法不清楚。
【问题讨论】:
标签: python numpy scikit-image