def run(src): subdirs = get_files_to_process(src) # print(subdirs) for dir in subdirs: for file in dir["files"]: image = read_file(file) backdrop = detect_backdrop(image) crop = crop_left_of_blue_line_hsv(image, backdrop=backdrop, visualize=True) filename = file.split("/")[-1] write_file(crop, "/Users/creimers/Downloads", filename)
def draw_black_box(target, old=False): outdir = get_target_dir(target["path"], "black-box", True) for file in target["files"]: image = read_file(file) backdrop = detect_backdrop(image) if backdrop == "white": image = trim_tape_edges(image) with_blue_line = crop_black_tape(image, backdrop, True) filename = file.split("/")[-1] outfile = os.path.join(outdir, filename) print(outfile) cv2.imwrite(outfile, with_blue_line)
def draw_blue_line(target, old=False): outdir = get_target_dir(target["path"], "blue-line", True) for file in target["files"]: filename = file.split("/")[-1] outfile = os.path.join(outdir, filename) print(filename) try: image = read_file(file) backdrop = detect_backdrop(image) if backdrop == "white": image = trim_tape_edges(image) with_blue_line = crop_black_tape(image, backdrop, False) with_blue_line = crop_left_of_blue_line_hsv( with_blue_line, backdrop, old, True) # outfile = os.path.join(outdir, filename) # print(outfile) cv2.imwrite(outfile, with_blue_line) except Exception as e: print(e) click.secho(file, fg="red")
def create_mask_overlay(image, smoothen=0, old=False, no_black_tape=False): """ create binary mask and put if over the original image inspiration: https://www.pyimagesearch.com/2016/03/07/transparent-overlays-with-opencv/ Args: image (list): the original image as np array, cropped left of the blue line smoothen (bool): smoothen edges, y/n """ backdrop = detect_backdrop(image) index_threshold, grey_threshold = get_threshold_values(backdrop, old) if backdrop == "white" and no_black_tape is False: image = trim_tape_edges(image) if no_black_tape is False: crop = crop_black_tape(image, backdrop) crop = crop_left_of_blue_line_hsv(crop, backdrop, old) # return crop overlay = crop.copy() output = crop.copy() # do this because the binary functions also do it white_row = np.zeros((12, overlay.shape[1]), dtype=np.uint8) + 1 white_row = cv2.merge((white_row, white_row, white_row)) * 255 overlay = np.vstack([white_row, overlay, white_row]) output = np.vstack([white_row, output, white_row]) binary_by_thresh = create_binary_mask_by_thresh( crop, backdrop, smoothen=smoothen, threshold=grey_threshold ) if backdrop == "white": binary_by_index = create_binary_mask_by_index( crop, threshold=index_threshold, smoothen=smoothen ) binary_combined = binary_by_index | binary_by_thresh contour = get_carrot_contour(binary_combined) else: contour = get_carrot_contour(binary_by_thresh) if backdrop == "white": overlay_color = (255, 0, 0) else: overlay_color = (0, 255, 0) alpha = 0.35 cv2.drawContours(overlay, [contour], -1, overlay_color, -1) cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output) c = contour extLeft = tuple(c[c[:, :, 0].argmin()][0]) # we don't need the extreme right point # extRight = tuple(c[c[:, :, 0].argmax()][0]) extTop = tuple(c[c[:, :, 1].argmin()][0]) extBot = tuple(c[c[:, :, 1].argmax()][0]) # crop at max points x = min(extLeft[0], extTop[0], extBot[0]) y1 = min(extLeft[1], extTop[1], extBot[1]) y2 = max(extLeft[1], extTop[1], extBot[1]) buffer = 25 mask = output[y1 - buffer : y2 + buffer, x - buffer :] return mask
def create_binary_mask( image, smoothen=0, minimize=True, old=False, no_black_tape=False ): """ create a binary mask of the carrot image """ backdrop = detect_backdrop(image) index_threshold, grey_threshold = get_threshold_values(backdrop, old) if backdrop == "white" and no_black_tape is False: crop = trim_tape_edges(image) else: crop = image if no_black_tape is False: crop = crop_black_tape(image, backdrop) crop = crop_left_of_blue_line_hsv(crop, backdrop, old) binary_by_thresh = create_binary_mask_by_thresh( crop, backdrop, threshold=grey_threshold, smoothen=smoothen ) if backdrop == "white": binary_by_index = create_binary_mask_by_index( crop, threshold=index_threshold, smoothen=smoothen ) binary_combined = binary_by_index | binary_by_thresh contour = reduce_to_contour(binary_combined, minimize=minimize) else: contour = reduce_to_contour(binary_by_thresh, minimize=minimize) # find gaps in first shoulder column and fill them trans = contour.T trans_reverse = trans[::-1] last_white = None for i, col in enumerate(trans_reverse): white_pixels = count_white_pixels(col) if white_pixels: last_white = i * -1 break if last_white is not None: for i in range(1, 8): column = contour[:, last_white - i].copy() column_filled = ndimage.binary_fill_holes(column).astype(int) * 255 contour[:, last_white - i] = column_filled # ensure shoulder symmetry # as few magic numbers as possible columns_to_consider = 2 length_difference = 0.9 for i in range(columns_to_consider, 0, -1): column = contour[:, last_white - i].copy() prev_column = contour[:, last_white - i - 1].copy() white_pixels = count_white_pixels(column) white_pixels_prev = count_white_pixels(prev_column) if white_pixels <= white_pixels_prev * length_difference: # this column first_white_pixel_col = column.argmax() last_white_pixel_col = len(column) - 2 - column[::-1].argmax() # prev column first_white_pixel_prev_col = prev_column.argmax() last_white_pixel_prev_col = ( len(prev_column) - 2 - prev_column[::-1].argmax() ) # where's the gap? start = abs(first_white_pixel_col - first_white_pixel_prev_col) end = abs(last_white_pixel_col - last_white_pixel_prev_col) if start < end: # start at start pixel = white_pixels_prev - start * 2 - 2 column[first_white_pixel_col : first_white_pixel_col + pixel] = 255 contour[:, last_white - i] = column else: # start at end pixel = white_pixels_prev - end * 2 - 2 column[last_white_pixel_col - pixel : last_white_pixel_col] = 255 contour[:, last_white - i] = column contour = reduce_to_contour(contour, minimize=False) # find and eliminate "empty" bins at right edge of image shoulder_index = get_index_of_shoulder(contour.T) cropped = contour[:, :shoulder_index] return cropped