VPoS / CellCTC /mask_convert.py
ghazishazan's picture
Upload folder using huggingface_hub
eb6a701 verified
raw
history blame
4.08 kB
import os
import numpy as np
from PIL import Image
import random
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# Base directory for the datasets
base_dir = "/l/users/sahal.mullappilly/Komal/documents/Cell/CTCdataset"
# Function to generate unique colors for each cell ID
def generate_colormap():
# Define a colormap with enough unique colors
colors = plt.cm.get_cmap('tab20', 20) # You can adjust the number of colors here
color_list = [colors(i) for i in range(colors.N)]
# Generate a list of unique RGB colors for each unique cell ID
return ListedColormap(color_list)
# Function to convert ground truth tif to colored png
def convert_gt_to_colored_png(gt_tif_path, output_png_path, colormap):
try:
# Open the ground truth tiff image using PIL
gt_img = Image.open(gt_tif_path)
# Convert to a numpy array
gt_array = np.array(gt_img)
# Create an empty colored image using the colormap
colored_gt = np.zeros((gt_array.shape[0], gt_array.shape[1], 3), dtype=np.uint8)
# For each unique cell ID, assign a unique color
unique_ids = np.unique(gt_array)
for cell_id in unique_ids:
if cell_id == 0: # Assuming 0 represents background, no need to color it
continue
color = colormap(cell_id % 20)[:3] # Normalize cell ID to map to the colormap
# Assign the color to all pixels with this cell ID
colored_gt[gt_array == cell_id] = np.array(color) * 255
# Convert the array back to an image
colored_gt_img = Image.fromarray(colored_gt)
# Save the colored image as PNG
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}")
# List of datasets and their corresponding subfolders (01, 02)
datasets = [
# {"dataset_name": "Fluo-C2DL-Huh7", "subfolders": ["01", "02"]},
# {"dataset_name": "Fluo-C2DL-MSC", "subfolders": ["01", "02"]},
# {"dataset_name": "Fluo-N2DH-SIM+", "subfolders": ["01", "02"]},
# {"dataset_name": "Fluo-N2DL-HeLa", "subfolders": ["01", "02"]},
{"dataset_name": "Fluo-N2DH-GOWT1", "subfolders": ["01", "02"]},
# {"dataset_name": "PhC-C2DL-PSC", "subfolders": ["01", "02"]},
# {"dataset_name": "DIC-C2DH-HeLa", "subfolders": ["01", "02"]},
# {"dataset_name": "BF-C2DL-HSC", "subfolders": ["01", "02"]},
# {"dataset_name": "BF-C2DL-MuSC", "subfolders": ["01", "02"]},
# {"dataset_name": "PhC-C2DH-U373", "subfolders": ["01", "02"]}
]
# Path for the main "Annotations" directory
main_annotations_dir = os.path.join(base_dir, "Annotations")
os.makedirs(main_annotations_dir, exist_ok=True)
# Create a colormap for unique cell IDs
colormap = generate_colormap()
# Process each dataset and its subfolders
for dataset in datasets:
for subfolder in dataset["subfolders"]:
# Paths for the input ground truth and the output directory
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}")
# Create output directory for each subfolder inside "Annotations"
os.makedirs(output_folder, exist_ok=True)
# Process each .tif file in the subfolder
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)
# Define the path for the new png file
png_filename = file.replace(".tif", ".png")
png_path = os.path.join(output_folder, png_filename)
# Convert the ground truth .tif to colored .png
convert_gt_to_colored_png(gt_tif_path, png_path, colormap)
print("Ground truth TIFF images converted to colored PNG in the 'Annotations' folder!")