Using ChatGPT- day 1/ 170

Jun 21, 2023

Create code to do cell segmentation and image registration for mouse brains using the Allen Brain Atlas common coordinates framework.

ChatGPT chat history: https://chat.openai.com/share/e5db5cac-6b98-417a-9b9f-c7c13f93b31c

Google Colab notebook: https://colab.research.google.com/drive/1X1XFWzh7WXQ_eH55mFVST_DZfhWmYNQE?authuser=1


import numpy as np

import matplotlib.pyplot as plt

import skimage.morphology as morphology

import skimage.measure as measure

Load the image and the soma mask

image = io.imread('image.tif')

soma_mask = io.im('soma_mask.tif')

Threshold the image to separate cells from the background

thresh = filters.threshold_otsu(image)

image_thresholded = image > thresh

Use morphological operations to refine cell segmentation

image_opened = morphology.opening(image_thresholded, selem=morphology.disk(5))

image_closed = morphology.closing(image_opened, selem=morphology.disk(5))

Use connected component labeling to identify individual cells

cell_labels, num_cells = measure.label(image_closed, return_num=True)

Iterate over each label (i.e. each cell)

for label in range(1,num_cells+1):

Extract the current cell from the labeled image

cell = np.zeros_like(image)

cell[cell_labels == label] = image[cell_labels==label]

Check if the cell is connected to the soma

connected = np.any(cell * soma_mask)

if not connected:

Delete the cell from the labeled image

image_closed[cell_labels == label] = 0

Visualize the resulting image

plt.imshow(image_closed,cmap='gray')

plt.show()