本文整理汇总了Python中scipy.ndimage.label方法的典型用法代码示例。如果您正苦于以下问题:Python ndimage.label方法的具体用法?Python ndimage.label怎么用?Python ndimage.label使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.label方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_largest_component
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [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: refine_room_region
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def refine_room_region(cw_mask, rm_ind):
label_rm, num_label = ndimage.label((1-cw_mask))
new_rm_ind = np.zeros(rm_ind.shape)
for j in xrange(1, num_label+1):
mask = (label_rm == j).astype(np.uint8)
ys, xs = np.where(mask!=0)
area = (np.amax(xs)-np.amin(xs))*(np.amax(ys)-np.amin(ys))
if area < 100:
continue
else:
room_types, type_counts = np.unique(mask*rm_ind, return_counts=True)
if len(room_types) > 1:
room_types = room_types[1:] # ignore background type which is zero
type_counts = type_counts[1:] # ignore background count
new_rm_ind += mask*room_types[np.argmax(type_counts)]
return new_rm_ind
示例3: test_label07
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label07():
"label 7"
data = np.array([[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, 0]])
out, n = ndimage.label(data)
assert_array_almost_equal(out, [[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, 0]])
assert_equal(n, 0)
示例4: test_label08
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label08():
"label 8"
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]])
out, n = ndimage.label(data)
assert_array_almost_equal(out, [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[3, 3, 0, 0, 0, 0],
[3, 3, 0, 0, 0, 0],
[0, 0, 0, 4, 4, 0]])
assert_equal(n, 4)
示例5: test_label09
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [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)
示例6: test_label11
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label11():
"label 11"
for type in types:
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]], type)
out, n = ndimage.label(data)
expected = [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[3, 3, 0, 0, 0, 0],
[3, 3, 0, 0, 0, 0],
[0, 0, 0, 4, 4, 0]]
assert_array_almost_equal(out, expected)
assert_equal(n, 4)
示例7: test_label11_inplace
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label11_inplace():
"label 11 in place"
for type in types:
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]], type)
n = ndimage.label(data, output=data)
expected = [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[3, 3, 0, 0, 0, 0],
[3, 3, 0, 0, 0, 0],
[0, 0, 0, 4, 4, 0]]
assert_array_almost_equal(data, expected)
assert_equal(n, 4)
示例8: test_label12
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label12():
"label 12"
for type in types:
data = np.array([[0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 0]], type)
out, n = ndimage.label(data)
expected = [[0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 0]]
assert_array_almost_equal(out, expected)
assert_equal(n, 1)
示例9: remove_small_conneceted_components
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def remove_small_conneceted_components(raw):
"""
All but the two largest connected components will be removed
"""
data = raw.copy()
# binarize image
data[data>0.5] = 1
cc, num_components = ndimage.label(np.uint8(data))
cc=cc.astype("uint16")
vals = np.bincount(cc.ravel())
sizes = list(vals)
try:
second_largest = sorted(sizes)[::-1][1]
except:
return raw.copy()
data[...] = 0
for i in range(0,len(vals)):
# 0 is background
if sizes[i]>=second_largest:
data[cc==i] = raw[cc==i]
return data
示例10: localize_from_map
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def localize_from_map(class_response_map, class_idx=0, location_type='bbox', threshold_ratio=1, multi_objects=True):
assert location_type == 'bbox' or location_type == 'point', 'Unknown location type'
foreground_map = class_response_map >= (class_response_map.mean() * threshold_ratio)
if multi_objects:
objects, count = label(foreground_map)
res = []
for obj_idx in range(count):
obj = objects == (obj_idx + 1)
if location_type == 'bbox':
score = class_response_map[obj].mean()
extraction = extract_bbox_from_map
elif location_type == 'point':
obj = class_response_map * obj.astype(float)
score = np.max(obj)
extraction = extract_point_from_map
res.append((class_idx,) + extraction(obj) + (score,))
return res
else:
if location_type == 'bbox':
return [(class_idx,) + extract_bbox_from_map(foreground_map) + (class_response_map.mean(),), ]
elif location_type == 'point':
return [(class_idx,) + extract_point_from_map(class_response_map) + (class_response_map.max(),), ]
示例11: test_label09
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [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)
示例12: test_label11
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label11():
for type in types:
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]], type)
out, n = ndimage.label(data)
expected = [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[3, 3, 0, 0, 0, 0],
[3, 3, 0, 0, 0, 0],
[0, 0, 0, 4, 4, 0]]
assert_array_almost_equal(out, expected)
assert_equal(n, 4)
示例13: test_label11_inplace
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def test_label11_inplace():
for type in types:
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]], type)
n = ndimage.label(data, output=data)
expected = [[1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0],
[3, 3, 0, 0, 0, 0],
[3, 3, 0, 0, 0, 0],
[0, 0, 0, 4, 4, 0]]
assert_array_almost_equal(data, expected)
assert_equal(n, 4)
示例14: mask_postprocessing
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def mask_postprocessing(labeled_mask):
if labeled_mask.max() == 0:
return labeled_mask
else:
img_box = np.zeros_like(labeled_mask)
for label_id in range(1, labeled_mask.max() + 1, 1):
label = np.where(labeled_mask == label_id, 1, 0).astype(np.uint8)
size = label.sum()
if size <= DROP_SIZE:
continue
elif MID_MIN_SIZE < size < MID_MAX_SIZE:
bbox_label = mask_to_bbox(label)
img_box = np.where(bbox_label, label_id, img_box).astype(np.uint8)
else:
img_box = np.where(label, label_id, img_box).astype(np.uint8)
img_box = misc.relabel(img_box)
return img_box
示例15: _find_flats_edges
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import label [as 别名]
def _find_flats_edges(self, data, mag, direction):
"""
Extend flats 1 square downstream
Flats on the downstream side of the flat might find a valid angle,
but that doesn't mean that it's a correct angle. We have to find
these and then set them equal to a flat
"""
i12 = np.arange(data.size).reshape(data.shape)
flat = mag == FLAT_ID_INT
flats, n = spndi.label(flat, structure=FLATS_KERNEL3)
objs = spndi.find_objects(flats)
f = flat.ravel()
d = data.ravel()
for i, _obj in enumerate(objs):
region = flats[_obj] == i+1
I = i12[_obj][region]
J = get_adjacent_index(I, data.shape, data.size)
f[J] = d[J] == d[I[0]]
flat = f.reshape(data.shape)
return flat