Skip to content

Instantly share code, notes, and snippets.

@hirokai
Created September 17, 2015 02:49
Show Gist options
  • Save hirokai/142922ad9ecca053e540 to your computer and use it in GitHub Desktop.
Save hirokai/142922ad9ecca053e540 to your computer and use it in GitHub Desktop.
Find cells in all frames from a movie, which can run in headless mode of Fiji
# This script runs on Fiji.
# Just drag and drop this file into a Fiji window.
# You can also use headless mode
# > /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless \
# > find_cells-headless.fiji.py /path/to/movie.avi
from ij import IJ, ImageStack, ImagePlus
from ij.process import ImageProcessor, ImageConverter
from ij.plugin import ImageCalculator, AVI_Reader, ZProjector, HyperStackConverter
from ij.plugin.filter import GaussianBlur, Analyzer, ParticleAnalyzer
from ij.measure import Measurements, ResultsTable
import sys
# Thresholding a movie (as ImagePlus)
def threshold(imp):
threshold = 7
sigma = 3
# Subtract average of all frames from every frame
proj = ZProjector(imp)
proj.setMethod(ZProjector.AVG_METHOD)
proj.doRGBProjection()
imp_avg = proj.getProjection()
imp_subt = ImageCalculator().run("Subtract create stack", imp, imp_avg)
# Prepare a Gaussian blur filter
blur = GaussianBlur()
# Prepare ImageStack for result
st = imp_subt.getImageStack()
n_slices = st.getSize()
st2 = ImageStack(st.getWidth(), st.getHeight(), n_slices)
# Apply Gaussian blur and thresholding to every frame
for sl in range(1, n_slices + 1):
sys.stdout.write('.')
ip = st.getProcessor(sl)
blur.blurGaussian(ip, sigma, sigma, 0.02)
ip2 = ip.convertToByteProcessor()
ip2.threshold(threshold)
ip2.invert()
st2.setProcessor(ip2, sl)
sys.stdout.flush()
sys.stdout.write('\n')
# Wrap ImageStack with ImagePlus for a return value
imp2 = ImagePlus("gray avg thresholded", st2)
return imp2
# Find coordinates (center of mass) of cells.
# Return (header as [str], data as [[str]])
# Each row of data has [Number, Area, XM, YM, Slice]
def find_coordinates(imp):
# rt = Analyzer.getResultsTable()
rt = ResultsTable()
pa = ParticleAnalyzer(ParticleAnalyzer.SHOW_NONE,
Measurements.SLICE | Measurements.CENTER_OF_MASS | Measurements.AREA, rt, 300, 10000, 0, 1)
n_slices = imp.getStackSize()
# Runs ParticleAnalyzer on every frame
for i in range(1, n_slices + 1):
imp.setSlice(i)
pa.analyze(imp)
ps = []
heading = ['Number'] + list(rt.getHeadings())
# Get all results from ResultsTable
for i in range(rt.getCounter()):
ps.append(rt.getRowAsString(i).split('\t'))
return heading, ps
def main():
# 1st command line argument is a path of input movie.
in_path = sys.argv[1] if len(
sys.argv) >= 2 else "/path/to/default_movie.avi"
out_path = in_path.replace('raw data', 'visualizer').replace('.avi', '.csv')
print('Input: ' + in_path)
print('Output: ' + out_path)
print('Script loaded.')
imp = AVI_Reader().openVirtual(in_path)
print('Movie loaded. Thresholding...')
imp2 = threshold(imp)
print('Thresholded. Finding coordinates...')
heading, ps = find_coordinates(imp2)
print('Found coordinates.')
# Write out coordinates as a csv file.
import csv
f = open(out_path, 'wb')
writer = csv.writer(f)
writer.writerow(heading)
for row in ps:
writer.writerow(row)
f.close()
print('CSV written to: ' + out_path)
print('Done')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment