2016-09-22 2 views
1

윤곽선을 찾은 이미지가 skimage.measure.find_contours()인데, 이제 가장 큰 닫힌 윤곽선 밖의 픽셀에 대한 마스크를 만들고 싶습니다. 어떤 생각을 어떻게 할 것인가? 문서의 예제를 수정skimage 윤곽선에서 마스크 만들기

: 여기

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() 

빨간색의 윤곽이다 :

enter image description here

그러나 당신이 확대되면 윤곽이의 해상도에 있지 발견 픽셀.

enter image description here

어떻게 완전히 외부 (즉 등고선에 의해 교차하지 않음)를 마스크 화소와 원고와 동일한 크기의 이미지를 생성 할 수 있는가? 예 :

from numpy import ma 
masked_image = ma.array(r.copy(), mask=False) 
masked_image.mask[pixels_outside_contour] = True 

고마워요!

답변

1

좋아, 나는 경로로 윤곽을 변환 한 후 내부의 픽셀을 선택하여이 작업을 할 수 있었다 :

# Convert the contour into a closed path 
from matplotlib import path 
closed_path = path.Path(contour.T) 

# Get the points that lie within the closed path 
idx = np.array([[(i,j) for i in range(r.shape[0])] for j in range(r.shape[1])]).reshape(np.prod(r.shape),2) 
mask = closed_path.contains_points(idx).reshape(r.shape) 

# Invert the mask and apply to the image 
mask = np.invert(mask) 
masked_data = ma.array(r.copy(), mask=mask) 

그러나,이 종류의 봉쇄에 대한 느린 테스트 N = r.shape[0]*r.shape[1] 픽셀입니다. 누구든지 더 빠른 알고리즘을 가지고 있습니까? 감사!

관련 문제