def run_segmentation(self, display=False): # mask = self.data_term(find_all=False) # mask = self.seeds.copy() # strel = skimor.disk(2) # ballM = np.zeros((2 * self.ball_r + 1, 2 * self.ball_r + 1, 2), dtype=np.int) # ballM[:, :, 1] = np.tile(np.arange(-self.ball_r, self.ball_r + 1), [2 * self.ball_r + 1, 1]) # ballM[:, :, 0] = ballM[:, :, 1].conj().transpose() # # ballMOut = np.zeros((2 * self.ball_r_out + 1, 2 * self.ball_r_out + 1, 2), dtype=np.int) # ballMOut[:, :, 1] = np.tile(np.arange(-self.ball_r_out, self.ball_r_out + 1), [2 * self.ball_r_out + 1, 1]) # ballMOut[:, :, 0] = ballMOut[:, :, 1].conj().transpose() # self.segmentation = self.segmentInnerBoundary(self.energy, mask, ballM, strel, self.min_diff, self.alpha, display) # self.membraneMask = skimor.binary_closing(self.membraneMask, strel) if self.smoothing: self.im = tools.smoothing(self.im, sigmaSpace=5, sigmaColor=5, sliceId=0) if scale != 1: self.im = tools.resize3D(self.im, self.scale, sliceId=0) self.seeds = tools.resize3D(self.seeds, self.scale, sliceId=0) self.segmentation = tools.resize3D(self.segmentation, self.scale, sliceId=0) newbies = self.seeds.copy() curr_it = 0 changed = True while changed and curr_it < self.max_iters: curr_it += 1 print 'iteration #%i/%i' % (curr_it, self.max_iters) # mask, accepted, refused = self.iterationIB( im, mask, ballM, strel, minDiff, alpha ) mask_new, accepted, refused = self.iteration_IB(newbies) # plt.figure() # plt.subplot(121), plt.imshow(self.segmentation[0,...].copy(), 'gray') # plt.subplot(122), plt.imshow(mask_new[0,...].copy(), 'gray') # if (curr_it % 2) == 0: # plt.show() # newbies = mask_new - self.segmentation # mask = mask_new self.segmentation = mask_new # self.newbies = np.zeros( self.newbies.shape, dtype=np.bool ) # self.newbies[accepted[0],accepted[1]] = True if not accepted.any(): changed = False for i, im in enumerate(self.segmentation): self.segmentation[i,...] = skimor.binary_closing(im, np.ones((3, 3))) # self.segmentation[i,...] = scindimor.binary_closing(self.segmentation, np.ones((3, 3, 3))) # rescale the segmentation to original shape if scale != 1: tmp = np.zeros(self.orig_shape) for i, im in enumerate(self.segmentation): tmp[i,...] = cv2.resize(im.astype(np.uint8), (self.orig_shape[2], self.orig_shape[1])) self.segmentation = tmp
def morph_snakes(data, mask, slice=None, scale=0.5, alpha=1000, sigma=1, smoothing_ls=1, threshold=0.3, balloon=1, max_iters=50, show=False, show_now=True): data_o = data.copy() mask_o = mask.copy() if scale != 1: # data = skitra.rescale(data, scale=scale, preserve_range=True).astype(np.uint8) # mask = skitra.rescale(mask, scale=scale, preserve_range=True).astype(np.bool) data = tools.resize3D(data, scale, sliceId=0) mask = tools.resize3D(mask, scale, sliceId=0) gI = morphsnakes.gborders(data, alpha=alpha, sigma=sigma) # Morphological GAC. Initialization of the level-set. mgac = morphsnakes.MorphGAC(gI, smoothing=smoothing_ls, threshold=threshold, balloon=balloon) mgac.levelset = mask mgac.run(iterations=max_iters) seg = mgac.levelset if scale != 1: # data = tools.resize_ND(data, shape=orig_shape) # mask = tools.resize_ND(mask, shape=orig_shape) seg = tools.resize_ND(seg, shape=data_o.shape) if show: tools.visualize_seg(data_o, seg, mask_o, slice=slice, title='morph snakes', show_now=show_now) return data, mask, seg
def initialize_graycom_deprecated( data, slice=None, distances=[ 1, ], scale=0.5, angles=[0, np.pi / 4, np.pi / 2, 3 * np.pi / 4], symmetric=False, c_t=5, show=False, show_now=True): if scale != 1: data = tools.resize3D(data, scale, sliceId=0) # computing gray co-occurence matrix print 'Computing gray co-occurence matrix ...', if data.ndim == 2: gcm = skifea.greycomatrix(data, distances, angles, symmetric=symmetric) # summing over distances and directions gcm = gcm.sum(axis=3).sum(axis=2) else: gcm = tools.graycomatrix_3D(data, connectivity=1) print 'done' # plt.figure() # plt.subplot(121), plt.imshow(gcm, 'jet', vmax=10 * gcm.mean()) # plt.subplot(122), plt.imshow(gcm2, 'jet', vmax=10 * gcm.mean()) # plt.show() # ---- # gcm2 = skifea.greycomatrix(data[10,...], distances, angles, symmetric=symmetric).sum(axis=3).sum(axis=2) # plt.figure() # plt.subplot(121), plt.imshow(gcm, 'jet', vmax=10 * gcm.mean()), plt.title('3D gcm') # plt.subplot(122), plt.imshow(gcm2, 'jet', vmax=10 * gcm2.mean()), plt.title('gcm of a slice') # plt.show() # ---- # plt.figure() # thresh = np.mean(gcm) # ts = (thresh * np.array([1, 3, 6, 9, 12])).astype(int) # for i, t in enumerate(ts): # plt.subplot(100 + 10 * len(ts) + i + 1) # plt.imshow(gcm > t, 'gray') # plt.title('t = %i' % t) # plt.show() # thresholding graycomatrix (GCM) thresh = c_t * np.mean(gcm) gcm_t = gcm > thresh gcm_to = skimor.binary_opening(gcm_t, selem=skimor.disk(3)) # find peaks in the GCM and return them as random variables rvs = analyze_glcm(gcm_to) if slice is not None: data = data[slice, ...] # deriving seed points seeds = np.zeros(data.shape, dtype=np.uint8) best_probs = np.zeros( data.shape ) # assign the most probable label if more than one are possible for i, rv in enumerate(rvs): probs = rv.pdf(data) s = probs > probs.mean() # probability threshold # sfh = skimor.remove_small_holes(s, min_size=0.1 * s.sum(), connectivity=2) s = tools.fill_holes_watch_borders(s) # s = skimor.binary_opening(s, selem=skimor.disk(3)) # plt.figure() # plt.subplot(131), plt.imshow(s, 'gray') # plt.subplot(132), plt.imshow(sfh, 'gray') # plt.subplot(133), plt.imshow(sfh2, 'gray') # plt.show() s = np.where((probs * s) > (best_probs * s), i + 1, s) # assign new label only if its probability is higher best_probs = np.where(s, probs, best_probs) # update best probs seeds = np.where(s, i + 1, seeds) # update seeds # # running Grow cut # gc = growcut.GrowCut(data, seeds, smooth_cell=False, enemies_T=0.7) # gc.run() # # postprocessing labels # labs = gc.get_labeled_im().astype(np.uint8)[0,...] # labs_f = scindifil.median_filter(labs, size=5) labs_f = scindifil.median_filter(seeds, size=3) # plt.figure() # plt.subplot(131), plt.imshow(gcm, 'gray', vmax=gcm.mean()), plt.title('gcm') # plt.subplot(132), plt.imshow(gcm_t, 'gray'), plt.title('thresholded') # plt.subplot(133), plt.imshow(gcm_to, 'gray'), plt.title('opened') # # plt.figure() # plt.subplot(121), plt.imshow(data, 'gray', interpolation='nearest'), plt.title('input') # plt.subplot(122), plt.imshow(seeds, 'jet', interpolation='nearest'), plt.title('seeds') # divider = make_axes_locatable(plt.gca()) # cax = divider.append_axes('right', size='5%', pad=0.05) # plt.colorbar(cax=cax, ticks=np.unique(seeds)) # plt.figure() # plt.subplot(141), plt.imshow(data, 'gray'), plt.title('input') # plt.subplot(142), plt.imshow(labs_f, 'gray'), plt.title('labels') # plt.subplot(143), plt.imshow(liver_blob, 'gray'), plt.title('liver blob') # plt.subplot(144), plt.imshow(init_mask, 'gray'), plt.title('init mask') # finding liver blob liver_blob = find_liver_blob(data, labs_f, slice=slice, show=show, show_now=show_now) # hole filling - adding (and then removing) a capsule of zeros, otherwise it'd fill holes touching image borders init_mask = tools.fill_holes_watch_borders(liver_blob) # init_mask = np.zeros([x + 2 for x in liver_blob.shape], dtype=np.uint8) # if liver_blob.ndim == 3: # for i, im in enumerate(liver_blob): # init_mask[i + 1, 1:-1, 1:-1] = im # else: # init_mask[1:-1, 1:-1] = liver_blob # init_mask = skimor.remove_small_holes(init_mask, min_size=0.1 * liver_blob.sum(), connectivity=2) # if liver_blob.ndim == 3: # init_mask = init_mask[1:-1, 1:-1, 1:-1] # else: # init_mask = init_mask[1:-1, 1:-1] # visualization if show: if slice is None: slice = 0 data_vis = data if data.ndim == 2 else data[slice, ...] seeds_vis = seeds if data.ndim == 2 else seeds[slice, ...] labs_f_vis = labs_f if data.ndim == 2 else labs_f[slice, ...] liver_blob_vis = liver_blob if data.ndim == 2 else liver_blob[slice, ...] init_mask_vis = init_mask if data.ndim == 2 else init_mask[slice, ...] plt.figure() plt.subplot(131), plt.imshow(gcm, 'gray', vmax=gcm.mean()), plt.title('gcm') plt.subplot(132), plt.imshow(gcm_t, 'gray'), plt.title('thresholded') plt.subplot(133), plt.imshow(gcm_to, 'gray'), plt.title('opened') plt.figure() plt.subplot(121), plt.imshow( data_vis, 'gray', interpolation='nearest'), plt.title('input') plt.subplot(122), plt.imshow( seeds_vis, 'jet', interpolation='nearest'), plt.title('seeds') divider = make_axes_locatable(plt.gca()) cax = divider.append_axes('right', size='5%', pad=0.05) plt.colorbar(cax=cax, ticks=np.unique(seeds)) plt.figure() plt.subplot(141), plt.imshow(data_vis, 'gray'), plt.title('input') plt.subplot(142), plt.imshow(labs_f_vis, 'gray'), plt.title('labels') plt.subplot(143), plt.imshow(liver_blob_vis, 'gray'), plt.title('liver blob') plt.subplot(144), plt.imshow(init_mask_vis, 'gray'), plt.title('init mask') if show_now: plt.show() return data, init_mask
def run(self, resize=True): #---- rescaling ---- if resize and self.scale != 0: self.img = tools.resize3D(self.img_orig, self.scale, sliceId=0) self.seeds = tools.resize3D(self.seeds_orig, self.scale, sliceId=0) self.mask = tools.resize3D(self.mask_orig, self.scale, sliceId=0) # for i, (im, seeds, mask) in enumerate(zip(self.img_orig, self.seeds_orig, self.mask_orig)): # self.img[i, :, :] = cv2.resize(im, (0,0), fx=self.scale, fy=self.scale, interpolation=cv2.INTER_NEAREST) # self.seeds[i, :, :] = cv2.resize(seeds, (0,0), fx=self.scale, fy=self.scale, interpolation=cv2.INTER_NEAREST) # self.mask[i, :, :] = cv2.resize(mask, (0,0), fx=self.scale, fy=self.scale, interpolation=cv2.INTER_NEAREST) # else: # self.img = self.img_orig # self.seeds = self.seeds_orig self.n_slices, self.n_rows, self.n_cols = self.img.shape #---- calculating intensity models ---- # if self.unaries is None: if self.models is None: self._debug('calculating intensity models ...', False) # self.models = self.calc_intensity_models() self.models = self.calc_models() self._debug('done', True) #---- creating unaries ---- if self.unaries is None: self._debug('calculating unary potentials ...', False) self.unaries = self.beta * self.get_unaries() self._debug('done', True) #---- create potts pairwise ---- if self.pairwise is None: self._debug('calculating pairwise potentials ...', False) # self.pairwise = - self.alpha * np.eye(self.n_objects, dtype=np.int32) self.set_pairwise() self._debug('done', True) #---- deriving graph edges ---- self._debug('deriving graph edges ...', False) # use the gerneral graph algorithm # first, we construct the grid graph # inds = np.arange(self.n_rows * self.n_cols).reshape(self.img.shape) # horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()] # vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()] # self.edges = np.vstack([horz, vert]).astype(np.int32) inds = np.arange(self.img.size).reshape(self.img.shape) if self.img.ndim == 2: horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()] vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()] self.edges = np.vstack([horz, vert]).astype(np.int32) elif self.img.ndim == 3: horz = np.c_[inds[:, :, :-1].ravel(), inds[:, :, 1:].ravel()] vert = np.c_[inds[:, :-1, :].ravel(), inds[:, 1:, :].ravel()] dept = np.c_[inds[:-1, :, :].ravel(), inds[1:, :, :].ravel()] self.edges = np.vstack([horz, vert, dept]).astype(np.int32) # deleting edges with nodes outside the mask nodes_in = np.ravel_multi_index(np.nonzero(self.mask), self.img.shape) rows_inds = np.in1d(self.edges, nodes_in).reshape( self.edges.shape).sum(axis=1) == 2 self.edges = self.edges[rows_inds, :] self._debug('done', True) #---- calculating graph cut ---- self._debug('calculating graph cut ...', False) # we flatten the unaries result_graph = pygco.cut_from_graph( self.edges, self.unaries.reshape(-1, self.n_objects), self.pairwise) self.labels = result_graph.reshape(self.img.shape) self._debug('done', True) #---- zooming to the original size ---- if resize and self.scale != 0: # self.labels_orig = cv2.resize(self.labels, (0,0), fx=1. / self.scale, fy= 1. / self.scale, interpolation=cv2.INTER_NEAREST) self.labels_orig = tools.resize3D(self.labels, 1. / self.scale, sliceId=0) else: self.labels_orig = self.labels self._debug('----------', True) self._debug('segmentation done', True) # self.show_slice(0) # plt.figure() # plt.subplot(221), plt.imshow(self.img_orig[0, :, :], 'gray', interpolation='nearest'), plt.title('input image') # plt.subplot(222), plt.imshow(self.seeds_orig[0, :, :], interpolation='nearest') # # plt.hold(True) # # seeds_v = np.nonzero(self.seeds) # # for i in range(len(seeds_v[0])): # # seed = (seeds_v[0][i], seeds_v[1][i]) # # if self.seeds[seed] # # # # plt.plot # plt.title('seeds') # plt.subplot(223), plt.imshow(self.labels, interpolation='nearest'), plt.title('segmentation') # plt.subplot(224), plt.imshow(skiseg.mark_boundaries(self.img_orig, self.labels), interpolation='nearest'), plt.title('segmentation') # plt.show() return self.labels_orig