def experiment_yen_threshold(image=None): images = image_generator() if image is not None: img_path = os.path.join(BASE_DIR, "data/imgs/") fn = os.path.join(img_path, image) im = load_images([fn])[0].astype("int32") images = itertools.chain([(fn, im)], images) for fn, im in images: imgs = [im] titles = ["Original"] print("computing yen threshold") yen = threshold_yen(im) channels = threshold_by_channel(im, yen) titles.append("Yen Thresholded") imgs.append(channels[0]) for cp in [175, 200, 225, 250]: titles.append("Cutpoint %d" % cp) print("cutting") rtn = np.zeros(im.shape) rtn[np.average(im, axis=2, weights=[0.7, 0.1, 0.2]) > cp, 0] = 255 for i in range(1, 3): rtn[:, :, i] = rtn[:, :, 0] imgs.append(rtn) print("plotting") plot_images(imgs, titles=titles, suptitle=fn.split("/")[-1])
def segmentation(retain=3, n_segments=20, compactness=20, sigma=0): images = image_generator() for im in images: retained_segments = segment_image_slic( image, retain=retain, n_segments=n_segments, compactness=compactness, sigma=sigma ) plot_images([image, retained_segments], ["Original", "Segmented"])
def main(csv_path=os.path.join(BASE_DIR, 'data/train.csv'), img_path=os.path.join(BASE_DIR, 'data/imgs/')): whale_index = pd.read_csv(csv_path) image_file_names = lookup_whale_images(whale_index, whale_id='whale_78785') image_arrays = load_images(image_file_names) image_file = image_file_names[0] image = image_arrays[0] resized_array = resize_images(image_arrays) transformed_array = transform_images(resized_array) t_im_1 = transformed_array[0].reshape((3, 300, 300)).transpose(1, 2, 0) t_im_2 = transformed_array[1].reshape((3, 300, 300)).transpose(1, 2, 0) plot_images([t_im_1, t_im_2],['transformed image 1','transformed image 2'])
def experiment_thresholding(votes_min=3): from skimage.filters import threshold_otsu from skimage.filters import threshold_li from skimage.filters import threshold_yen from skimage.filters import threshold_adaptive from scipy.ndimage import median_filter images = image_generator() for fn, im in images: print("inspecting image: ", fn) print("computing otsu threshold") otsu = threshold_otsu(im) otsu_ch1 = np.zeros(im.shape) otsu_ch2 = np.zeros(im.shape) otsu_ch3 = np.zeros(im.shape) otsu_ch1[im[:, :, 0] > otsu, 0] = 255 otsu_ch2[im[:, :, 1] > otsu, 1] = 255 otsu_ch3[im[:, :, 2] > otsu, 2] = 255 otsu_ch1 = smallest_partition(otsu_ch1, 0) otsu_ch2 = smallest_partition(otsu_ch2, 1) otsu_ch3 = smallest_partition(otsu_ch3, 2) print("computing yen threshold") yen = threshold_yen(im) yen_ch1 = np.zeros(im.shape) yen_ch2 = np.zeros(im.shape) yen_ch3 = np.zeros(im.shape) yen_ch1[im[:, :, 0] > yen, 0] = 255 yen_ch2[im[:, :, 1] > yen, 1] = 255 yen_ch3[im[:, :, 2] > yen, 2] = 255 yen_ch1 = smallest_partition(yen_ch1, 0) yen_ch2 = smallest_partition(yen_ch2, 1) yen_ch3 = smallest_partition(yen_ch3, 2) print("computing li threshold") li = threshold_li(im) li_ch1 = np.zeros(im.shape) li_ch2 = np.zeros(im.shape) li_ch3 = np.zeros(im.shape) li_ch1[im[:, :, 0] > li, 0] = 255 li_ch2[im[:, :, 1] > li, 1] = 255 li_ch3[im[:, :, 2] > li, 2] = 255 li_ch1 = smallest_partition(li_ch1, 0) li_ch2 = smallest_partition(li_ch2, 1) li_ch3 = smallest_partition(li_ch3, 2) print("computing average threshold") av_ch1 = np.zeros(im.shape) av_ch2 = np.zeros(im.shape) av_ch3 = np.zeros(im.shape) votes1 = otsu_ch1 + yen_ch1 + li_ch1 votes1 = otsu_ch1 + yen_ch1 + li_ch1 votes2 = otsu_ch2 + yen_ch2 + li_ch2 votes3 = otsu_ch3 + yen_ch3 + li_ch3 av_ch1[votes1[:, :, 0] >= (255 * votes_min), 0] = 255 av_ch2[votes2[:, :, 1] >= (255 * votes_min), 1] = 255 av_ch3[votes3[:, :, 2] >= (255 * votes_min), 2] = 255 thresholded_images = [ otsu_ch1, otsu_ch2, otsu_ch3, yen_ch1, yen_ch2, yen_ch3, li_ch1, li_ch2, li_ch3, av_ch1, av_ch2, av_ch3, ] print("filtering out specks") for idx, im in enumerate(thresholded_images): thresholded_images[idx] = median_filter(im, size=3) titles = [ "Channel 1 Otsu", "Channel 2 Otsu", "Channel 3 Otsu", "Channel 1 Yen", "Channel 2 Yen", "Channel 3 Yen", "Channel 1 Li", "Channel 2 Li", "Channel 3 Li", "Channel 1 Avg", "Channel 2 Avg", "Channel 3 Avg", ] print("plotting") plot_images(thresholded_images, titles=titles, suptitle=fn.split("/")[-1])