本文整理汇总了Python中scipy.ndimage.distance_transform_edt方法的典型用法代码示例。如果您正苦于以下问题:Python ndimage.distance_transform_edt方法的具体用法?Python ndimage.distance_transform_edt怎么用?Python ndimage.distance_transform_edt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.distance_transform_edt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_blobs
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def random_blobs(shape, blobdensity, size, roughness=2.0):
from random import randint
from builtins import range # python2 compatible
h, w = shape
numblobs = int(blobdensity * w * h)
mask = np.zeros((h, w), 'i')
for i in range(numblobs):
mask[randint(0, h-1), randint(0, w-1)] = 1
dt = ndi.distance_transform_edt(1-mask)
mask = np.array(dt < size, 'f')
mask = ndi.gaussian_filter(mask, size/(2*roughness))
mask -= np.amin(mask)
mask /= np.amax(mask)
noise = np.random.rand(h, w)
noise = ndi.gaussian_filter(noise, size/(2*roughness))
noise -= np.amin(noise)
noise /= np.amax(noise)
return np.array(mask * noise > 0.5, 'f')
示例2: test_distance_transform_edt01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def test_distance_transform_edt01(self):
#euclidean distance transform (edt)
for type in self.types:
data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]], type)
out, ft = ndimage.distance_transform_edt(data,
return_indices=True)
bf = ndimage.distance_transform_bf(data, 'euclidean')
assert_array_almost_equal(bf, out)
dt = ft - numpy.indices(ft.shape[1:], dtype=ft.dtype)
dt = dt.astype(numpy.float64)
numpy.multiply(dt, dt, dt)
dt = numpy.add.reduce(dt, axis=0)
numpy.sqrt(dt, dt)
assert_array_almost_equal(bf, dt)
示例3: test_distance_transform_edt03
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def test_distance_transform_edt03(self):
for type in self.types:
data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]], type)
ref = ndimage.distance_transform_bf(data, 'euclidean',
sampling=[2, 2])
out = ndimage.distance_transform_edt(data,
sampling=[2, 2])
assert_array_almost_equal(ref, out)
示例4: test_distance_transform_edt4
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def test_distance_transform_edt4(self):
for type in self.types:
data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]], type)
ref = ndimage.distance_transform_bf(data, 'euclidean',
sampling=[2, 1])
out = ndimage.distance_transform_edt(data,
sampling=[2, 1])
assert_array_almost_equal(ref, out)
示例5: get_distance_to_nearest_keypoint
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def get_distance_to_nearest_keypoint(x1, y1, shape):
''' Return full-res matrix with distance to nearest keypoint in pixels
Parameters
----------
x1 : 1D vector - X coordinates of keypoints
y1 : 1D vector - Y coordinates of keypoints
shape : shape of image
Returns
-------
dist : 2D numpy array - image with distances
'''
seed = np.zeros(shape, dtype=bool)
seed[np.uint16(y1), np.uint16(x1)] = True
dist = nd.distance_transform_edt(~seed,
return_distances=True,
return_indices=False)
return dist
示例6: test_distance_transform_edt01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def test_distance_transform_edt01(self):
# euclidean distance transform (edt)
for type_ in self.types:
data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
out, ft = ndimage.distance_transform_edt(data, return_indices=True)
bf = ndimage.distance_transform_bf(data, 'euclidean')
assert_array_almost_equal(bf, out)
dt = ft - numpy.indices(ft.shape[1:], dtype=ft.dtype)
dt = dt.astype(numpy.float64)
numpy.multiply(dt, dt, dt)
dt = numpy.add.reduce(dt, axis=0)
numpy.sqrt(dt, dt)
assert_array_almost_equal(bf, dt)
示例7: computeEvaluationMask
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def computeEvaluationMask(maskDIR, resolution, level):
"""Computes the evaluation mask.
Args:
maskDIR: the directory of the ground truth mask
resolution: Pixel resolution of the image at level 0
level: The level at which the evaluation mask is made
Returns:
evaluation_mask
"""
slide = openslide.open_slide(maskDIR)
dims = slide.level_dimensions[level]
pixelarray = np.zeros(dims[0]*dims[1], dtype='uint')
pixelarray = np.array(slide.read_region((0,0), level, dims))
distance = nd.distance_transform_edt(255 - pixelarray[:,:,0])
Threshold = 75/(resolution * pow(2, level) * 2) # 75µm is the equivalent size of 5 tumor cells
binary = distance < Threshold
filled_image = nd.morphology.binary_fill_holes(binary)
evaluation_mask = measure.label(filled_image, connectivity = 2)
return evaluation_mask
示例8: compute_distance_map
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def compute_distance_map(seg, label=1):
""" compute distance from label boundaries
:param ndarray seg: integer images, typically a segmentation
:param int label: selected singe label in segmentation
:return ndarray:
>>> img = np.zeros((6, 6), dtype=int)
>>> img[1:5, 2:] = 1
>>> dist = compute_distance_map(img)
>>> np.round(dist, 2)
array([[ 2.24, 1.41, 1. , 1. , 1. , 1.41],
[ 2. , 1. , 0. , 0. , 0. , 1. ],
[ 2. , 1. , 0. , 1. , 1. , 1.41],
[ 2. , 1. , 0. , 1. , 1. , 1.41],
[ 2. , 1. , 0. , 0. , 0. , 1. ],
[ 2.24, 1.41, 1. , 1. , 1. , 1.41]])
"""
contour_coord = contour_coords(seg, label)
# logger.debug('contour coordinates {}'.format(repr(contourCoord)))
contour_map = 1 - binary_image_from_coords(contour_coord, seg.shape)
dist = ndimage.distance_transform_edt(contour_map)
# logger.debug('distance map \total{}'.format(repr(dist)))
return dist
示例9: compute_dtm
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def compute_dtm(img_gt, out_shape):
"""
compute the distance transform map of foreground in binary mask
input: segmentation, shape = (batch_size, x, y, z)
output: the foreground Distance Map (SDM)
dtm(x) = 0; x in segmentation boundary
inf|x-y|; x in segmentation
"""
fg_dtm = np.zeros(out_shape)
for b in range(out_shape[0]): # batch size
for c in range(out_shape[1]):
posmask = img_gt[b].astype(np.bool)
if posmask.any():
posdis = distance(posmask)
fg_dtm[b][c] = posdis
return fg_dtm
示例10: compute_dtm01
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def compute_dtm01(img_gt, out_shape):
"""
compute the normalized distance transform map of foreground in binary mask
input: segmentation, shape = (batch_size, x, y, z)
output: the foreground Distance Map (SDM) shape=out_shape
sdf(x) = 0; x in segmentation boundary
inf|x-y|; x in segmentation
0; x out of segmentation
normalize sdf to [0, 1]
"""
normalized_dtm = np.zeros(out_shape)
for b in range(out_shape[0]): # batch size
# ignore background
for c in range(1, out_shape[1]):
posmask = img_gt[b].astype(np.bool)
if posmask.any():
posdis = distance(posmask)
normalized_dtm[b][c] = posdis/np.max(posdis)
return normalized_dtm
示例11: compute_dtm
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def compute_dtm(img_gt, out_shape):
"""
compute the distance transform map of foreground in binary mask
input: segmentation, shape = (batch_size, x, y, z)
output: the foreground Distance Map (SDM)
dtm(x) = 0; x in segmentation boundary
inf|x-y|; x in segmentation
"""
fg_dtm = np.zeros(out_shape)
for b in range(out_shape[0]): # batch size
for c in range(1, out_shape[1]):
posmask = img_gt[b].astype(np.bool)
if posmask.any():
posdis = distance(posmask)
fg_dtm[b][c] = posdis
return fg_dtm
示例12: hd_loss
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def hd_loss(seg_soft, gt, seg_dtm, gt_dtm):
"""
compute huasdorff distance loss for binary segmentation
input: seg_soft: softmax results, shape=(b,2,x,y,z)
gt: ground truth, shape=(b,x,y,z)
seg_dtm: segmentation distance transform map; shape=(b,2,x,y,z)
gt_dtm: ground truth distance transform map; shape=(b,2,x,y,z)
output: boundary_loss; sclar
"""
delta_s = (seg_soft[:,1,...] - gt.float()) ** 2
s_dtm = seg_dtm[:,1,...] ** 2
g_dtm = gt_dtm[:,1,...] ** 2
dtm = s_dtm + g_dtm
multipled = torch.einsum('bxyz, bxyz->bxyz', delta_s, dtm)
hd_loss = multipled.mean()
return hd_loss
示例13: compute_dtm
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def compute_dtm(img_gt, out_shape):
"""
compute the distance transform map of foreground in binary mask
input: segmentation, shape = (batch_size, x, y, z);
out_shape = (batch_size, 1, x, y, z)
output: the foreground Distance Map (SDM)
dtm(x) = 0; x in segmentation boundary
inf|x-y|; x in segmentation
"""
fg_dtm = np.zeros(out_shape)
for b in range(out_shape[0]): # batch size
for c in range(out_shape[1]):
posmask = img_gt[b].astype(np.bool)
if posmask.any():
posdis = distance(posmask)
fg_dtm[b][c] = posdis
return fg_dtm
示例14: compute_dtm
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def compute_dtm(img_gt, out_shape):
"""
compute the distance transform map of foreground in binary mask
input: segmentation, shape = (batch_size, x, y, z)
output: the foreground Distance Map (SDM)
dtm(x) = 0; x in segmentation boundary
inf|x-y|; x in segmentation
"""
fg_dtm = np.zeros(out_shape)
for b in range(out_shape[0]): # batch size
for c in range(out_shape[1]):
posmask = img_gt[b].astype(np.bool)
if posmask.any():
posdis = distance(posmask)
fg_dtm[b][c] = posdis
return fg_dtm
示例15: get_image_area_to_sample
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import distance_transform_edt [as 别名]
def get_image_area_to_sample(img):
"""
calculate set g_c, which has two properties
1) They represent background pixels
2) They are within a certain distance to the object
:param img: Image that represents the object instance
"""
#TODO: In the paper 'Deep Interactive Object Selection', they calculate g_c first based on the original object instead
# of the dilated one.
# Dilate the object by d_margin pixels to extend the object boundary
img_area = np.copy(img)
img_area = morphology.binary_dilation(img_area, morphology.diamond(D_MARGIN)).astype(np.uint8)
g_c = np.logical_not(img_area).astype(int)
g_c[np.where(distance_transform_edt(g_c) > D)] = 0
return g_c