def segment_neun(data): lowpass = ndimage.gaussian_filter(data, 4) labels = data - lowpass mask = labels >= 1 label_values = np.unique(labels) labels[mask] = 1 + rank_order(labels[mask])[0].astype(labels.dtype) rescaled = exposure.rescale_intensity(labels, out_range=(0, 255)) markers = np.zeros_like(rescaled) markers[rescaled>200] = 1 return markers
def labels_check(labels): labels = np.copy(labels) label_values = np.unique(labels) # Reorder label values to have consecutive integers (no gaps) if np.any(np.diff(label_values) != 1): mask = labels >= 0 labels[mask] = rank_order(labels[mask])[0].astype(labels.dtype) labels = labels.astype(np.int32) if np.any(labels < 0): filled = ndi.binary_propagation(labels > 0, mask=labels >= 0) labels[np.logical_and(np.logical_not(filled), labels == 0)] = -1 del filled labels = np.atleast_3d(labels) return labels
def random_walker(data, labels, mode='bf', tol=1.e-3, copy=True, return_full_prob=False, spacing=None, alpha=0.3, beta=0.3, gamma=0.4, a=130.0, b=10.0, c=800.0): """Random walker algorithm for segmentation from markers. Random walker algorithm is implemented for gray-level or multichannel images. Parameters ---------- data : array_like Image to be segmented in phases. Gray-level `data` can be two- or three-dimensional; multichannel data can be three- or four- dimensional (multichannel=True) with the highest dimension denoting channels. Data spacing is assumed isotropic unless the `spacing` keyword argument is used. labels : array of ints, of same shape as `data` without channels dimension Array of seed markers labeled with different positive integers for different phases. Zero-labeled pixels are unlabeled pixels. Negative labels correspond to inactive pixels that are not taken into account (they are removed from the graph). If labels are not consecutive integers, the labels array will be transformed so that labels are consecutive. In the multichannel case, `labels` should have the same shape as a single channel of `data`, i.e. without the final dimension denoting channels. beta : float Penalization coefficient for the random walker motion (the greater `beta`, the more difficult the diffusion). mode : string, available options {'cg_mg', 'cg', 'bf'} Mode for solving the linear system in the random walker algorithm. If no preference given, automatically attempt to use the fastest option available ('cg_mg' from pyamg >> 'cg' with UMFPACK > 'bf'). - 'bf' (brute force): an LU factorization of the Laplacian is computed. This is fast for small images (<1024x1024), but very slow and memory-intensive for large images (e.g., 3-D volumes). - 'cg' (conjugate gradient): the linear system is solved iteratively using the Conjugate Gradient method from scipy.sparse.linalg. This is less memory-consuming than the brute force method for large images, but it is quite slow. - 'cg_mg' (conjugate gradient with multigrid preconditioner): a preconditioner is computed using a multigrid solver, then the solution is computed with the Conjugate Gradient method. This mode requires that the pyamg module (http://pyamg.org/) is installed. For images of size > 512x512, this is the recommended (fastest) mode. tol : float tolerance to achieve when solving the linear system, in cg' and 'cg_mg' modes. copy : bool If copy is False, the `labels` array will be overwritten with the result of the segmentation. Use copy=False if you want to save on memory. multichannel : bool, default False If True, input data is parsed as multichannel data (see 'data' above for proper input format in this case) return_full_prob : bool, default False If True, the probability that a pixel belongs to each of the labels will be returned, instead of only the most likely label. spacing : iterable of floats Spacing between voxels in each spatial dimension. If `None`, then the spacing between pixels/voxels in each dimension is assumed 1. Returns ------- output : ndarray * If `return_full_prob` is False, array of ints of same shape as `data`, in which each pixel has been labeled according to the marker that reached the pixel first by anisotropic diffusion. * If `return_full_prob` is True, array of floats of shape `(nlabels, data.shape)`. `output[label_nb, i, j]` is the probability that label `label_nb` reaches the pixel `(i, j)` first. See also -------- skimage.morphology.watershed: watershed segmentation A segmentation algorithm based on mathematical morphology and "flooding" of regions from markers. Notes ----- Multichannel inputs are scaled with all channel data combined. Ensure all channels are separately normalized prior to running this algorithm. The `spacing` argument is specifically for anisotropic datasets, where data points are spaced differently in one or more spatial dimensions. Anisotropic data is commonly encountered in medical imaging. The algorithm was first proposed in *Random walks for image segmentation*, Leo Grady, IEEE Trans Pattern Anal Mach Intell. 2006 Nov;28(11):1768-83. The algorithm solves the diffusion equation at infinite times for sources placed on markers of each phase in turn. A pixel is labeled with the phase that has the greatest probability to diffuse first to the pixel. The diffusion equation is solved by minimizing x.T L x for each phase, where L is the Laplacian of the weighted graph of the image, and x is the probability that a marker of the given phase arrives first at a pixel by diffusion (x=1 on markers of the phase, x=0 on the other markers, and the other coefficients are looked for). Each pixel is attributed the label for which it has a maximal value of x. The Laplacian L of the image is defined as: - L_ii = d_i, the number of neighbors of pixel i (the degree of i) - L_ij = -w_ij if i and j are adjacent pixels The weight w_ij is a decreasing function of the norm of the local gradient. This ensures that diffusion is easier between pixels of similar values. When the Laplacian is decomposed into blocks of marked and unmarked pixels:: L = M B.T B A with first indices corresponding to marked pixels, and then to unmarked pixels, minimizing x.T L x for one phase amount to solving:: A x = - B x_m where x_m = 1 on markers of the given phase, and 0 on other markers. This linear system is solved in the algorithm using a direct method for small images, and an iterative method for larger images. Examples -------- >>> np.random.seed(0) >>> a = np.zeros((10, 10)) + 0.2 * np.random.rand(10, 10) >>> a[5:8, 5:8] += 1 >>> b = np.zeros_like(a) >>> b[3, 3] = 1 # Marker for first phase >>> b[6, 6] = 2 # Marker for second phase >>> random_walker(a, b) array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32) """ # Parse input data if mode is None: if amg_loaded: mode = 'cg_mg' elif UmfpackContext is not None: mode = 'cg' else: mode = 'bf' if (labels != 0).all(): warn('Random walker only segments unlabeled areas, where ' 'labels == 0. No zero valued areas in labels were ' 'found. Returning provided labels.') if return_full_prob: # Find and iterate over valid labels unique_labels = np.unique(labels) unique_labels = unique_labels[unique_labels > 0] out_labels = np.empty(labels.shape + (len(unique_labels), ), dtype=np.bool) for n, i in enumerate(unique_labels): out_labels[..., n] = (labels == i) else: out_labels = labels return out_labels if data.ndim < 4: raise ValueError('Dta must have 4 ' 'dimensions.') dims = data[..., 0].shape # To reshape final labeled result data = img_as_float(data) # Spacing kwarg checks if spacing is None: spacing = np.asarray((1., ) * 4) elif len(spacing) == len(dims): if len(spacing) == 2: # Need a dummy spacing for singleton 3rd dim spacing = np.r_[spacing, 1.] else: # Convert to array spacing = np.asarray(spacing) else: raise ValueError('Input argument `spacing` incorrect, should be an ' 'iterable with one number per spatial dimension.') if copy: labels = np.copy(labels) label_values = np.unique(labels) # Reorder label values to have consecutive integers (no gaps) if np.any(np.diff(label_values) != 1): mask = labels >= 0 labels[mask] = rank_order(labels[mask])[0].astype(labels.dtype) labels = labels.astype(np.int32) # If the array has pruned zones, be sure that no isolated pixels # exist between pruned zones (they could not be determined) if np.any(labels < 0): filled = ndi.binary_propagation(labels > 0, mask=labels >= 0) labels[np.logical_and(np.logical_not(filled), labels == 0)] = -1 del filled labels = np.atleast_3d(labels) if np.any(labels < 0): lap_sparse = _build_laplacian(data, spacing, mask=labels >= 0, alpha=alpha, beta=beta, gamma=gamma, a=a, b=b, c=c) else: lap_sparse = _build_laplacian(data, spacing, alpha=alpha, beta=beta, gamma=gamma, a=a, b=b, c=c) lap_sparse, B = _buildAB(lap_sparse, labels) # We solve the linear system # lap_sparse X = B # where X[i, j] is the probability that a marker of label i arrives # first at pixel j by anisotropic diffusion. if mode == 'cg': X = _solve_cg(lap_sparse, B, tol=tol, return_full_prob=return_full_prob) if mode == 'cg_mg': if not amg_loaded: warn("""pyamg (http://pyamg.org/)) is needed to use this mode, but is not installed. The 'cg' mode will be used instead.""") X = _solve_cg(lap_sparse, B, tol=tol, return_full_prob=return_full_prob) else: X = _solve_cg_mg(lap_sparse, B, tol=tol, return_full_prob=return_full_prob) if mode == 'bf': X = _solve_bf(lap_sparse, B, return_full_prob=return_full_prob) # Clean up results if return_full_prob: labels = labels.astype(np.float) X = np.array([ _clean_labels_ar(Xline, labels, copy=True).reshape(dims) for Xline in X ]) for i in range(1, int(labels.max()) + 1): mask_i = np.squeeze(labels == i) X[:, mask_i] = 0 X[i - 1, mask_i] = 1 else: X = _clean_labels_ar(X + 1, labels).reshape(dims) return X
def peak_thrs_local_max(image, min_distance=1, threshold_abs=None, threshold_rel=None, exclude_border=True, indices=True, num_peaks=np.inf, footprint=None, labels=None): """ Function after modification: returns the coordinates for a range of thresholds Peaks are the local maxima in a region of `2 * min_distance + 1` (i.e. peaks are separated by at least `min_distance`). If peaks are flat (i.e. multiple adjacent pixels have identical intensities), the coordinates of all such pixels are returned. If both `threshold_abs` and `threshold_rel` are provided, the maximum of the two is chosen as the minimum intensity threshold of peaks. Parameters ---------- image : ndarray Input image. min_distance : int, optional Minimum number of pixels separating peaks in a region of `2 * min_distance + 1` (i.e. peaks are separated by at least `min_distance`). To find the maximum number of peaks, use `min_distance=1`. threshold_abs : float, optional Minimum intensity of peaks. By default, the absolute threshold is the minimum intensity of the image. threshold_rel : float, optional Minimum intensity of peaks, calculated as `max(image) * threshold_rel`. exclude_border : int, optional If nonzero, `exclude_border` excludes peaks from within `exclude_border`-pixels of the border of the image. indices : bool, optional If True, the output will be an array representing peak coordinates. If False, the output will be a boolean array shaped as `image.shape` with peaks present at True elements. num_peaks : int, optional Maximum number of peaks. When the number of peaks exceeds `num_peaks`, return `num_peaks` peaks based on highest peak intensity. footprint : ndarray of bools, optional If provided, `footprint == 1` represents the local region within which to search for peaks at every point in `image`. Overrides `min_distance` (also for `exclude_border`). labels : ndarray of ints, optional If provided, each unique region `labels == value` represents a unique region to search for peaks. Zero is reserved for background. Returns ------- output : ndarray or ndarray of bools * If `indices = True` : (row, column, ...) coordinates of peaks. * If `indices = False` : Boolean array shaped like `image`, with peaks represented by True values. Notes ----- The peak local maximum function returns the coordinates of local peaks (maxima) in an image. A maximum filter is used for finding local maxima. This operation dilates the original image. After comparison of the dilated and original image, this function returns the coordinates or a mask of the peaks where the dilated image equals the original image. Examples -------- >>> img1 = np.zeros((7, 7)) >>> img1[3, 4] = 1 >>> img1[3, 2] = 1.5 >>> img1 array([[ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 1.5, 0. , 1. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ]]) >>> peak_local_max(img1, min_distance=1) array([[3, 2], [3, 4]]) >>> peak_local_max(img1, min_distance=2) array([[3, 2]]) >>> img2 = np.zeros((20, 20, 20)) >>> img2[10, 10, 10] = 1 >>> peak_local_max(img2, exclude_border=0) array([[10, 10, 10]]) """ if type(exclude_border) == bool: exclude_border = min_distance if exclude_border else 0 out = np.zeros_like(image, dtype=np.bool) # In the case of labels, recursively build and return an output # operating on each label separately if labels is not None: label_values = np.unique(labels) # Reorder label values to have consecutive integers (no gaps) if np.any(np.diff(label_values) != 1): mask = labels >= 1 labels[mask] = 1 + rank_order(labels[mask])[0].astype(labels.dtype) labels = labels.astype(np.int32) # New values for new ordering label_values = np.unique(labels) for label in label_values[label_values != 0]: maskim = (labels == label) out += peak_local_max(image * maskim, min_distance=min_distance, threshold_abs=threshold_abs, threshold_rel=threshold_rel, exclude_border=exclude_border, indices=False, num_peaks=np.inf, footprint=footprint, labels=None) if indices is True: return np.transpose(out.nonzero()) else: return out.astype(np.bool) if np.all(image == image.flat[0]): if indices is True: return np.empty((0, 2), np.int) else: return out # Non maximum filter if footprint is not None: image_max = ndi.maximum_filter(image, footprint=footprint, mode='constant') else: size = 2 * min_distance + 1 image_max = ndi.maximum_filter(image, size=size, mode='constant') mask = image == image_max if exclude_border: # zero out the image borders for i in range(mask.ndim): mask = mask.swapaxes(0, i) remove = (footprint.shape[i] if footprint is not None else 2 * exclude_border) mask[:remove // 2] = mask[-remove // 2:] = False mask = mask.swapaxes(0, i) # find top peak candidates above a threshold thresholds = [] if threshold_abs is None: threshold_abs = image.min() thresholds.append(threshold_abs) if threshold_rel is not None: thresholds.append(threshold_rel * image.max()) if thresholds: mask_original = mask # save the local maxima's of the image thrs_coords = { } # dictionary holds the coordinates correspond for each threshold for threshold in thresholds[0]: mask = mask_original mask &= image > threshold # get coordinates of peaks coordinates = np.transpose(mask.nonzero()) if coordinates.shape[0] > num_peaks: intensities = image.flat[np.ravel_multi_index( coordinates.transpose(), image.shape)] idx_maxsort = np.argsort(intensities)[::-1] coordinates = coordinates[idx_maxsort][:num_peaks] if indices is True: thrs_coords[threshold] = coordinates else: nd_indices = tuple(coordinates.T) out[nd_indices] = True return out if thresholds and thrs_coords: return thrs_coords
#io.show() elif transform[0].lower() == "integral-blur": exitStr = "(applying integral_image)" img = img_as_ubyte(integral_image(img)) #io.imshow(img) #io.show() elif transform[0].lower() == "canny-edge": exitStr = "(applying canny-edge)" img = img_as_ubyte( canny(img, float(transform[1]), float(transform[2]), float(transform[3]))) #io.imshow(img) #io.show() elif transform[0].lower() == "rank-order": exitStr = "(applying rank_order)" img, _ = rank_order(img) #io.imshow(img) #io.show() #elif transform[0].lower() == "resize": # img = resize(img, <tuple>) elif transform[0].lower() == "sobel": exitStr = "(applying sobel)" img = img_as_ubyte(sobel(img)) #io.imshow(img) #io.show() elif transform[0].lower() == "erosion": exitStr = "(applying erosion)" img = img_as_ubyte(erosion(img, square(int(transform[1])))) #io.imshow(img) #io.show() elif transform[0].lower() == "threshold-adaptive":