def axes_from_rotation(self, x0, y0, x1, y1, rotation): """ sets the cell axes from the box and the rotation """ # midpoints mx = (x1 + x0) / 2 my = (y1 + y0) / 2 # assumes long is X. This duplicates rotations but simplifies # using different algorithms such as brightness self.long_axis = [[x0, my], [x1, my]] self.short_axis = [[mx, y0], [mx, y1]] self.short_axis = \ np.asarray(np.dot(self.short_axis, rotation.T), dtype=np.int32) self.long_axis = \ np.asarray(np.dot(self.long_axis, rotation.T), dtype=np.int32) # check if axis fall outside area due to rounding errors bx0, by0, bx1, by1 = self.box self.short_axis[0] = \ cp.bounded_point(bx0, bx1, by0, by1, self.short_axis[0]) self.short_axis[1] = \ cp.bounded_point(bx0, bx1, by0, by1, self.short_axis[1]) self.long_axis[0] = \ cp.bounded_point(bx0, bx1, by0, by1, self.long_axis[0]) self.long_axis[1] = \ cp.bounded_point(bx0, bx1, by0, by1, self.long_axis[1]) self.stats["Length"] = \ np.linalg.norm(self.long_axis[1] - self.long_axis[0]) self.stats["Width"] = \ np.linalg.norm(self.short_axis[1] - self.short_axis[0])
def axes_from_rotation(self, x0, y0, x1, y1, rotation): """ sets the cell axes from the box and the rotation """ # midpoints mx = (x1 + x0) / 2 my = (y1 + y0) / 2 # assumes long is X. This duplicates rotations but simplifies # using different algorithms such as brightness self.long_axis = [[x0, my], [x1, my]] self.short_axis = [[mx, y0], [mx, y1]] self.short_axis = np.asarray(np.dot(self.short_axis, rotation.T), dtype=np.int32) self.long_axis = np.asarray(np.dot(self.long_axis, rotation.T), dtype=np.int32) # check if axis fall outside area due to rounding errors bx0, by0, bx1, by1 = self.box self.short_axis[0] = cp.bounded_point(bx0, bx1, by0, by1, self.short_axis[0]) self.short_axis[1] = cp.bounded_point(bx0, bx1, by0, by1, self.short_axis[1]) self.long_axis[0] = cp.bounded_point(bx0, bx1, by0, by1, self.long_axis[0]) self.long_axis[1] = cp.bounded_point(bx0, bx1, by0, by1, self.long_axis[1]) self.stats["Length"] = np.linalg.norm(self.long_axis[1] - self.long_axis[0]) self.stats["Width"] = np.linalg.norm(self.short_axis[1] - self.short_axis[0])
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)