當前位置: 首頁>>代碼示例>>Python>>正文


Python ndimage.label方法代碼示例

本文整理匯總了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 
開發者ID:HiLab-git,項目名稱:PyMIC,代碼行數:22,代碼來源:image_process.py

示例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 
開發者ID:zlzeng,項目名稱:DeepFloorplan,代碼行數:19,代碼來源:util.py

示例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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:18,代碼來源:test_measurements.py

示例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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:18,代碼來源:test_measurements.py

示例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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:19,代碼來源:test_measurements.py

示例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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:20,代碼來源:test_measurements.py

示例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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:20,代碼來源:test_measurements.py

示例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) 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:18,代碼來源:test_measurements.py

示例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 
開發者ID:GUR9000,項目名稱:Deep_MRI_brain_extraction,代碼行數:23,代碼來源:Segmentation_predictor.py

示例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(),), ] 
開發者ID:yeezhu,項目名稱:SPN.pytorch,代碼行數:24,代碼來源:utils.py

示例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) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:18,代碼來源:test_measurements.py

示例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) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:19,代碼來源:test_measurements.py

示例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) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:19,代碼來源:test_measurements.py

示例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 
開發者ID:minerva-ml,項目名稱:open-solution-ship-detection,代碼行數:19,代碼來源:postprocessing.py

示例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 
開發者ID:creare-com,項目名稱:pydem,代碼行數:26,代碼來源:dem_processing.py


注:本文中的scipy.ndimage.label方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。