本文整理汇总了Python中scipy.ndimage.binary_closing方法的典型用法代码示例。如果您正苦于以下问题:Python ndimage.binary_closing方法的具体用法?Python ndimage.binary_closing怎么用?Python ndimage.binary_closing使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.binary_closing方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_binary_closing01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def test_binary_closing01(self):
expected = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
for type in self.types:
data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type)
out = ndimage.binary_closing(data)
assert_array_almost_equal(out, expected)
示例2: test_binary_closing02
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def test_binary_closing02(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
for type in self.types:
data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type)
out = ndimage.binary_closing(data, struct)
assert_array_almost_equal(out, expected)
示例3: process_failure
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def process_failure(name):
name = name.replace("mask","truth")
name2 = name.replace("truth","")
image,_,_ = image_read_write.load_itk_image(name2)
#image_cropped = image[:,120:420,60:460]
image_mask = np.zeros(image.shape)
center = 256
cc,rr = circle(center+20,center,160)
image_mask[:,cc,rr] = 1
image[image>threshold]=0
image[image!=0]=1
image = image*image_mask
#image_cropped[image_cropped>threshold]=0
#image_cropped[image_cropped!=0]=1
kernel20 = np.zeros((15,15))
cc,rr = circle(7,7,8)
kernel20[cc,rr]=1
image = binary_closing(image, [kernel20],1)
#image[:,:,:]=0
#image[:,120:420,60:460]=image_cropped
truth,_,_ = image_read_write.load_itk_image(name)
print evaluator.calculate_dice(image,truth,name)
image = np.array(image,dtype=np.int8)
#LoadImages.save_itk(image,name)
示例4: test_binary_closing01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def test_binary_closing01(self):
expected = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
for type_ in self.types:
data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type_)
out = ndimage.binary_closing(data)
assert_array_almost_equal(out, expected)
示例5: test_binary_closing02
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def test_binary_closing02(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
for type_ in self.types:
data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type_)
out = ndimage.binary_closing(data, struct)
assert_array_almost_equal(out, expected)
示例6: simple_bodymask
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def simple_bodymask(img):
maskthreshold = -500
oshape = img.shape
img = ndimage.zoom(img, 128/np.asarray(img.shape), order=0)
bodymask = img > maskthreshold
bodymask = ndimage.binary_closing(bodymask)
bodymask = ndimage.binary_fill_holes(bodymask, structure=np.ones((3, 3))).astype(int)
bodymask = ndimage.binary_erosion(bodymask, iterations=2)
bodymask = skimage.measure.label(bodymask.astype(int), connectivity=1)
regions = skimage.measure.regionprops(bodymask.astype(int))
if len(regions) > 0:
max_region = np.argmax(list(map(lambda x: x.area, regions))) + 1
bodymask = bodymask == max_region
bodymask = ndimage.binary_dilation(bodymask, iterations=2)
real_scaling = np.asarray(oshape)/128
return ndimage.zoom(bodymask, real_scaling, order=0)
示例7: calc_hist
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def calc_hist(self, ):
hist_ = np.sum(self.imgarr, axis=1).astype('float')
hist_mean = np.mean(hist_)
self.fft = abs(np.fft.rfft(hist_ - hist_mean))
max_harm = int(np.argmax(self.fft))
self.best_harmonic = self.ht // (1 + max_harm)
assert max_harm > 0
self.closed = nd.binary_closing(self.imgarr, structure=np.ones((1, self.best_harmonic // 4)))
self.hist = np.sum(self.closed, axis=1).astype("float")
self.gauss_hist = nd.filters.gaussian_filter1d(
self.hist,
self.best_harmonic / 16, mode='constant',
cval=0,
truncate=2.0)
self.d_gauss_hist = nd.filters.convolve(self.gauss_hist, [-1, 0, 1])
示例8: get_uv_mask
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def get_uv_mask(vertices_vis, triangles, uv_coords, h, w, resolution):
triangles = triangles.T
vertices_vis = vertices_vis.astype(np.float32)
uv_mask = render_texture(uv_coords.T, vertices_vis[np.newaxis, :], triangles, resolution, resolution, 1)
uv_mask = np.squeeze(uv_mask > 0)
uv_mask = ndimage.binary_closing(uv_mask)
uv_mask = ndimage.binary_erosion(uv_mask, structure = np.ones((4,4)))
uv_mask = ndimage.binary_closing(uv_mask)
uv_mask = ndimage.binary_erosion(uv_mask, structure = np.ones((4,4)))
uv_mask = ndimage.binary_erosion(uv_mask, structure = np.ones((4,4)))
uv_mask = ndimage.binary_erosion(uv_mask, structure = np.ones((4,4)))
uv_mask = uv_mask.astype(np.float32)
return np.squeeze(uv_mask)
示例9: process_image
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def process_image(name, do_closing, closing_structure):
image_train,_,_ = image_read_write.load_itk_image(name)
name = name.replace("mask","truth")
image_truth,_,_ = image_read_write.load_itk_image(name)
truth = np.zeros(image_truth.shape, dtype=np.uint8)
truth[image_truth >0]=1
if do_closing:
image_train = binary_closing(image_train, closing_structure,1)
image_train = binary_closing(image_train, [[[1]],[[1]],[[1]],[[1]],[[1]]],1)
score = calculate_dice(image_train,truth, name)
return score
示例10: setup_method
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def setup_method(self):
a = numpy.zeros((5,5), dtype=bool)
a[1:4, 1:4] = True
a[4,4] = True
self.array = a
self.sq3x3 = numpy.ones((3,3))
self.opened_old = ndimage.binary_opening(self.array, self.sq3x3,
1, None, 0)
self.closed_old = ndimage.binary_closing(self.array, self.sq3x3,
1, None, 0)
示例11: test_closing_new_arguments
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def test_closing_new_arguments(self):
closed_new = ndimage.binary_closing(self.array, self.sq3x3, 1, None,
0, None, 0, False)
assert_array_equal(closed_new, self.closed_old)
示例12: find_facade_cuts
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def find_facade_cuts(facade_sig, dilation_amount=60):
edges = facade_sig > facade_sig.mean() + facade_sig.std()
edges = binary_closing(edges, structure=np.ones(dilation_amount))
run, start, val = run_length_encode(edges)
result = []
for s, e in zip(start[val], start[val] + run[val]):
result.append(s + facade_sig[s:e].argmax())
result = [0] + result + [len(facade_sig) - 1]
result = np.unique(result)
return np.array(result)
示例13: background_mask
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def background_mask(mri, thresh_perc=1):
"""Creates the background mask from an MRI"""
grad_magnitude = gradient_magnitude(mri)
nonzero_grad_mag = grad_magnitude[grad_magnitude > 0]
thresh_val = np.percentile(nonzero_grad_mag.flatten(), thresh_perc)
background_mask = grad_magnitude < thresh_val
se36 = ndimage.generate_binary_structure(3, 6)
closed = ndimage.binary_closing(background_mask, se36, iterations=6)
final_mask = ndimage.binary_erosion(closed, se36, iterations=5)
return final_mask
示例14: crop_and_resize
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def crop_and_resize(img, mask=None, width=192, height=192):
bmask = simple_bodymask(img)
# img[bmask==0] = -1024 # this line removes background outside of the lung.
# However, it has been shown problematic with narrow circular field of views that touch the lung.
# Possibly doing more harm than help
reg = skimage.measure.regionprops(skimage.measure.label(bmask))
if len(reg) > 0:
bbox = np.asarray(reg[0].bbox)
else:
bbox = (0, 0, bmask.shape[0], bmask.shape[1])
img = img[bbox[0]:bbox[2], bbox[1]:bbox[3]]
img = ndimage.zoom(img, np.asarray([width, height]) / np.asarray(img.shape), order=1)
if not mask is None:
mask = mask[bbox[0]:bbox[2], bbox[1]:bbox[3]]
mask = ndimage.zoom(mask, np.asarray([width, height]) / np.asarray(mask.shape), order=0)
# mask = ndimage.binary_closing(mask,iterations=5)
return img, mask, bbox
## For some reasons skimage.transform leads to edgy mask borders compared to ndimage.zoom
# def reshape_mask(mask, tbox, origsize):
# res = np.ones(origsize) * 0
# resize = [tbox[2] - tbox[0], tbox[3] - tbox[1]]
# imgres = skimage.transform.resize(mask, resize, order=0, mode='constant', cval=0, anti_aliasing=False, preserve_range=True)
# res[tbox[0]:tbox[2], tbox[1]:tbox[3]] = imgres
# return res
示例15: main
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import binary_closing [as 别名]
def main(args):
if args.threshold is None:
print("Please provide a binarization threshold")
return 1
data, hdr = read(args.input, inc_header=True)
mask = binarize_volume(data, args.threshold, minvol=args.minvol, fill=args.fill)
if args.base_map is not None:
base_map = read(args.base_map, inc_header=False)
base_mask = binarize_volume(base_map, args.threshold, minvol=args.minvol, fill=args.fill)
total_width = args.extend + args.edge_width
excl_mask = binary_dilate(mask, total_width, strel=args.relion)
base_mask = binary_dilate(base_mask, args.extend, strel=args.relion)
base_mask = base_mask &~ excl_mask
if args.overlap > 0:
incl_mask = binary_dilate(base_mask, args.overlap, strel=args.relion) & excl_mask
base_mask = base_mask | incl_mask
mask = base_mask
elif args.extend > 0:
mask = binary_dilate(mask, args.extend, strel=args.relion)
if args.close:
se = binary_sphere(args.extend, False)
mask = binary_closing(mask, structure=se, iterations=1)
final = mask.astype(np.single)
if args.edge_width != 0:
dt = distance_transform_edt(~mask) # Compute *outward* distance transform of mask.
idx = (dt <= args.edge_width) & (dt > 0) # Identify edge points by distance from mask.
x = np.arange(1, args.edge_width + 1) # Domain of the edge profile.
if "sin" in args.edge_profile:
y = np.sin(np.linspace(np.pi/2, 0, args.edge_width + 1)) # Range of the edge profile.
f = interp1d(x, y[1:])
final[idx] = f(dt[idx]) # Insert edge heights interpolated at distance transform values.
write(args.output, final, psz=hdr["xlen"] / hdr["nx"])
return 0