Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ij import IJ
- from ij.io import DirectoryChooser, OpenDialog
- from ij.gui import GenericDialog
- import csv
- import os
- def run(image_filename, roi_filename=None, orientation_filename=None):
- imp = IJ.openImage(image_filename)
- roi = load_roi(roi_filename)
- imp.setRoi(roi)
- measure_area(imp)
- imp.show()
- def load_roi(roi_filename):
- f = open(roi_filename, 'r')
- x = []
- y = []
- reader = csv.DictReader(f, delimiter='\t')
- for row in reader:
- x.append(float(row['X']))
- y.append(float(row['Y']))
- f.close()
- polygon = FloatPolygon(x,y)
- roi = PolygonRoi(polygon, PolygonRoi.POLYGON)
- return roi
- def measure_area(imp):
- pix = imp.getProcessor().getPixels()
- total_pixels = len(pix)
- roi = imp.getProcessor().getRoi()
- w = imp.getWidth()
- total = 0
- above_thresh = 0
- total_intensity = 0.
- for i, p in enumerate(pix):
- x = i % w
- y = i / w # integer division
- if roi.contains(x,y):
- total += 1
- if p > 100:
- above_thresh += 1
- total_intensity += float(p)
- IJ.showProgress(i, total_pixels)
- if total == 0:
- print 'total pixels inside roi = 0'
- total = 1
- IJ.showProgress(1)
- stats = imp.getStatistics(Measurements.STD_DEV |
- Measurements.CENTROID |
- Measurements.CENTER_OF_MASS |
- Measurements.MEAN)
- print """Pixel count via stats: %d
- Pixel count via iterating over pixels: %d""" % (stats.pixelCount, total)
- def get_options():
- # Get image
- i_od = OpenDialog("Choose image file", None)
- image_filename = os.path.join(i_od.getDirectory(), i_od.getFileName())
- # Get Region of Interest (roi)
- r_od = OpenDialog("Choose roi file", None)
- roi_filename = os.path.join(r_od.getDirectory(), r_od.getFileName())
- # Get threshold value
- gd = GenericDialog("Options")
- gd.addNumericField("Threshold - just ignore, still hardcoded", 500, 0) # 500 default and zero d.p.
- gd.showDialog()
- if (image_filename is None) or \
- (roi_filename is None) or \
- gd.wasCanceled():
- raise Exception("User canceled the dialog")
- else:
- threshold = gd.getNextNumber()
- return (image_filename, roi_filename)
- options = get_options()
- run(*options)
Advertisement
Add Comment
Please, Sign In to add comment