def experiement_clahe_and_entropy(file=None): images = image_generator() if file is not None: images = add_image_to_image_generator(images, file) claheizer = cv2.createCLAHE() for fn, im in images: image_arrays = [] titles = [] # plot original image titles.append("Original") image_arrays.append(im) # clahe clahe = cv2.cvtColor(im.astype("uint8"), cv2.COLOR_RGB2Lab) tmp = clahe.copy() tmp[:, :, 0] = claheizer.apply(clahe[:, :, 0]) clahe = cv2.cvtColor(tmp.astype("uint8"), cv2.COLOR_Lab2RGB) titles.append("CLAHE") image_arrays.append(clahe) tmp = cv2.cvtColor(clahe, cv2.COLOR_RGB2Lab) ent = skimage.filters.rank.entropy(tmp[:, :, 0], skimage.morphology.disk(5)) titles.append("Entropy L") image_arrays.append(ent) # plot subplot_images(image_arrays, titles=titles, show_plot=True, suptitle=fn.split("/")[-1])
def experiment_yen_mask_rescale(file=None): images = image_generator() if file is not None: images = add_image_to_image_generator(images, file) claheizer = cv2.createCLAHE() for fn, im in images: image_arrays = [] titles = [] # plot original image titles.append("Original") image_arrays.append(im) # thresholded yen = threshold_yen(im) thresholded = threshold_by_channel(im, yen)[0] titles.append("Yen Thresholded") image_arrays.append(thresholded) # masked and equalized masked = im.copy() masked[thresholded <= 0] = 0 titles.append("Masked") image_arrays.append(masked) # equalize from skimage.util import img_as_ubyte equalized = masked.copy() for chan in range(1, 3): img = masked[:, :, chan].astype("uint8") rng = (0, 175) if chan == 1 else (175, 255) rescaled = exposure.rescale_intensity(img, in_range=rng) equalized[:, :, chan] = rescaled break titles.append("Equalied") image_arrays.append(equalized) # plot subplot_images(image_arrays, titles=titles, show_plot=True, suptitle=fn.split("/")[-1])
def experiement_segment_crop_gabor(file=None, dilation_iterations=40, num_regions=4): images = image_generator() if file is not None: images = add_image_to_image_generator(images, file) gabor_filters = build_gabor_filters() for fn, im in images: image_arrays = [] titles = [] # plot original image titles.append("Original Image") image_arrays.append(im) # segmented image segmented = segment_image(im, n_segments=20, compactness=20, sigma=2) titles.append("Segmented Image") image_arrays.append(segmented) # re-segment image brightest_colors = extract_brightest_colors(segmented, thresh=3.5) bright_regions = select_colors(segmented, brightest_colors) resegmented = segmented.copy() resegmented[bright_regions <= 0] = 0 resegmented = segment_image(resegmented, n_segments=5, compactness=30, sigma=2) titles.append("Re-Segmented Image") image_arrays.append(resegmented) colors = list_colors_exclude_black(segmented)[[0, 4, 8, 12, 16]] rgb_crops = [] rgb_labels = [] crop_titles = [] for color in list(colors): mask = select_colors(segmented, [color]) > 0 x, y = ndimage.measurements.center_of_mass((mask[:, :, 0] > 0).astype(int)) xmin = int(max(x - 50, 0)) xmax = int(min(x + 50, im.shape[0] - 1)) ymin = int(max(y - 50, 0)) ymax = int(min(y + 50, im.shape[1] - 1)) rgb_crops.append(im[xmin:xmax, ymin:ymax, :].copy()) rgb_labels.append(str(color)) crop_titles.append("Crop for Color " + str(color)) subplot_images(rgb_crops, titles=crop_titles, suptitle="Crops") for crop, color in zip(rgb_crops, rgb_labels): rgb_gabor = apply_gabor_filters(crop, gabor_filters) subplot_images(rgb_gabor, suptitle="RGB Gabor Filters for Color " + color) subplot_images(image_arrays, titles=titles, suptitle=fn.split("/")[-1], show_plot=True)
def experiment_yen_distort_reds(file=None): images = image_generator() if file is not None: images = add_image_to_image_generator(images, file) for fn, im in images: image_arrays = [] titles = [] # plot original image titles.append("Original") image_arrays.append(im) yen = threshold_yen(im) thresholded = threshold_by_channel(im, yen)[0] titles.append("Yen Thresholded") image_arrays.append(thresholded) # equalize equalized = np.zeros_like(im) for chan in range(3): equalized[:, :, chan] = (exposure.equalize_hist(im[:, :, chan]) * 255).astype("uint8") titles.append("Equalized") image_arrays.append(equalized) # experiments point to a reasonable threshold of 10 or 20 thresh = 140 # TUNE: 160 reds_ycbcr = extract_brightest_reds(equalized, thresh=thresh, verbose=False) titles.append("Reds Thresholded YCBCR %d" % thresh) image_arrays.append(reds_ycbcr) filled_ycbcr = reds_ycbcr.copy() mask = ndimage.binary_fill_holes((reds_ycbcr[:, :, 0] > 0)) # mask = ndimage.binary_fill_holes((np.amax(reds_ycbcr, axis=2) > 0)) filled_ycbcr[mask, :] = np.array([255, 0, 0]) titles.append("Filled YCBCR Reds") image_arrays.append(filled_ycbcr) smoothed_ycbcr = reds_ycbcr.copy() smoothed_ycbcr[:, :, 0] = binary_closing(smoothed_ycbcr[:, :, 0] > 0, iterations=3) * 255 mask = np.amax(smoothed_ycbcr, axis=2) > 0 structure = calculate_binary_opening_structure(mask, weight=1, hollow=False) mask = binary_opening(mask, structure=structure, iterations=1) smoothed_ycbcr[mask <= 0, :] = 0 titles.append("Smoothed YCBCR Reds") image_arrays.append(smoothed_ycbcr) # plot subplot_images(image_arrays, titles=titles, show_plot=True, suptitle=fn.split("/")[-1]) continue # segmented image segmented = segment_image(smoothed_ycbcr, n_segments=3, compactness=100, sigma=2) titles.append("Segmented") image_arrays.append(segmented) titles.append("Least Gray Bright Region") image_arrays.append(bright) d, m, b = extract_colors_grays(segmented, n_grays=6) grays = select_colors(segmented, [d, m, b]) titles.append("Gray Regions of Varied Intensity") image_arrays.append(grays) quantized = quantize_colors( segmented, n_colors=3, n_samples=1000, max_iter=300, n_init=10, n_jobs=1, random_state=SEED, verbose=True, split=False, ) titles.append("Quantized Image %d Colors" % 3) image_arrays.append(quantized) quantized = quantize_colors( segmented, n_colors=4, n_samples=1000, max_iter=300, n_init=10, n_jobs=1, random_state=SEED, verbose=True, split=False, ) titles.append("Quantized Image %d Colors" % 4) image_arrays.append(quantized) quantized = quantize_colors( segmented, n_colors=5, n_samples=1000, max_iter=300, n_init=10, n_jobs=1, random_state=SEED, verbose=True, split=False, ) titles.append("Quantized Image %d Colors" % 5) image_arrays.append(quantized) subplot_images(image_arrays, titles=titles, show_plot=True, suptitle=fn.split("/")[-1])
def experiment_yen_distort_reds_entropy_blues(file=None): images = image_generator() if file is not None: images = add_image_to_image_generator(images, file) for fn, im in images: image_arrays = [] titles = [] # plot original image titles.append("Original") image_arrays.append(im) # threshold yen = threshold_yen(im) thresholded = threshold_by_channel(im, yen)[0] titles.append("Yen Thresholded") image_arrays.append(thresholded) # distort equalized = np.zeros_like(im, dtype="uint8") for chan in range(3): equalized[:, :, chan] = (exposure.equalize_hist(im[:, :, chan]) * 255).astype("uint8") # entropy ent = skimage.filters.rank.entropy(equalized[:, :, 0], skimage.morphology.disk(3)) titles.append("Entropy") image_arrays.append(ent) # thresholded thresh = 1.5 blues = np.zeros_like(im, dtype="uint8") blues[ent > thresh, :] = 0 blues[ent <= thresh] = 255 titles.append("Entropy Blues Thresholded %d" % thresh) image_arrays.append(blues) # reds thresh = 160 reds = extract_brightest_reds(equalized, thresh=thresh, verbose=False) titles.append("Reds Thresholded YCBCR %d" % thresh) image_arrays.append(reds) # union union = np.zeros_like(im) union[thresholded > 0] = 255 union[reds > 0] = 255 union[blues > 0] = 255 titles.append("Union") image_arrays.append(union) for name, color_image in zip(["yen", "blues", "reds", "union"], [thresholded, blues, reds, union]): binary_image = color_image[:, :, 0] > 0 structure = calculate_binary_opening_structure(binary_image) binary_image = binary_opening(binary_image, structure=structure) binary_image = binary_dilation(binary_image, iterations=3) if name == "yen": regions, labels = extract_largest_regions(binary_image, num_regions=2) else: regions, labels = extract_largest_regions(binary_image, num_regions=3) mask = convex_hull_mask(regions > 0, mask=True) print(mask.shape) crop = np.zeros_like(im) crop[mask > 0, :] = im[mask > 0, :] titles.append(name.title() + "Region") image_arrays.append(crop) # plot subplot_images( image_arrays, titles=titles, # show_plot=True, save_plot=True, suptitle="crop-" + fn.split("/")[-1], )
def experiment_yen_reds_fit_region(file=None): images = image_generator() if file is not None: images = add_image_to_image_generator(images, file) for fn, im in images: print(fn) image_arrays = [] titles = [] # plot original image titles.append("Original") image_arrays.append(im) # threshold yen = threshold_yen(im) thresholded = threshold_by_channel(im, yen)[0] titles.append("Yen Thresholded") image_arrays.append(thresholded) # reds equalized = np.zeros_like(im, dtype="uint8") for chan in range(3): equalized[:, :, chan] = (exposure.equalize_hist(im[:, :, chan]) * 255).astype("uint8") # reds thresh = 160 reds = extract_brightest_reds(equalized, thresh=thresh, verbose=False) titles.append("Reds Thresholded YCBCR %d" % thresh) image_arrays.append(reds) # encode encoded = np.zeros_like(im[:, :, 0], dtype="uint8") * -1 encoded[thresholded[:, :, 0] > 0] = 2 encoded[np.amax(reds, axis=2) > 0] = 1 encoded = encoded.astype("uint8") titles.append("Encoded") image_arrays.append(encoded) print("fitting reference frame") best_loc, best_val = None, 0 for x in range(100, 1000, 100): print("x = ", x) for y in range(3 * x // 2, 1500, 100): print("y = ", y) for h in range(y // 3 - 1, y, y // 3): for z in range(x // 3 - 1, x, x // 3): for i in range(1, 4): theta = i * np.pi / 3 reference = draw_reference_frame(x, y, z, h, theta) res = cv2.matchTemplate(encoded, reference, cv2.TM_SQDIFF) _, max_val, _, max_loc = cv2.minMaxLoc(res) if max_val > best_val: best_val = max_val best_loc = max_loc print(x, y, z, h, np.degrees(theta)) print(max_val, best_loc) print("\n") # plot subplot_images( image_arrays, titles=titles, # show_plot=True, save_plot=True, suptitle="crop-" + fn.split("/")[-1], )