本文整理匯總了Python中scipy.ndimage.generate_binary_structure方法的典型用法代碼示例。如果您正苦於以下問題:Python ndimage.generate_binary_structure方法的具體用法?Python ndimage.generate_binary_structure怎麽用?Python ndimage.generate_binary_structure使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.generate_binary_structure方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_largest_component
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def get_largest_component(image):
"""
get the largest component from 2D or 3D binary image
image: nd array
"""
dim = len(image.shape)
if(image.sum() == 0 ):
print('the largest component is null')
return image
if(dim == 2):
s = ndimage.generate_binary_structure(2,1)
elif(dim == 3):
s = ndimage.generate_binary_structure(3,1)
else:
raise ValueError("the dimension number should be 2 or 3")
labeled_array, numpatches = ndimage.label(image, s)
sizes = ndimage.sum(image, labeled_array, range(1, numpatches + 1))
max_label = np.where(sizes == sizes.max())[0] + 1
output = np.asarray(labeled_array == max_label, np.uint8)
return output
示例2: flood_fill
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def flood_fill(test_array, h_max=255):
"""
fill in the hole
"""
input_array = np.copy(test_array)
el = ndimage.generate_binary_structure(2,2).astype(np.int)
inside_mask = ndimage.binary_erosion(~np.isnan(input_array), structure=el)
output_array = np.copy(input_array)
output_array[inside_mask]=h_max
output_old_array = np.copy(input_array)
output_old_array.fill(0)
el = ndimage.generate_binary_structure(2,1).astype(np.int)
while not np.array_equal(output_old_array, output_array):
output_old_array = np.copy(output_array)
output_array = np.maximum(input_array,ndimage.grey_erosion(output_array, size=(3,3), footprint=el))
return output_array
示例3: test_label09
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_label09():
"label 9"
data = np.array([[1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0]])
struct = ndimage.generate_binary_structure(2, 2)
out, n = ndimage.label(data, struct)
assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[2, 2, 0, 0, 0, 0],
[2, 2, 0, 0, 0, 0],
[0, 0, 0, 3, 3, 0]])
assert_equal(n, 3)
示例4: test_binary_erosion23
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_binary_erosion23(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
for type in self.types:
data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type)
out = ndimage.binary_erosion(data, struct,
border_value=1)
assert_array_almost_equal(out, expected)
示例5: test_binary_dilation26
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_binary_dilation26(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 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([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type)
out = ndimage.binary_dilation(data, struct)
assert_array_almost_equal(out, expected)
示例6: test_binary_opening02
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_binary_opening02(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[1, 1, 1, 0, 0, 0, 0, 0],
[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, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 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_opening(data, struct)
assert_array_almost_equal(out, expected)
示例7: test_binary_closing02
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [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)
示例8: test_label09
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_label09():
data = np.array([[1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0]])
struct = ndimage.generate_binary_structure(2, 2)
out, n = ndimage.label(data, struct)
assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[2, 2, 0, 0, 0, 0],
[2, 2, 0, 0, 0, 0],
[0, 0, 0, 3, 3, 0]])
assert_equal(n, 3)
示例9: test_binary_erosion23
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_binary_erosion23(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
for type_ in self.types:
data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type_)
out = ndimage.binary_erosion(data, struct, border_value=1)
assert_array_almost_equal(out, expected)
示例10: test_binary_dilation26
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_binary_dilation26(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 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([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]], type_)
out = ndimage.binary_dilation(data, struct)
assert_array_almost_equal(out, expected)
示例11: test_binary_opening02
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_binary_opening02(self):
struct = ndimage.generate_binary_structure(2, 2)
expected = [[1, 1, 1, 0, 0, 0, 0, 0],
[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, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 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_opening(data, struct)
assert_array_almost_equal(out, expected)
示例12: test_binary_closing02
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [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)
示例13: _run_interface
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def _run_interface(self, runtime):
in_file = nb.load(self.inputs.in_file)
wm_mask = nb.load(self.inputs.wm_mask).get_data()
wm_mask[wm_mask < 0.9] = 0
wm_mask[wm_mask > 0] = 1
wm_mask = wm_mask.astype(np.uint8)
if self.inputs.erodemsk:
# Create a structural element to be used in an opening operation.
struc = nd.generate_binary_structure(3, 2)
# Perform an opening operation on the background data.
wm_mask = nd.binary_erosion(wm_mask, structure=struc).astype(np.uint8)
data = in_file.get_data()
data *= 1000.0 / np.median(data[wm_mask > 0])
out_file = fname_presuffix(
self.inputs.in_file, suffix="_harmonized", newpath="."
)
in_file.__class__(data, in_file.affine, in_file.header).to_filename(out_file)
self._results["out_file"] = out_file
return runtime
示例14: _prepare_mask
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def _prepare_mask(mask, label, erode=True):
fgmask = mask.copy()
if np.issubdtype(fgmask.dtype, np.integer):
if isinstance(label, (str, bytes)):
label = FSL_FAST_LABELS[label]
fgmask[fgmask != label] = 0
fgmask[fgmask == label] = 1
else:
fgmask[fgmask > 0.95] = 1.0
fgmask[fgmask < 1.0] = 0
if erode:
# Create a structural element to be used in an opening operation.
struc = nd.generate_binary_structure(3, 2)
# Perform an opening operation on the background data.
fgmask = nd.binary_opening(fgmask, structure=struc).astype(np.uint8)
return fgmask
示例15: test_label
# 需要導入模塊: from scipy import ndimage [as 別名]
# 或者: from scipy.ndimage import generate_binary_structure [as 別名]
def test_label(seed, prob, shape, chunks, connectivity):
np.random.seed(seed)
a = np.random.random(shape) < prob
d = da.from_array(a, chunks=chunks)
s = spnd.generate_binary_structure(a.ndim, connectivity)
a_l, a_nl = spnd.label(a, s)
d_l, d_nl = dask_image.ndmeasure.label(d, s)
assert a_nl == d_nl.compute()
assert a_l.dtype == d_l.dtype
assert a_l.shape == d_l.shape
_assert_equivalent_labeling(a_l, d_l.compute())