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()
2021-11-13 15:52:25,505 [INFO] WRITING LOG OUTPUT TO C:\Users\carse\.cellpose\run.log
# 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 for 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)
2021-11-13 15:52:25,760 [INFO] >>>> using CPU 2021-11-13 15:52:25,827 [INFO] ~~~ ESTIMATING CELL DIAMETER(S) ~~~ 2021-11-13 15:52:33,370 [WARNING] WARNING: not interp 2021-11-13 15:52:35,023 [INFO] estimated cell diameter(s) in 9.20 sec 2021-11-13 15:52:35,024 [INFO] >>> diameter(s) = 2021-11-13 15:52:35,025 [INFO] [ 29.38 ] 2021-11-13 15:52:35,025 [INFO] ~~~ FINDING MASKS ~~~ 2021-11-13 15:52:55,436 [INFO] >>>> TOTAL TIME 29.61 sec 2021-11-13 15:52:56,985 [INFO] ~~~ ESTIMATING CELL DIAMETER(S) ~~~ 2021-11-13 15:53:04,969 [WARNING] WARNING: not interp 2021-11-13 15:53:05,959 [INFO] estimated cell diameter(s) in 8.97 sec 2021-11-13 15:53:05,960 [INFO] >>> diameter(s) = 2021-11-13 15:53:05,961 [INFO] [ 33.18 ] 2021-11-13 15:53:05,961 [INFO] ~~~ FINDING MASKS ~~~ 2021-11-13 15:53:18,715 [INFO] >>>> TOTAL TIME 21.73 sec 2021-11-13 15:53:19,473 [INFO] ~~~ ESTIMATING CELL DIAMETER(S) ~~~ 2021-11-13 15:53:38,160 [WARNING] WARNING: not interp 2021-11-13 15:53:40,954 [INFO] estimated cell diameter(s) in 21.48 sec 2021-11-13 15:53:40,955 [INFO] >>> diameter(s) = 2021-11-13 15:53:40,956 [INFO] [ 30.58 ] 2021-11-13 15:53:40,956 [INFO] ~~~ FINDING MASKS ~~~ 2021-11-13 15:54:11,780 [INFO] >>>> TOTAL TIME 52.31 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()