Python scipy.ndimage.label() Examples
The following are 30
code examples of scipy.ndimage.label().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scipy.ndimage
, or try the search function
.
Example #1
Source File: image_process.py From PyMIC with Apache License 2.0 | 7 votes |
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
Example #2
Source File: Segmentation_predictor.py From Deep_MRI_brain_extraction with MIT License | 6 votes |
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
Example #3
Source File: dem_processing.py From pydem with Apache License 2.0 | 6 votes |
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
Example #4
Source File: postprocessing.py From open-solution-ship-detection with MIT License | 6 votes |
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
Example #5
Source File: util.py From DeepFloorplan with GNU General Public License v3.0 | 6 votes |
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
Example #6
Source File: labels.py From napari with BSD 3-Clause "New" or "Revised" License | 6 votes |
def mode(self): """MODE: Interactive mode. The normal, default mode is PAN_ZOOM, which allows for normal interactivity with the canvas. In PICK mode the cursor functions like a color picker, setting the clicked on label to be the curent label. If the background is picked it will select the background label `0`. In PAINT mode the cursor functions like a paint brush changing any pixels it brushes over to the current label. If the background label `0` is selected than any pixels will be changed to background and this tool functions like an eraser. The size and shape of the cursor can be adjusted in the properties widget. In FILL mode the cursor functions like a fill bucket replacing pixels of the label clicked on with the current label. It can either replace all pixels of that label or just those that are contiguous with the clicked on pixel. If the background label `0` is selected than any pixels will be changed to background and this tool functions like an eraser. In ERASE mode the cursor functions similarly to PAINT mode, but to paint with background label, which effectively removes the label. """ return str(self._mode)
Example #7
Source File: labels.py From napari with BSD 3-Clause "New" or "Revised" License | 6 votes |
def color(self, color): if not color: color = {} color_mode = LabelColorMode.AUTO else: color_mode = LabelColorMode.DIRECT if self._background_label not in color: color[self._background_label] = 'transparent' if None not in color: color[None] = 'black' colors = { label: transform_color(color_str)[0] for label, color_str in color.items() } self._color = colors self.color_mode = color_mode
Example #8
Source File: test_measurements.py From Computable with MIT License | 6 votes |
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)
Example #9
Source File: test_measurements.py From Computable with MIT License | 6 votes |
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)
Example #10
Source File: test_measurements.py From Computable with MIT License | 6 votes |
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)
Example #11
Source File: test_measurements.py From Computable with MIT License | 6 votes |
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)
Example #12
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #13
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #14
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #15
Source File: utils.py From SPN.pytorch with MIT License | 6 votes |
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(),), ]
Example #16
Source File: test_measurements.py From Computable with MIT License | 6 votes |
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)
Example #17
Source File: test_measurements.py From Computable with MIT License | 6 votes |
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)
Example #18
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_b(self): # Same data as test_a, but different labels. The label 9 exceeds the # length of 'labels', so this test will follow a different code path. x = [0,1,2,6] labels = [0,0,9,9] index = [0,9] for shp in [(4,), (2,2)]: x = np.array(x).reshape(shp) labels = np.array(labels).reshape(shp) counts, sums = ndimage.measurements._stats(x, labels=labels, index=index) assert_array_equal(counts, [2, 2]) assert_array_equal(sums, [1.0, 8.0])
Example #19
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label08(): 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)
Example #20
Source File: grid_utils.py From TINT with BSD 2-Clause "Simplified" License | 5 votes |
def clear_small_echoes(label_image, min_size): """ Takes in binary image and clears objects less than min_size. """ flat_image = pd.Series(label_image.flatten()) flat_image = flat_image[flat_image > 0] size_table = flat_image.value_counts(sort=False) small_objects = size_table.keys()[size_table < min_size] for obj in small_objects: label_image[label_image == obj] = 0 label_image = ndimage.label(label_image) return label_image[0]
Example #21
Source File: grid_utils.py From TINT with BSD 2-Clause "Simplified" License | 5 votes |
def get_filtered_frame(grid, min_size, thresh): """ Returns a labeled frame from gridded radar data. Smaller objects are removed and the rest are labeled. """ echo_height = get_vert_projection(grid, thresh) labeled_echo = ndimage.label(echo_height)[0] frame = clear_small_echoes(labeled_echo, min_size) return frame
Example #22
Source File: postprocessing.py From open-solution-ship-detection with MIT License | 5 votes |
def label(mask): labeled, nr_true = ndi.label(mask) return labeled
Example #23
Source File: labels.py From napari with BSD 3-Clause "New" or "Revised" License | 5 votes |
def color_mode(self): """Color mode to change how color is represented. AUTO (default) allows color to be set via a hash function with a seed. DIRECT allows color of each label to be set directly by a color dict. """ return str(self._color_mode)
Example #24
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label01(): data = np.ones([]) out, n = ndimage.label(data) assert_array_almost_equal(out, 1) assert_equal(n, 1)
Example #25
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label02(): data = np.zeros([]) out, n = ndimage.label(data) assert_array_almost_equal(out, 0) assert_equal(n, 0)
Example #26
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label03(): data = np.ones([1]) out, n = ndimage.label(data) assert_array_almost_equal(out, [1]) assert_equal(n, 1)
Example #27
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label05(): data = np.ones([5]) out, n = ndimage.label(data) assert_array_almost_equal(out, [1, 1, 1, 1, 1]) assert_equal(n, 1)
Example #28
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label06(): data = np.array([1, 0, 1, 1, 0, 1]) out, n = ndimage.label(data) assert_array_almost_equal(out, [1, 0, 2, 2, 0, 3]) assert_equal(n, 3)
Example #29
Source File: test_measurements.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_label07(): 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)
Example #30
Source File: labels.py From napari with BSD 3-Clause "New" or "Revised" License | 5 votes |
def selected_label(self, selected_label): if selected_label < 0: raise ValueError('cannot reduce selected label below 0') if selected_label == self.selected_label: return self._selected_label = selected_label self._selected_color = self.get_color(selected_label) self.events.selected_label()