Skip to content

Instantly share code, notes, and snippets.

@stefanv
Created September 21, 2012 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stefanv/3763784 to your computer and use it in GitHub Desktop.
Save stefanv/3763784 to your computer and use it in GitHub Desktop.
Cell detection
from skimage import io, filter, morphology, segmentation, measure, img_as_float
import os
# Download the image
if not os.path.exists('cell.png'):
print "Downloading snowflakes image..."
import urllib2
u = urllib2.urlopen('http://people.sc.fsu.edu/~jburkardt/data/tif/cell.png')
f = open('cell.png', 'w')
f.write(u.read())
f.close()
# Load image from disk
image = img_as_float(io.imread('cell.png'))
# Perform edge detection
edges = filter.sobel(image)
# Threshold
image_bw = edges > filter.threshold_otsu(edges)
# Perform morphological opening to join parts together
image_bw = morphology.closing(image_bw, selem=morphology.diamond(3))
# Remove objects touching the image border
image_bw = segmentation.clear_border(image_bw)
# Find individual objects in the image
labels = morphology.label(image_bw, neighbors=8, background=0)
# Mark the cell boundaries
for i in range(labels.max() + 1):
c = measure.find_contours(labels == i, 0)[0].astype(int)
image[c[:, 0], c[:, 1]] = 1
io.imshow(image)
io.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment