Created
September 17, 2015 02:39
-
-
Save hirokai/1dbe5d9e7a32abd07407 to your computer and use it in GitHub Desktop.
Find cells in all frames from a movie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script runs on Fiji. | |
# Just drag and drop this file into a Fiji window. | |
from ij import IJ, ImageStack, ImagePlus | |
from ij.process import ImageProcessor | |
from ij.plugin import ImageCalculator, ZProjector | |
from ij.plugin.filter import GaussianBlur, Analyzer, ParticleAnalyzer | |
from ij.measure import Measurements | |
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.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. | |
def find_coordinates(imp): | |
rt = Analyzer.getResultsTable() | |
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) | |
rt.show('Results of particle analyzer') | |
return [] | |
def main(): | |
IJ.open("/path/to/movie.avi") | |
imp = IJ.getImage() | |
imp2 = threshold(imp) | |
# Area, XM, YM, and Slice are displayed in Result window. | |
find_coordinates(imp2) | |
imp2.show() | |
print('Done') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment