def calculate_distance_map(self, img_luv, target_distribution): """Calculates a distance map by sliding a window by a given stride""" distances = np.empty( dtype=np.uint8, shape=(int((img_luv.shape[0] - self.window_size) / self.stride), int((img_luv.shape[1] - self.window_size) / self.stride))) for x in range(distances.shape[1]): for y in range(distances.shape[0]): x_px = x * self.stride y_px = y * self.stride local_distribution = Utils.calculate_distribution( img_luv[y_px:y_px + self.window_size, x_px:x_px + self.window_size]) distance = Utils.bhattacharyya_distance( local_distribution, target_distribution) # clamp & check distance if np.isnan(distance): print("Distance calculated was NaN! Setting to 255.") distance = 255 if distance > 255: distance = 255 if distance < 0: distance = 0 distances[y, x] = int(distance) # DEBUG self.distances_img = self.distance_map_to_img(distances, img_luv) return distances
def segment(self, img_original, target_distribution_rect): """Returns a quad or None if segmentation failed""" # DEBUG self.img_original = img_original # norm, preprocess, luv img_normalized = self.normalize(img_original) img_preprocessed = self.preprocess(img_normalized) img_luv = cv2.cvtColor(img_preprocessed, cv2.COLOR_BGR2Luv) # target distribution r = target_distribution_rect.scale(self.scale_factor).floor() target_distribution = Utils.calculate_distribution( img_luv[r.a[1]:r.b[1], r.a[0]:r.b[0]]) # distance map distances = self.calculate_distance_map(img_luv, target_distribution) # region region = self.get_receipt_pixels(distances) if region is None: return None # quad quad = self.extract_quad(region) return quad
def small_distribution(self): """Calculates distribution of the small ROI""" r = self.small_roi_rect().floor() img = self.img[r.a[1]:r.b[1], r.a[0]:r.b[0]] img_luv = cv2.cvtColor(img, cv2.COLOR_BGR2Luv) return Utils.calculate_distribution(img_luv)
def distribution(self): """Calculates the distribution of the selected region of interest""" img_luv = cv2.cvtColor(self.distribution_img, cv2.COLOR_BGR2Luv) return Utils.calculate_distribution(img_luv)