+open("http://www.antweb.org/images/casent0249453/casent0249453_h_1_high.jpg");
+//open("http://www.antweb.org/images/casent0902634/casent0902634_h_1_high.jpg");
+getDimensions(width, height, channels, slices, frames);
+//-- duplicate the bottom right of the image to try and find the scale bar
+makeRectangle(width-floor(width*0.4), height-150, floor(width*0.4), 150);
+run("Duplicate...", "title=scale");
+run("Median...", "radius=1");
+setAutoThreshold("Default light");
+setOption("BlackBackground", true);
+run("Analyze Particles...", "size=100-Infinity show=Masks clear add in_situ");
+//-- for each object check concavity or solidity
+numObj=roiManager("count");
+//-- record the squareness and the width
+squareness=newArray(numObj);
+for (i = 0; i < numObj; i++) {
+ roiManager("select", i);
+ //--calculate "squareness"
+ sqW[i]=List.getValue("Width");
+ //-- don't really need this other than to calculate the bounding box so replace every loop
+ sqH=List.getValue("Height");
+ squareness[i]=List.getValue("Area")/(sqW[i]*sqH);
+//Array.print(squareness);
+//-- Rank the array from highest squareness to lowest
+squarenessRank=Array.reverse(Array.rankPositions(squareness));
+//-- may want to check that the squareness is above, eg. 0.96
+//-- now pull the width to use for calibration
+calibVal=sqW[squarenessRank[0]];
+//Array.print(squarenessRank);
+//-- Set the scale on the origial
+run( "Set Scale...", "distance=" + calibVal + " known=1 unit=mm" );
+//-- add the scale bar as an overlay
+roiManager("select", squarenessRank[0]);
+getSelectionBounds(sbX, sbY, sbW, sbH);
+makeRectangle(sbX+width-floor(width*0.4), sbY+height-150, sbW, sbH);
+Overlay.addSelection("magenta", 3);