def merge_cells(self, label_c1, label_c2, params, segments_manager, image_manager): """merges two cells""" label_c1 = int(label_c1) label_c2 = int(label_c2) print len(self.cells[str(label_c2)].outline) print len(self.cells[str(label_c2)].lines) self.cells[str(label_c2)].stats["Area"] = ( self.cells[str(label_c2)].stats["Area"] + self.cells[str(label_c1)].stats["Area"] ) self.cells[str(label_c2)].lines.extend(self.cells[str(label_c1)].lines) self.cells[str(label_c2)].merged_list.append(label_c1) self.cells[str(label_c2)].outline.extend(self.cells[str(label_c1)].outline) self.cells[str(label_c2)].stats["Neighbours"] = ( self.cells[str(label_c2)].stats["Neighbours"] + self.cells[str(label_c1)].stats["Neighbours"] - 2 ) del self.cells[str(label_c1)] rotations = cp.rotation_matrices(params.axial_step) self.cells[str(label_c2)].compute_axes(rotations, image_manager.mask.shape) self.cells[str(label_c2)].recompute_outline(segments_manager.labels) if len(self.cells[str(label_c2)].merged_list) > 0: self.cells[str(label_c2)].merged_with = "Yes" print len(self.cells[str(label_c2)].outline) print len(self.cells[str(label_c2)].lines) print len(self.original_cells[str(label_c2)].outline) print len(self.original_cells[str(label_c2)].lines)
def merge_cells(self, label_c1, label_c2, params, segments_manager, image_manager): """merges two cells""" label_c1 = int(label_c1) label_c2 = int(label_c2) self.cells[str(label_c2)].stats["Area"] = self.cells[str( label_c2)].stats["Area"] + self.cells[str(label_c1)].stats["Area"] self.cells[str(label_c2)].lines.extend(self.cells[str(label_c1)].lines) self.cells[str(label_c2)].merged_list.append(label_c1) self.cells[str(label_c2)].outline.extend( self.cells[str(label_c1)].outline) self.cells[str(label_c2)].stats["Neighbours"] = self.cells[str( label_c2)].stats["Neighbours"] + self.cells[str( label_c1)].stats["Neighbours"] - 2 del self.cells[str(label_c1)] rotations = cp.rotation_matrices(params.axial_step) self.cells[str(label_c2)].compute_axes(rotations, image_manager.mask.shape) self.cells[str(label_c2)].recompute_outline(segments_manager.labels) if len(self.cells[str(label_c2)].merged_list) > 0: self.cells[str(label_c2)].merged_with = "Yes"
def compute_cells(self, params, image_manager, segments_manager): """Creates a cell list that is stored on self.cells as a dict, where each cell id is a key of the dict. Also creates an overlay of the cells edges over both the base and fluor image. Requires the loading of the images and the computation of the segments""" self.cell_regions_from_labels(segments_manager.labels) rotations = cp.rotation_matrices(params.axial_step) self.compute_box_axes(rotations, image_manager.mask.shape) self.original_cells = deepcopy(self.cells) for k in self.cells.keys(): try: c = self.cells[k] if len(c.neighbours) > 0: bestneigh = max(c.neighbours.iterkeys(), key=(lambda key: c.neighbours[key])) bestinterface = c.neighbours[bestneigh] cn = self.cells[str(int(bestneigh))] if cp.check_merge(c, cn, rotations, bestinterface, image_manager.mask, params): self.merge_cells(c.label, cn.label, params, segments_manager, image_manager) except KeyError: print "Cell was already merged and deleted" for k in self.cells.keys(): cp.assign_cell_color(self.cells[k], self.cells, self.cell_colors) self.overlay_cells(image_manager)
def split_cells(self, label_c1, params, segments_manager, image_manager): """Splits a previously merged cell.""" merged_cells = self.cells[str(label_c1)].merged_list merged_cells.append(label_c1) del self.cells[str(label_c1)] rotations = cp.rotation_matrices(params.axial_step) for id in merged_cells: id = int(id) self.cells[str(id)] = deepcopy(self.original_cells[str(id)]) self.cells[str(id)].compute_axes(rotations, image_manager.mask.shape) self.cells[str(id)].recompute_outline(segments_manager.labels) if len(self.cells[str(id)].merged_list) == 0: self.cells[str(id)].merged_with = "No" for k in self.cells.keys(): cp.assign_cell_color(self.cells[k], self.cells, self.cell_colors)
def compute_cells(self, params, image_manager, segments_manager): """Creates a cell list that is stored on self.cells as a dict, where each cell id is a key of the dict. Also creates an overlay of the cells edges over both the base and fluor image. Requires the loading of the images and the computation of the segments""" self.cell_regions_from_labels(segments_manager.labels, params.imageloaderparams.pixel_size) rotations = cp.rotation_matrices( params.cellprocessingparams.axial_step) self.compute_box_axes(rotations, image_manager.mask.shape, params.imageloaderparams.pixel_size) self.original_cells = deepcopy(self.cells) for k in list(self.cells.keys()): try: c = self.cells[k] if len(c.neighbours) > 0: bestneigh = max(list(iter(c.neighbours.keys())), key=(lambda key: c.neighbours[key])) bestinterface = c.neighbours[bestneigh] cn = self.cells[str(int(bestneigh))] if cp.check_merge(c, cn, rotations, bestinterface, image_manager.mask, params.cellprocessingparams): self.merge_cells(c.label, cn.label, params, segments_manager, image_manager) except KeyError: print("Cell was already merged and deleted") for k in self.cells.keys(): cp.assign_cell_color(self.cells[k], self.cells, self.cell_colors, params.imageloaderparams.pixel_size) self.overlay_cells(image_manager)
def remove_sept_from_membrane(self, maskshape): """Method used to remove the pixels of the septum that were still in the membrane""" # get outline points of septum mask septum_outline = [] septum_mask = self.sept_mask septum_outline = self.get_outline_points(septum_mask) # compute box of the septum septum_box = self.compute_sept_box_fix(septum_outline, maskshape) # compute axis of the septum rotations = cp.rotation_matrices(5) points = np.asarray(septum_outline) # in two columns, x, y width = len(points) + 1 # no need to do more rotations, due to symmetry for rix in range(len(rotations) / 2 + 1): r = rotations[rix] nx0, ny0, nx1, ny1, nwidth = cp.bound_rectangle( np.asarray(np.dot(points, r))) if nwidth < width: width = nwidth x0 = nx0 x1 = nx1 y0 = ny0 y1 = ny1 angle = rix rotation = rotations[angle] # midpoints mx = (x1 + x0) / 2 my = (y1 + y0) / 2 # assumes long is X. This duplicates rotations but simplifies # using different algorithms such as brightness long = [[x0, my], [x1, my]] short = [[mx, y0], [mx, y1]] short = np.asarray(np.dot(short, rotation.T), dtype=np.int32) long = np.asarray(np.dot(long, rotation.T), dtype=np.int32) # check if axis fall outside area due to rounding errors bx0, by0, bx1, by1 = septum_box short[0] = cp.bounded_point(bx0, bx1, by0, by1, short[0]) short[1] = cp.bounded_point(bx0, bx1, by0, by1, short[1]) long[0] = cp.bounded_point(bx0, bx1, by0, by1, long[0]) long[1] = cp.bounded_point(bx0, bx1, by0, by1, long[1]) length = np.linalg.norm(long[1] - long[0]) width = np.linalg.norm(short[1] - short[0]) if length < width: dum = length length = width width = dum dum = short short = long long = dum # expand long axis to create a linmask bx0, by0 = long[0] bx1, by1 = long[1] h, w = self.sept_mask.shape linmask = np.zeros((h, w)) h, w = self.sept_mask.shape[0] - 2, self.sept_mask.shape[1] - 2 bin_factor = int(width) if bx1 - bx0 == 0: x, y = line(bx0, 0, bx0, w) linmask[x, y] = 1 try: linmask = morphology.binary_dilation( linmask, np.ones((bin_factor, bin_factor))).astype(float) except RuntimeError: bin_factor = 4 linmask = morphology.binary_dilation( linmask, np.ones((bin_factor, bin_factor))).astype(float) else: m = ((by1 - by0) / (bx1 - bx0)) b = by0 - m * bx0 if b < 0: l_y0 = 0 l_x0 = int(-b / m) if h * m + b > w: l_y1 = w l_x1 = int((w - b) / m) else: l_x1 = h l_y1 = int(h * m + b) elif b > w: l_y0 = w l_x0 = int((w - b) / m) if h * m + b < 0: l_y1 = 0 l_x1 = int(-b / m) else: l_x1 = h l_y1 = int((h - b) / m) else: l_x0 = 0 l_y0 = int(b) if m > 0: if h * m + b > w: l_y1 = w l_x1 = int((w - b) / m) else: l_x1 = h l_y1 = int(h * m + b) elif m < 0: if h * m + b < 0: l_y1 = 0 l_x1 = int(-b / m) else: l_x1 = h l_y1 = int(h * m + b) else: l_x1 = h l_y1 = int(b) x, y = line(l_x0, l_y0, l_x1, l_y1) linmask[x, y] = 1 try: linmask = morphology.binary_dilation( linmask, np.ones((bin_factor, bin_factor))).astype(float) except RuntimeError: bin_factor = 4 linmask = morphology.binary_dilation( linmask, np.ones((bin_factor, bin_factor))).astype(float) return img_as_float(linmask)
def remove_sept_from_membrane(self, maskshape): """Method used to remove the pixels of the septum that were still in the membrane""" # get outline points of septum mask septum_outline = [] septum_mask = self.sept_mask septum_outline = self.get_outline_points(septum_mask) # compute box of the septum septum_box = self.compute_sept_box_fix(septum_outline, maskshape) # compute axis of the septum rotations = cp.rotation_matrices(5) points = np.asarray(septum_outline) # in two columns, x, y width = len(points) + 1 # no need to do more rotations, due to symmetry for rix in range(len(rotations) / 2 + 1): r = rotations[rix] nx0, ny0, nx1, ny1, nwidth = cp.bound_rectangle(np.asarray(np.dot(points, r))) if nwidth < width: width = nwidth x0 = nx0 x1 = nx1 y0 = ny0 y1 = ny1 angle = rix rotation = rotations[angle] # midpoints mx = (x1 + x0) / 2 my = (y1 + y0) / 2 # assumes long is X. This duplicates rotations but simplifies # using different algorithms such as brightness long = [[x0, my], [x1, my]] short = [[mx, y0], [mx, y1]] short = np.asarray(np.dot(short, rotation.T), dtype=np.int32) long = np.asarray(np.dot(long, rotation.T), dtype=np.int32) # check if axis fall outside area due to rounding errors bx0, by0, bx1, by1 = septum_box short[0] = cp.bounded_point(bx0, bx1, by0, by1, short[0]) short[1] = cp.bounded_point(bx0, bx1, by0, by1, short[1]) long[0] = cp.bounded_point(bx0, bx1, by0, by1, long[0]) long[1] = cp.bounded_point(bx0, bx1, by0, by1, long[1]) length = np.linalg.norm(long[1] - long[0]) width = np.linalg.norm(short[1] - short[0]) if length < width: dum = length length = width width = dum dum = short short = long long = dum # expand long axis to create a linmask bx0, by0 = long[0] bx1, by1 = long[1] h, w = self.sept_mask.shape linmask = np.zeros((h, w)) h, w = self.sept_mask.shape[0] - 2, self.sept_mask.shape[1] - 2 bin_factor = int(width) if bx1 - bx0 == 0: x, y = line(bx0, 0, bx0, w) linmask[x, y] = 1 try: linmask = morphology.binary_dilation(linmask, np.ones((bin_factor, bin_factor))).astype(float) except RuntimeError: bin_factor = 4 linmask = morphology.binary_dilation(linmask, np.ones((bin_factor, bin_factor))).astype(float) else: m = (by1 - by0) / (bx1 - bx0) b = by0 - m * bx0 if b < 0: l_y0 = 0 l_x0 = int(-b / m) if h * m + b > w: l_y1 = w l_x1 = int((w - b) / m) else: l_x1 = h l_y1 = int(h * m + b) elif b > w: l_y0 = w l_x0 = int((w - b) / m) if h * m + b < 0: l_y1 = 0 l_x1 = int(-b / m) else: l_x1 = h l_y1 = int((h - b) / m) else: l_x0 = 0 l_y0 = int(b) if m > 0: if h * m + b > w: l_y1 = w l_x1 = int((w - b) / m) else: l_x1 = h l_y1 = int(h * m + b) elif m < 0: if h * m + b < 0: l_y1 = 0 l_x1 = int(-b / m) else: l_x1 = h l_y1 = int(h * m + b) else: l_x1 = h l_y1 = int(b) x, y = line(l_x0, l_y0, l_x1, l_y1) linmask[x, y] = 1 try: linmask = morphology.binary_dilation(linmask, np.ones((bin_factor, bin_factor))).astype(float) except RuntimeError: bin_factor = 4 linmask = morphology.binary_dilation(linmask, np.ones((bin_factor, bin_factor))).astype(float) return img_as_float(linmask)