我想在较大图像内的多边形的像素坐标上应用高斯模糊,然后用模糊的多边形在同一坐标上进行一些操作。skimage
中存在的绘制多边形函数可直接直接的图像坐标,而不是掩码。理想情况下,我想将过滤器应用于掩码本身,但draw polygon
功能不会让我蒙版。
img = np.zeros((10, 10), dtype=np.uint8)
r = np.array([1, 2, 8, 1])
c = np.array([1, 7, 4, 1])
rr, cc = polygon(r, c)
# Apply Gaussian blur here on the locations specified by the polygon
img[rr, cc] = 1 # or do something else on the blurred positions.
我显然不能首先在图像上运行高斯模糊,因为如果我在rr, cc
上运行高斯模糊,我将获得小数值,并且将无法通过索引访问相同的多边形。我如何解决问题?
scipy's Gaussian Blur不会将掩码作为输入,因此您需要模糊整个图像,然后将值复制为该多边形。在这种情况下,您可以使用这些索引:
from skimage import filters
img_blurred = filters.gaussian(img)
img_poly_blurred = np.copy(img) # don't modify img in-place unless you're sure!
img_poly_blurred[rr, cc] = img_blurred[rr, cc]
这是我解决的方式。
mask = np.zeros_like(img)
mask[rr, cc] = 1 # or anything else on the blurred positions
mask = filters.gaussian(mask, sigma=3)