Last active
May 30, 2016 13:17
-
-
Save hirokai/9398795 to your computer and use it in GitHub Desktop.
Radial profile in Fiji
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
# 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 | |
# Get mean pixel intensity in a specified donut-shaped area | |
def get_mean_in_donut(imp, cx, cy, min_r, max_r): | |
options = IS.MEAN | IS.MEDIAN | IS.MIN_MAX | IS.AREA | |
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, and divide it by the area. | |
return (stats_big.mean * stats_big.area - stats_small.mean * stats_small.area) / (stats_big.area - stats_small.area) | |
# Getting a radial profile. | |
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 = 'recipe.csv' | |
# Open a csv file. csvfile keeps a "file handle". | |
csvfile = open(infile, 'rb') | |
# Any error happening inside try block does not stop the execution of script. | |
try: | |
# reader is used for reading a csv line by line. | |
reader = csv.reader(csvfile) | |
reader.next() # Skip the first row | |
for row in reader: | |
if len(row) >= 5: | |
# '*' operator expands a list to arguments to a function. | |
profile = process(row[0], *map(int, row[1:5])) | |
print ",".join(map(str, profile)) | |
# finally block is executed in the end no matter if an error occurs or not. | |
finally: | |
csvfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment