Skip to content

Instantly share code, notes, and snippets.

@hirokai
Created February 3, 2014 07:08
Show Gist options
  • Save hirokai/8779858 to your computer and use it in GitHub Desktop.
Save hirokai/8779858 to your computer and use it in GitHub Desktop.
# Example of Fiji script for calculating a radial profile.
from ij import IJ
from ij.process import ImageStatistics as IS
from ij.gui import Roi, OvalRoi
import csv
options = IS.MEAN | IS.MEDIAN | IS.MIN_MAX | IS.AREA
def get_mean_in_donut(imp, cx, cy, min_r, max_r):
global options
ip = imp.getProcessor()
# Define two ROIs. One is outer diameter. Another is inner diameter.
roi_small = OvalRoi(cx-min_r, cy-min_r, min_r*2+1, min_r*2+1)
roi_big = OvalRoi(cx-max_r, cy-max_r, max_r*2+1, max_r*2+1)
imp.setRoi(roi_small)
stats_small = IS.getStatistics(ip, options, imp.getCalibration())
imp.setRoi(roi_big)
stats_big = IS.getStatistics(ip, options, imp.getCalibration())
# Subtract integrated intensity of inner circle from that of outer circle.
return (stats_big.mean * stats_big.area - stats_small.mean * stats_small.area) / (stats_big.area - stats_small.area)
def get_radialprofile(imp, cx, cy, rmax, num_bin):
r_interval = rmax / num_bin
rs = [(i*r_interval, (i+1)*r_interval) for i in range(num_bin)]
return [get_mean_in_donut(imp, cx, cy, r[0], r[1]) for r in rs]
def process(filename, cx, cy, r, num_bin):
imp = IJ.openImage(filename)
if imp is None:
print "Could not open image from file:", filename
return None
else:
return get_radialprofile(imp, cx, cy, r, num_bin)
# CSV file with five columns: file path, cx, cy, r, # of bins
infile = '/Users/hiroyuki/Documents/PycharmProjects/test/recipe.csv'
csvfile = open(infile, 'rb')
try:
reader = csv.reader(csvfile)
reader.next() # Skip the first row
for row in reader:
if len(row) >= 5:
profile = process(row[0], *map(int,row[1:5])) # '*' operator expands a list to arguments to a function.
print ",".join(map(str,profile))
else:
pass
finally:
csvfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment