import numpy as np
import time, os, sys
from urllib.parse import urlparse
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
mpl.rcParams['figure.dpi'] = 300
from cellpose import utils, io
# I will download images from website
urls = ['http://www.cellpose.org/static/images/img02.png',
'http://www.cellpose.org/static/images/img03.png',
'http://www.cellpose.org/static/images/img05.png']
files = []
for url in urls:
parts = urlparse(url)
filename = os.path.basename(parts.path)
if not os.path.exists(filename):
sys.stderr.write('Downloading: "{}" to {}\n'.format(url, filename))
utils.download_url_to_file(url, filename)
files.append(filename)
# REPLACE FILES WITH YOUR IMAGE PATHS
# files = ['img0.tif', 'img1.tif']
# view 1 image
img = io.imread(files[-1])
plt.figure(figsize=(2,2))
plt.imshow(img)
plt.axis('off')
plt.show()
# RUN CELLPOSE
from cellpose import models, io
# DEFINE CELLPOSE MODEL
# model_type='cyto' or model_type='nuclei'
model = models.Cellpose(gpu=False, model_type='cyto')
# define CHANNELS to run segementation on
# grayscale=0, R=1, G=2, B=3
# channels = [cytoplasm, nucleus]
# if NUCLEUS channel does not exist, set the second channel to 0
# channels = [0,0]
# IF ALL YOUR IMAGES ARE THE SAME TYPE, you can give a list with 2 elements
# channels = [0,0] # IF YOU HAVE GRAYSCALE
# channels = [2,3] # IF YOU HAVE G=cytoplasm and B=nucleus
# channels = [2,1] # IF YOU HAVE G=cytoplasm and R=nucleus
# or if you have different types of channels in each image
channels = [[2,3], [0,0], [0,0]]
# if diameter is set to None, the size of the cells is estimated on a per image basis
# you can set the average cell `diameter` in pixels yourself (recommended)
# diameter can be a list or a single number for all images
# you can run all in a list e.g.
# >>> imgs = [io.imread(filename) in files]
# >>> masks, flows, styles, diams = model.eval(imgs, diameter=None, channels=channels)
# >>> io.masks_flows_to_seg(imgs, masks, flows, diams, files, channels)
# >>> io.save_to_png(imgs, masks, flows, files)
# or in a loop
for chan, filename in zip(channels, files):
img = io.imread(filename)
masks, flows, styles, diams = model.eval(img, diameter=None, channels=chan)
# save results so you can load in gui
io.masks_flows_to_seg(img, masks, flows, diams, filename, chan)
# save results as png
io.save_to_png(img, masks, flows, filename)
>>>> using CPU cellpose_residual_on_style_on_concatenation_off processing 1 image(s) time spent: running network 1.50s; flow+mask computation 0.00 time spent: running network 1.52s; flow+mask computation 0.99 estimated cell diameters for 1 image(s) in 4.11 sec time spent: running network 10.35s; flow+mask computation 0.98 estimated masks for 1 image(s) in 11.34 sec >>>> TOTAL TIME 15.45 sec processing 1 image(s) time spent: running network 1.46s; flow+mask computation 0.00 time spent: running network 1.62s; flow+mask computation 0.56 estimated cell diameters for 1 image(s) in 3.73 sec time spent: running network 6.75s; flow+mask computation 0.57 estimated masks for 1 image(s) in 7.33 sec >>>> TOTAL TIME 11.06 sec processing 1 image(s) time spent: running network 4.53s; flow+mask computation 0.00 time spent: running network 4.27s; flow+mask computation 1.82 estimated cell diameters for 1 image(s) in 10.75 sec time spent: running network 18.52s; flow+mask computation 1.74 estimated masks for 1 image(s) in 20.27 sec >>>> TOTAL TIME 31.02 sec
# DISPLAY RESULTS
from cellpose import plot
fig = plt.figure(figsize=(12,5))
plot.show_segmentation(fig, img, masks, flows[0], channels=chan)
plt.tight_layout()
plt.show()