Skip to content

Instantly share code, notes, and snippets.

Created September 21, 2012 20:45
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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('')
f = open('cell.png', 'w')
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment