def level_curves(fname, npoints = 200, smoothing = 10, level = 0.5) :
"Loads regularly sampled curves from a .PNG image."
# Find the contour lines
img = misc.imread(fname, flatten = True) # Grayscale
img = (img.T[:, ::-1]) / 255.
img = gaussian_filter(img, smoothing, mode='nearest')
lines = find_contours(img, level)
# Compute the sampling ratio for every contour line
lengths = np.array( [arclength(line) for line in lines] )
points_per_line = np.ceil( npoints * lengths / np.sum(lengths) )
# Interpolate accordingly
points = [] ; connec = [] ; index_offset = 0
for ppl, line in zip(points_per_line, lines) :
(p, c) = resample(line, ppl)
points.append(p)
connec.append(c + index_offset)
index_offset += len(p)
size = np.maximum(img.shape[0], img.shape[1])
points = np.vstack(points) / size
connec = np.vstack(connec)
return Curve(points, connec)
# Pyplot Output =================================================================================
评论列表
文章目录