我有一张图片,使用
修改文档中的示例:
skimage.measure.find_contours()
找到了轮廓,现在我想为完全位于最大封闭轮廓之外的像素创建一个蒙版。您有任何想法如何实现吗?修改文档中的示例:
import numpy as np
import matplotlib.pyplot as plt
from skimage import measure
# Construct some test data
x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
r = np.sin(np.exp((np.sin(x)**2 + np.cos(y)**2)))
# Find contours at a constant value of 0.8
contours = measure.find_contours(r, 0.8)
# Select the largest contiguous contour
contour = sorted(contours, key=lambda x: len(x))[-1]
# Display the image and plot the contour
fig, ax = plt.subplots()
ax.imshow(r, interpolation='nearest', cmap=plt.cm.gray)
X, Y = ax.get_xlim(), ax.get_ylim()
ax.step(contour.T[1], contour.T[0], linewidth=2, c='r')
ax.set_xlim(X), ax.set_ylim(Y)
plt.show()
这里是红色轮廓:
但如果你放大看,就会发现轮廓不在像素的分辨率上。
我该如何创建一个与原始图像具有相同尺寸的图像,并将完全位于轮廓线外(即未被轮廓线穿过)的像素蒙版?例如:
from numpy import ma
masked_image = ma.array(r.copy(), mask=False)
masked_image.mask[pixels_outside_contour] = True
谢谢!