diff --git a/Analyze/Microscope Measurement Tools/Choose_Microscope_Calibration.py b/Analyze/Microscope Measurement Tools/Choose_Microscope_Calibration.py new file mode 100644 index 0000000..d99992b --- /dev/null +++ b/Analyze/Microscope Measurement Tools/Choose_Microscope_Calibration.py @@ -0,0 +1,173 @@ +''' Choose_Microscope_Calibration.py +Plugin for FIJI, to enable custom scaling for various microscope objectives. + +Reads user settings from `Microscope_Calibrations_user_settings.py` +including user-calibrations and names for various microscope objectives. +This function will popup a menu of all available microscope cals listed in the settings file, and then apply that scaling (and unit) to the image. +User can optionally apply the scaling to all open images, and/or run the "Scale Bar..." command afterwards. + +Based off Microscope_Scale.java & Correct_3d_drift.py + + +v1.1 +Demis D. John, Praevium Research Inc., 2015-05-29 +''' + +#print "hello outside!" + +## Import some modules: +from ij import IJ, ImagePlus, WindowManager +from ij.gui import GenericDialog, YesNoCancelDialog + +import sys, os + + + + +# add the path to this script, so we can find the user-settings +libpth = os.path.split( os.path.split( sys.path[0] )[0] )[0] # path to Fiji folder +#print libpth +libpth = os.path.join(libpth, 'plugins', 'Scripts', 'Analyze', 'Microscope Measurement Tools') + + +try: + sys.path.index( libpth ) # see if search-path is already added +except ValueError: + # path wasn't included yet, so add it: + sys.path.append( libpth ) +#sys.path.append('/Applications/Fiji.app/plugins/Scripts/Plugins/Demis/') +#print sys.path + +# microscope settings should be in the file `Microscope_Calibrations_user_settings.py`: +import Microscope_Calibrations_user_settings as cal # imports `names`, `cals`, `units` under namespace `cal.names` etc. + + + + +# the run() function is called at the end of this script: +def run(): + '''This is the main function run when the plugin is called.''' + + #print "hello in run()" + + + + #print cal.names, cal.cals, cal.units + + + imp = IJ.getImage() # get the current Image as ImagePlus object? + #print "imp=", imp + + + CalIdx, SetGlobalScale, AddScaleBar = uScopeCalDialog(cal) # show Calibrations dialog + + if CalIdx == None: return # User cancelled - exit + + # the following translated from "Microscope_Scale.java": + newcal = imp.getCalibration().copy() # make a copy of calibration object + newcal.setUnit( cal.units[CalIdx] ) + print "Chosen Cal=", cal.cals[CalIdx], " px/unit" + newcal.pixelWidth = 1./cal.cals[CalIdx] + newcal.pixelHeight = newcal.pixelWidth * cal.aspect_ratio[CalIdx] + + if SetGlobalScale: + '''Apply to all images''' + imp.setGlobalCalibration(newcal) + winlist = WindowManager.getIDList(); + #print "winlist=", winlist + if (winlist==None): return # exit if no images open + for win in winlist: + #(int i=0; i 20 cals, use dropdown list, otherwise use radio buttons''' + if len(cal.names) > 20: + Radio=False + # Drop-Down list: + gd.addChoice(" Calibration:", CalStr, CalStr[0] ) # default = 1st + + else: + Radio=True + gd.addRadioButtonGroup(" Calibration:", CalStr, len(CalStr), 1, CalStr[0]) + #addRadioButtonGroup(label, [String items], rows, columns, String:defaultItem) + #end if(cal>20) + + gd.addCheckbox("Apply Scale to all open images?", False) + gd.addCheckbox("Add Scale Bar to this image?", False) + gd.addMessage("These calibrations can be altered by editing the file: \nFiji.app/plugins/Scripts/Plugins/Analyze/...\n\tMicroscope Measurement Tools/...\n\tMicroscope_Calibrations_user_settings.py") + + gd.showDialog() + + + if gd.wasCanceled(): + return None, None, None # return None's if user cancels + + if Radio: + ChosenCal = gd.getNextRadioButton() + # Find the index to the chosen radio button w/ [list].index(): + CalIdx = CalStr.index( ChosenCal ) + else: + ChosenCal = gd.getNextChoiceIndex() + CalIdx = ChosenCal # the index to the choice + + SetGlobalScale = gd.getNextBoolean() + AddScaleBar = gd.getNextBoolean() + + + + #print ChosenRadio,CalIdx, SetGlobalScale + #print "end uScopeCalDialog()." + return CalIdx, SetGlobalScale, AddScaleBar +#end uScopeCalDialog() + + + + +run() # Run the script function! + + diff --git a/Analyze/Microscope Measurement Tools/Draw_Measurement_-_Line.py b/Analyze/Microscope Measurement Tools/Draw_Measurement_-_Line.py new file mode 100644 index 0000000..772a2e2 --- /dev/null +++ b/Analyze/Microscope Measurement Tools/Draw_Measurement_-_Line.py @@ -0,0 +1,163 @@ +'''Draw Measurement - Line.py +Part of the "Microscope Measurement Tools" scripts +by Demis D. John, Praevium Research Inc., 2015-05-25 + +Draw a Line & Length of the Line along the currently selected Line ROI. + +''' + +#print "hello outside!" + +## Import some modules: +from ij import IJ, ImagePlus, WindowManager #, gui +from ij.gui import GenericDialog, YesNoCancelDialog + +from java.awt import Color as jColor # for setting color +from java.awt import Font as jFont # for setting text font + +import sys, os + + + + +# add the path to this script, so we can find the user-settings +libpth = os.path.split( os.path.split( sys.path[0] )[0] )[0] # split-off the "/jars/lib" part +#print libpth +libpth = os.path.join(libpth, 'plugins', 'Scripts', 'Analyze', 'Microscope Measurement Tools') +# hard-coded path, underneath the Fiji directory. + +try: + sys.path.index( libpth ) # see if search-path is already added +except ValueError: + # path wasn't included yet, so add it: + sys.path.append( libpth ) +#sys.path.append('/Applications/Fiji.app/plugins/Scripts/Plugins/uScopeMeas/') +#print sys.path + +# microscope settings should be in the file `Microscope_Calibrations_user_settings.py`: +import Microscope_Calibrations_user_settings as sets # imports settings under `sets.linecolor`, `sets.linethickness` etc. + +#print os.path.abspath(sets.__file__) +#print dir(sets) +#print sets.cals + + +# the run() function is called at the end of this script: +def run(): + '''This is the main function run when the plugin is called.''' + + #print dir(IJ) + ip = IJ.getProcessor() + + imp = IJ.getImage() # get the current Image, which is an ImagePlus object + #print "imp=", type(imp), imp + #print dir(imp) + + roi = imp.getRoi() # get the drawn ROI + #print "roi=", roi + + #print roi.getClass() + + + # check ROI type + if roi==None: + gd = GenericDialog("Draw Measurement - Line") + gd.addMessage("Please draw a straight-line first!") + gd.showDialog() + return + #raise Exception( "Please draw a line ROI first!" ) + if roi.getTypeAsString() != "Straight Line": + gd = GenericDialog("Draw Measurement - Line") + gd.addMessage("Please draw a straight-line first!") + gd.showDialog() + return + #raise Exception( "Not a Line ROI! (type="+roi.getTypeAsString()+")" ) + + + + # set ROI params from settings: + roi.setStrokeWidth( sets.linethickness ) + roi.setStrokeColor( jColor(float(sets.linecolor[0]), float(sets.linecolor[1]), float(sets.linecolor[2]), float(sets.linecolor[3])) ) + + #roi.drawPixels( ip ) # draw along the ROI - only draws outline unfortunately + ip.drawRoi(roi) # draw the ROI on the image + + + #imp.updateAndDraw() #update the image + + p1 = [ int(roi.x1d), int(roi.y1d) ] # point 1 (x,y) + p2 = [ int(roi.x2d), int(roi.y2d) ] # point 2 + print "Line Points: p1=", p1, " & p2=", p2 + pm = midpoint(p1, p2) # get midpoint coord + + unit = imp.getCalibration().getUnit().encode('utf8') # get the unit as UTF-8 (for \mu) + if unit != 'pixel': unit=unit[1:] # strip weird char at start + + + # format of measurement text (eg. 3 decimal points): + lenstr = "%0.3f" % roi.getLength() + " %s" % (unit) # string to print as length + print "Line length= %s" % lenstr + print "x,y=", p2[0], p2[1] + + #ip.moveTo(p2[0]-ip.getStringWidth(lenstr), p2[1]); # move the drawing 'pen' + if sets.texttoleft: + x=p2[0]-ip.getStringWidth(lenstr) + else: + x = p2[0] + y=p2[1] + + + # set font + ip.setFont( jFont('SansSerif', 0, sets.textsize) ) + ip.setColor( jColor( float(sets.textcolor[0]), float(sets.textcolor[1]), float(sets.textcolor[2]), float(sets.textcolor[3]) ) ) + + + if sets.textbackgroundcolor: + ip.drawString( lenstr, x, y, jColor( float(sets.textbackgroundcolor[0]), float(sets.textbackgroundcolor[1]), float(sets.textbackgroundcolor[2]), float(sets.textbackgroundcolor[3]) ) ) # write the text w/ BG color + else: + ip.drawString( lenstr, x, y ) # write the text + + + imp.updateAndDraw() #update the image + + # to do: + # Add dialogue for user to alter draw options? Or just from settings file? + + + +#end run() +""" java.awt.Font: Font(String name, int style (0=plain?), int size) """ +""" +class ImageProcessor: + drawString(java.lang.String s, int x, int y) + Draws a string at the specified location using the current fill/draw value. + drawString(java.lang.String s, int x, int y, java.awt.Color background) + Draws a string at the specified location with a filled background. +""" +'''ImageProcessor: + drawRoi(Roi roi): Draws the specified ROI on this image using the stroke width, stroke color and fill color defined by roi.setStrokeWidth, roi.setStrokeColor() and roi.setFillColor(). +''' + + + +def midpoint( p1, p2 ): + ''' return the midpoint as [x,y] list. + Takes two points, also as a pair of [x,y] lists. + ''' + + x1 = min( p1[0], p2[0] ) + y1 = min( p1[1], p2[1] ) + x2 = max( p1[0], p2[0] ) + y2 = max( p1[1], p2[1] ) + + xm = (x2-x1)/2. + x1 + ym = (y2-y1)/2. + y1 + + return [int(xm), int(ym)] +#end midpoint() + + + + +run() # Run the script function! + diff --git a/Analyze/Microscope Measurement Tools/Microscope_Calibrations_user_settings.py b/Analyze/Microscope Measurement Tools/Microscope_Calibrations_user_settings.py new file mode 100644 index 0000000..d309a8a --- /dev/null +++ b/Analyze/Microscope Measurement Tools/Microscope_Calibrations_user_settings.py @@ -0,0 +1,99 @@ +''' + FIJI Plugin +Configuration for Microscope Calibrations v1.py +Demis D. John, Praevium Research Inc., 2015 + +Please make sure the lists `names`, `cals` and `units` all have the same number of items! + +After you edit this file, be sure to delete the corresponding *.pyclass file and restart Fiji for the settings to take effect. +''' + + +""" +################################ + Microscope Image settings +################################ + +Microscope scaling/pixel-size calibration settings. +""" + +# The names of the microscope calibrations (shows up as the radio button names): +names = ['FluoroScope 5x', 'FluoroScope 10x', 'FluoroScope 20x', 'FluoroScope 100x', 'FluoroScope 200x'] + + +# The 'pixel-per-unit' obtained from the "Set Scale..." Dialog, for each named calibration above: +cals = [0.5, 1.0, 3.0, 5.0, 10.0] +# This is just 1/pixel_width, in case you were wondering. + + +# units for each calibration: +units = ['um','um','um','um','um'] +# for identical units for all cals, use the following line instead (uncomment): +#units = ['um' for x in cals] # list-comprehension with constant `um` + + +# Aspect Ratio for each calibration (default should be 1.0 ) +# Ratio is defined as so: pixelHeight = pixelWidth * AspectRatio +# The following sets the aspect ratio to 1 for all calibrations: +aspect_ratio = [ 1.0 for x in cals ] # list-comprehension with constant `1` +# Uncomment the following line to use custom aspect ratios: +#aspect_ratio = [1, 1, 1, 1, 1] + + + + + +""" +################################ + Draw Line settings +################################ + +Settings for the script "Draw Measurement - Line + +Colors are specified as: + [R, G, B, transparency] values, from 0->1.0. + Leave last value as 1 for completely opaque. + Eg. opaque red would be [1,0,0, 1] + and half-transparent blue would be [0,0,1, 0.5] + opaque black is [0,0,0, 1] + opaque white is [1,1,1, 1] +""" + +linethickness = 5.0 # in pixels +linecolor = [ 0, 0.8, 0, 0.75] +textsize = 30 # text height in pixels, I think +textcolor = [ 0, 0.8, 0, 1.0] +textbackgroundcolor = [ 0, 0, 0, 0.5] # background color behind text. +#textbackgroundcolor = None # set to None for no background - uncomment this line +texttoleft = True # put text on right side of last point? + + + + + + + + + + +''' +--------------------------------------------------------- +Warn user if they run this file as a stand-alone plugin. +''' + +def run(): + ''' If someone tries to run this file by itself, warn them of their error. Unfortunately, since I was too lazy to make Microscope_Calibrations a full plugin (rather than a script), this accompanying settings file will show up in the Scripts menu.''' + from ij.gui import GenericDialog + + gd = GenericDialog("Microscope_Calibrations_user_settings.py") + gd.addMessage("This file is only for setting the microscope calibrations and settings for the plugins 'Microscope Measurement Tools'.\nNothing is done when this settings file is run by itself.\nPlease open this file in a text editor instead, to edit the calibrations.\n \n" + \ + "The file should reside in a path like the following\n" + \ + "Fiji.app/plugins/Scripts/Analyze/Microscope Measurement Tools/Microscope_Calibrations_user_settings.py\n " + "\n" + \ + "Changes to the settings file are not automatically picked up by Fiji. The workaround is to\n 1) Quit Fiji.\n 2) Delete the '$py.class' file 'Microscope_Calibrations_user_settings$py.class'\n 3) Open Fiji. Make sure the new settings show up in 'Choose Microscope Calibration'." ) + + gd.showDialog() +#end run() + +if __name__ == '__main__': + run() # run the above function if the user called this file! + diff --git a/Microscope Meas. - Calibration instructions.pdf b/Microscope Meas. - Calibration instructions.pdf new file mode 100644 index 0000000..52a30c8 Binary files /dev/null and b/Microscope Meas. - Calibration instructions.pdf differ