| import os |
| import numpy as np |
| from PIL import Image |
| import random |
| import matplotlib.pyplot as plt |
| from matplotlib.colors import ListedColormap |
|
|
| |
| base_dir = "/l/users/sahal.mullappilly/Komal/documents/Cell/CTCdataset" |
|
|
| |
| def generate_colormap(): |
| |
| colors = plt.cm.get_cmap('tab20', 20) |
| color_list = [colors(i) for i in range(colors.N)] |
| |
| return ListedColormap(color_list) |
|
|
| |
| def convert_gt_to_colored_png(gt_tif_path, output_png_path, colormap): |
| try: |
| |
| gt_img = Image.open(gt_tif_path) |
|
|
| |
| gt_array = np.array(gt_img) |
|
|
| |
| colored_gt = np.zeros((gt_array.shape[0], gt_array.shape[1], 3), dtype=np.uint8) |
|
|
| |
| unique_ids = np.unique(gt_array) |
| for cell_id in unique_ids: |
| if cell_id == 0: |
| continue |
| color = colormap(cell_id % 20)[:3] |
| |
| colored_gt[gt_array == cell_id] = np.array(color) * 255 |
|
|
| |
| colored_gt_img = Image.fromarray(colored_gt) |
| |
| colored_gt_img.save(output_png_path) |
| print(f"Converted {gt_tif_path} to {output_png_path}") |
| except Exception as e: |
| print(f"Error converting {gt_tif_path}: {e}") |
|
|
| |
| datasets = [ |
| |
| |
| |
| |
| {"dataset_name": "Fluo-N2DH-GOWT1", "subfolders": ["01", "02"]}, |
| |
| |
| |
| |
| |
| ] |
|
|
| |
| main_annotations_dir = os.path.join(base_dir, "Annotations") |
| os.makedirs(main_annotations_dir, exist_ok=True) |
|
|
| |
| colormap = generate_colormap() |
|
|
| |
| for dataset in datasets: |
| for subfolder in dataset["subfolders"]: |
| |
| input_folder = os.path.join(base_dir, "datasets", dataset["dataset_name"], f"{subfolder}_GT", "TRA") |
| output_folder = os.path.join(main_annotations_dir, f"{dataset['dataset_name']}-{subfolder}") |
| |
| |
| os.makedirs(output_folder, exist_ok=True) |
|
|
| |
| for root, dirs, files in os.walk(input_folder): |
| for file in files: |
| if file.endswith(".tif"): |
| gt_tif_path = os.path.join(root, file) |
| |
| png_filename = file.replace(".tif", ".png") |
| png_path = os.path.join(output_folder, png_filename) |
| |
| |
| convert_gt_to_colored_png(gt_tif_path, png_path, colormap) |
|
|
| print("Ground truth TIFF images converted to colored PNG in the 'Annotations' folder!") |