haesleinhuepf / git-bob-bioimage-analysis-example

BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Nuclei segmentation #71

Open haesleinhuepf opened 4 days ago

haesleinhuepf commented 4 days ago

This is human_mitosis.png:

human_mitosis

I would like to segment and label the bright nuclei in it, using the Voronoi-Otsu-Labeling algorithm. The resulting label image should be converted to an uint8 RGB image and saved as .png file.

git-bob think about how to do this with Python.

github-actions[bot] commented 4 days ago

This message was generated by git-bob (version: 0.8.3, model: gpt-4o-2024-08-06, log), an experimental AI-based assistant. It can make mistakes and has limitations. Check its messages carefully.

To segment the bright nuclei in the provided image using the Voronoi-Otsu-Labeling algorithm and convert the result into an RGB image, we can follow these steps:

# Step 1: Import necessary libraries
from skimage.io import imread
from skimage import color
import napari_segment_blobs_and_things_with_membranes as nsbatwm

# Step 2: Download and load the image
import requests
from pathlib import Path

image_url = "https://github.com/user-attachments/assets/c1bf94c5-fe80-4ff9-a46e-d4d9cb9f276f"
image_path = Path("data/human_mitosis.png")

# Make sure the data folder exists
image_path.parent.mkdir(parents=True, exist_ok=True)

# Download the image
response = requests.get(image_url)
image_path.write_bytes(response.content)

# Load the image
image = imread(image_path)

# Step 3: Use the Voronoi-Otsu-Labeling algorithm to segment and label the nuclei
label_image = nsbatwm.voronoi_otsu_labeling(image, spot_sigma=3.5, outline_sigma=1)

# Step 4: Convert the label image to an RGB image
rgb_image = color.label2rgb(label_image, bg_label=0, image_alpha=0)

# Step 5: Save the RGB image as a .png file
from skimage.io import imsave
rgb_image_path = Path("data/nuclei_segmented.png")
imsave(rgb_image_path, (rgb_image * 255).astype('uint8'))

Make sure to execute this code in your Python environment after ensuring the necessary libraries are installed. This will segment the nuclei in the image, convert the labeled result into an RGB image, and save it as 'nuclei_segmented.png' in the data folder.

haesleinhuepf commented 4 days ago

Ok, this looks good! Turn it into a jupyter notebook, save it to the "notebooks" folder and adapt the paths in the file so that the relative paths work; e.g. "../data" for the images.

git-bob implement this!