def calc_histograms_lab_setups(): for polimer_type in ["PDL-05", "PDLG-5002"]: radius_coefs = {"PDL-05": 0.9, "PDLG-5002": 0.95} paths = file_paths.get_benchtop_setup_paths(polimer_type) for sample_id in range(len(paths)): sample_name = list(paths.keys())[sample_id] print( f"============== {sample_id} sample: {sample_name} ==============" ) img3d = get_bin_img(sample_name) print('tot: ', np.sum(img3d) / img3d.size) fig, ax = plt.subplots() ax.imshow(img3d[0], cmap="gray") dm.save_plot(fig, "previews", f'{sample_id} bin ' + sample_name) cylindric_fragments, cylindric_masks \ = divide_image_into_sector_cylindric_fragments(img3d, height=len(img3d)//3-1, radius_coef=radius_coefs[polimer_type]) fig = get_porosity_histogram_disrtibution( cylindric_fragments, sample_name, img3d.shape, pixel_size_mm=PIXEL_SIZE_MM_SETUP, masks=cylindric_masks, radius_coef=radius_coefs[polimer_type]) dm.save_plot(fig, SAVE_IMG_DESKTOP_SETUP_FOLDER, f'hist {sample_id} {sample_name}')
def save_first_section_of_img(img_3d, file_id, edge_size): with plt.style.context('classic'): fig, ax = plt.subplots(figsize=(10, 10)) ax.imshow(img_3d[0], cmap='gray') ax = plot_cubic_periodic_grid(img_3d[0], ax, edge_size) ax.set_title(f'sample id {file_id}') dm.save_plot(fig, SAVE_IMG_SYNCHROTON_FOLDER, f'section {file_id}')
def max_sections(data, folder, shots_num): k = 0 img_frame = [] for img, _ in data: img_frame.append(img) k += 1 if k % shots_num == 0: img = np.max(img_frame, axis=0) fig, ax = plt.subplots(figsize=(7, 7)) ax.imshow(img, cmap="gray") dm.save_plot(fig, folder, str(k // shots_num)) img_frame = []
def preview(sample_name, n, contrast=True, server=True): if server: img = dm.get_img2d_from_server(sample_name, n) else: file_name = dm.generate_tif_file_name(n, True) img = dm.get_img2d_from_database(file_name, folder_name="crop_"+sample_name) if contrast: clip_limit = 0.1 img = exposure.equalize_adapthist(img, clip_limit=clip_limit) fig, ax = plt.subplots(figsize=(7, 7)) ax.imshow(img, cmap="gray") dm.save_plot(fig, "sections", sample_name+"_"+str(n))
def plot_N_sections_multiotsu(img3d, polimer_attenuation, filename, N=3, radius_coef=0.9, folder=SAVE_IMG_DESKTOP_SETUP_FOLDER): section_indexes = np.linspace(0, len(img3d), N, endpoint=False, dtype=int) fig, axes = plt.subplots(nrows=3, ncols=N, figsize=(21, 21)) axes_plot, axes_hist, axes_bin = axes img3d = gaussian_filter(img3d, sigma=3) for ax_plot, ax_hist, i in zip(axes_plot, axes_hist, section_indexes): ax_plot.imshow(img3d[i], cmap="gray") ax_hist.hist(img3d[i].flatten(), bins=255, color="gray") thresholds = threshold_multiotsu(img3d[i]) for thresh in thresholds: ax_hist.axvline(thresh, color='red') img3d = binarize_without_eppendorf(img3d, polimer_attenuation=polimer_attenuation) img3d = lv.remove_levitating_stones(img3d) for ax_bin, i in zip(axes_bin, section_indexes): ax_bin.imshow(img3d[i], cmap="gray", interpolation=None) center = np.asarray(img3d[i].shape) // 2 rr, cc = disk(center, int(np.min(center) * radius_coef), shape=img3d[i].shape) mask = np.zeros(img3d[i].shape, dtype=int) mask[rr, cc] = True mask = np.ma.masked_where(mask > 0, mask) ax_bin.imshow(mask, cmap="hsv", alpha=0.3) dm.save_plot(fig, folder, 'section ' + filename)
img2d_mask = thresh(exposure.equalize_adapthist(img2d, clip_limit=clip_limit)) img2d_mask = get_small_pores_mask(img2d, img2d_mask, percentile_glob=97.5, min_large_contour_length=1000, window_size=200) squares = [([500, 1500], [600,1500]), ([900, 1200], [900,1200])] axes[0].imshow(img2d, cmap='gray') viewer.view_applied_rectangle(img2d, *squares[0], axes[0], color='red') viewer.view_applied_rectangle(img2d, *squares[1], axes[0], color='green') axes[0].set_title("original image") #axes[1].imshow(exposure.equalize_adapthist(img2d, clip_limit=0.05), cmap='gray') viewer.view_applied_mask(exposure.equalize_adapthist(img2d, clip_limit=clip_limit), img2d_mask, axes[1], alpha=1) viewer.view_applied_rectangle(img2d, *squares[0], axes[1], color='red') viewer.view_applied_rectangle(img2d, *squares[1], axes[1], color='green') axes[1].set_title("adaptive contrast") viewer.view_region(exposure.equalize_adapthist(img2d, clip_limit=clip_limit), img2d_mask, axes[2], *squares[0]) axes[2].set_title("RED box zoomed", fontdict={'color': 'red'}) viewer.view_region(img2d, img2d_mask, axes[3], *squares[1], alpha=0.3) axes[3].set_title("GREEN box zoomed", fontdict={'color': 'green'}) dm.save_plot(fig, "plots", "section") # fig, axes = plt.subplots(ncols=2, figsize=(14, 7), constrained_layout=True)
center_coords) # new approach img_without_contours_frag = median(img_without_contours_frag) min_brightness = np.min(img_without_contours_frag) max_brightness = np.max(img_without_contours_frag) local_thresh = min_brightness + (max_brightness - min_brightness) * 0.5 if local_thresh > global_thresh: local_thresh = global_thresh bin_cropped_fragment = img_without_contours_frag > local_thresh paste(mask_frame, bin_cropped_fragment, center_coords) return ~mask_frame.astype(bool) if __name__ == '__main__': file_id = '123497' num = np.random.randint(0, 2120) #100 # 320 img2d_gray = get_2d_slice_of_sample_from_database(num, file_id=file_id) # fig = preview_small_pores_detection_by_fragment(img2d_gray, plots=8) # pores_mask = get_small_pores_mask(img2d_gray) # ic(np.sum(pores_mask)/pores_mask.size) # fig = preview_small_pores_detection_by_fragment(img2d_gray, percentile=2) fig = preview_small_pores_detection_full(img2d_gray, percentile=2) dm.save_plot(fig, "previews", f"preview_small_pores{file_id}")
hists.append(hist) hist_mean = np.mean(hists, axis=0) hist_std = np.std(hists, axis=0) if size_of_sampling > 1 else 0 mean_of_distribution = count_mean_from_hist(hist_mean, bins) ax.axvline(mean_of_distribution, color="orange", label=f"mean={mean_of_distribution:.2f} mkm") median_of_distribution = count_median_from_hist(hist_mean, bins) ax.axvline(median_of_distribution, color="lawngreen", label=f"median={median_of_distribution:.2f} mkm") total_mean_vol = np.mean(total_mean_pore_volume_in_layers) stats = dict(unit_name="mkm", sample_size=size_of_sampling, size_type=size_type, min_x_value=cpe.recount_volumes_to_diameters(min_pore_volume * pixel_size_mkm) \ if size_type == "diameter" else min_pore_volume * pixel_size_mkm, max_x_value=cpe.recount_volumes_to_diameters(max_pore_volume * pixel_size_mkm) \ if size_type == "diameter" else max_pore_volume * pixel_size_mkm, total_mean_pore_volume_in_layers=cpe.recount_volumes_to_diameters(total_mean_vol) * \ pixel_size_mkm if size_type == "diameter" else total_mean_vol * pixel_size_mkm, ) plot_error_hist(hist_mean, hist_std, bins, ax, stats) ax.legend() ax.grid() dm.save_plot(fig, "plots", f"histogram {file_id} {size_type}")
paths = file_paths.get_benchtop_setup_paths(polimer_type) df = dm.load_data("setup_culindric_porosities.csv") #df = pd.DataFrame(columns = ['polimer_type', 'sample_number', 'date', 'mean', 'std']) for sample_id in range(len(paths)): sample_name = list(paths.keys())[sample_id] print( f"============== {sample_id} sample: {sample_name} ==============") print(sample_name.split()) img3d = ~get_bin_img(sample_name) print('tot: ', np.sum(img3d) / img3d.size) fig, ax = plt.subplots() ax.imshow(img3d[0], cmap="gray") dm.save_plot(fig, "previews", f'{sample_id} bin ' + sample_name) cylindric_fragments, cylindric_masks \ = divide_image_into_sector_cylindric_fragments(img3d, height=len(img3d)//3-1, radius_coef=radius_coefs[polimer_type]) std, mean = get_mean_porosity_and_std(cylindric_fragments, cylindric_masks) data_info = sample_name.split() polimer, sample_number, date = data_info[0][:-2], data_info[0][ -1], data_info[-1] df = df.append( { 'polimer_type': polimer,
figsize=(15, 30), constrained_layout=True) axes = axes.flatten() axes[0].imshow(img2d_gray, cmap="gray") axes[1].imshow(img_matched, cmap="gray") img_matched_bin = img_matched < threshold _ = axes[2].hist(img_matched.flatten(), bins=255) axes[2].axvline(x=threshold, color='red') axes[3].imshow(img_matched_bin, cmap="gray") mask = cpe.find_mask_longest_contours((img_matched > threshold), filter_by_contour_length=True, min_contour_length=max_contour_size, max_number_of_contours=None) image_with_contours = binary_fill_holes(mask) axes[4].imshow(image_with_contours, cmap="gray") imgl, _ = label(image_with_contours.astype(int)) axes[5].imshow(imgl, cmap="hsv") #axes[6].imshow(mask, cmap="gray") # axes[7].imshow(np.logical_and(image_with_contours, mask), cmap="gray") for i, ax in enumerate(axes): if i != 2 and i != 4: ax.axis("off") dm.save_plot(fig, 'hr_bin_plots', 'histogram transforms')
import file_paths import data_manager as dm if __name__ == '__main__': polimer_type = ["PDL-05", "PDLG-5002"][1] radius_coefs = {"PDL-05": 0.9, "PDLG-5002": 0.95} paths = file_paths.get_benchtop_setup_paths(polimer_type) for sample_id in range(len(paths)): sample_name = list(paths.keys())[sample_id] img3d_grey = h5py.File(paths[sample_name], 'r')['Reconstruction'][:] img3d_bin = ~get_bin_img(sample_name) N = 3 fig, axes = plt.subplots(nrows=2, ncols=N, figsize=(21, 21)) axes_grey, axes_bin = axes section_indexes = np.linspace(50, len(img3d_bin), N, endpoint=False, dtype=int) for ax_grey, ax_bin, i in zip(axes_grey, axes_bin, section_indexes): ax_grey.imshow(img3d_grey[i], cmap="gray") ax_bin.imshow(img3d_bin[i], cmap="gray") dm.save_plot(fig, "setup bin section", 'section ' + str(sample_id) + ' ' + sample_name)
import data_manager as dm import matplotlib.pyplot as plt from skimage.transform import radon, iradon angles = np.arange(0, 180, 1) blobns = 2 porsty = 0.5 shape = 1000 # phantom = generator.blobs([shape, shape], porosity=1 - porsty, blobiness=blobns) margin_per_cent = 0.1 phantom = np.ones((shape, shape), dtype=int) fig, ax = plt.subplots(1, 1, figsize=(10, 10)) ax.imshow(phantom, cmap='gray') dm.save_plot(fig, 'plots', 'phantom') source_object = shape * np.sqrt(2) + 80 object_det = shape * np.sqrt(2) + 80 sinogram_parallel = radon(phantom, theta=angles, circle=False) sinogram_fan = np.transpose( ae.astra_fp_2d_fan(phantom, angles, source_object, object_det, detector_size=10000)) fig, ax = plt.subplots(1, 2) ax[0].imshow(sinogram_fan, cmap='gray') ax[0].set_title('sinogram_fan') ax[1].imshow(sinogram_parallel, cmap='gray')
axes = np.transpose(axes) for row_index, (axes_row, k) in enumerate(zip(axes, k_vals)): img2d_restored, saved_energy = poganins_correction(img2d, k) thresh = threshold_otsu(img2d_restored) * 0.7 if row_index == ncols // 2: axes_row[0].imshow(img2d, cmap='gray') else: axes_row[0].axis("off") axes_row[1].imshow(img2d_restored, cmap='gray') axes_row[1].set_title(f"saved energy: {saved_energy:.5f}, k: {k}") axes_row[2].imshow(img2d_restored > thresh, cmap='gray') axes_row[2].set_title(f"saved energy: {saved_energy:.5f}, k: {k}") dm.save_plot(fig, "unbiased_poganin", f"{file_id}") # k_vals = np.arange(0.1, 10, 0.5) # energies = [] # ic(k_vals) # for k in k_vals: # _, s = poganins_correction(img2d, k) # energies.append(s) # # fig, ax = plt.subplots(figsize=(7, 7)) # ax.plot(k_vals, energies) # ax.grid() # dm.save_plot(fig, "unbiased_poganin", f"{file_id}_energies")
data_info = sample_name.split() polimer, sample_number, date = data_info[0][:-2], data_info[0][ -1], data_info[-1] img3d = get_bin_img(sample_name) snow = ps.networks.snow(im=img3d, voxel_size=9) proj = op.io.PoreSpy.import_data(snow) net, geom = proj pore_diameters, pore_distances = geom['pore.diameter'], geom[ 'throat.length'] title = f"DIAMETERS: {sample_name}" fig, mean_diameters, median_diameters, standard_dev_diameters = plot_hist( pore_diameters, "DIAMETERS", sample_name) dm.save_plot(fig, "networks", f"{sample_id} " + title) title = f"DISTANCES: {sample_name}" fig, mean_distances, median_distances, standard_dev_distances = plot_hist( pore_distances, "DISTANCES", sample_name) dm.save_plot(fig, "networks", f"{sample_id} " + title) # df = df.append({'polimer_type': polimer_type, # 'sample_number': sample_number, # 'median_diameters': median_diameters, # 'mean_diameters': mean_diameters, # 'standard_dev_diameters': standard_dev_diameters, # 'mean_distances': mean_distances, # 'median_distances': median_distances, # 'standard_dev_distances': standard_dev_distances}, ignore_index=True)