def _get_unif_points(rect, N, no_side=False): rect_w = rect.width() rect_h = rect.height() if not no_side: x_step = rect_h / (np.sqrt(N) - 1) y_step = rect_w / (np.sqrt(N) - 1) else: x_step = rect_h / (np.sqrt(N) + 1) y_step = rect_w / (np.sqrt(N) + 1) x_count = int(rect_h / x_step) y_count = int(rect_w / y_step) points = parts.Parts() if not no_side: x_from, y_from = 0, 0 x_to, y_to = x_count + 1, y_count + 1 else: x_from, y_from = 1, 1 x_to, y_to = x_count, y_count for i in range(x_from, x_to): for j in range(y_from, y_to): x = (rect.xmin + int(((i) * x_step))) y = (rect.ymin + int(((j) * y_step))) points.append(parts.Part(-1, '?', -1, y, x, 1)) return points
def _get_rand_points(rect, N): xs = np.random.uniform(low=rect.xmin + 1, high=rect.xmax - 1, size=N) ys = np.random.uniform(low=rect.ymin + 1, high=rect.ymax - 1, size=N) points = parts.Parts() for x, y in zip(xs, ys): points.append( parts.Part(-1, '?', -1, int(round(y)), int(round(x)), 1)) return points
def _get_norm_points(rect, N, var_x, var_y, clip=True): cent_x, cent_y = rect.center() xs = np.random.normal(loc=cent_x, scale=var_x, size=N) ys = np.random.normal(loc=cent_y, scale=var_y, size=N) points = parts.Parts() for x, y in zip(xs, ys): if rect.xmin <= x <= rect.xmax and rect.ymin <= y <= rect.ymax or not clip: points.append( parts.Part(-1, '?', -1, int(round(y)), int(round(x)), 1)) return points
def _get_bg_points(self, rect, seg, N): img_shape = seg.shape[:2] full_image_rect = Rect(0, img_shape[0], 0, img_shape[1]) # strategy can only be rand for unif, if set to norm then go with unif if self.point_gen_strategy == 'rand': points = self._get_rand_points(full_image_rect, N) elif self.point_gen_strategy in ['unif', 'norm']: points = self._get_unif_points(full_image_rect, N, True) else: raise KeyError('point generations strategy is not good: ', self.point_gen_strategy) # filter points for note being fpoints = parts.Parts() for point in points: if rect.xmin <= point.y <= rect.xmax and rect.ymin <= point.x <= rect.ymax: continue else: fpoints.append(point) return fpoints
def _get_part_points(self, rect, seg, N): if self.point_gen_strategy == 'rand': points = self._get_rand_points(rect, N) elif self.point_gen_strategy == 'unif': points = self._get_unif_points(rect, N) elif self.point_gen_strategy == 'norm': points = self._get_norm_points(rect, N, min(3, rect.height() / 5.), min(3, rect.width() / 5.), True) else: raise KeyError('point generations strategy is not good: ', self.point_gen_strategy) # filter points for being in seg fpoints = parts.Parts() for point in points: if point.is_inbound(seg.shape): if seg[point.y, point.x, 0]: fpoints.append(point) return fpoints