Skip to content

Instantly share code, notes, and snippets.

@ved-sharma
Last active June 10, 2021 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ved-sharma/016d890054f71cde058f55d9d2cbf18c to your computer and use it in GitHub Desktop.
Save ved-sharma/016d890054f71cde058f55d9d2cbf18c to your computer and use it in GitHub Desktop.
// ImageJ/Fiji macro to count no. of shapes in an image (Cirlces, Squares and Triangles)
// Macro written in response to the question on image.sc forum:
// https://forum.image.sc/t/can-i-count-different-shape-with-the-imagej/53497
//
// Version 2: added the interpolate ROI to smoothen the ROI boundary
// Author: Ved P Sharma (June 10, 2021)
run("Select None");
roiManager("reset");
run("Invert");
setAutoThreshold("Default dark");
//run("Threshold...");
//setThreshold(218, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Fill Holes");
run("Create Selection");
roiManager("Add");
roiManager("Select", 0);
roiManager("Split");
roiManager("Select", 0);
roiManager("Delete");
roiManager("Deselect");
n = roiManager('count');
circles = 0; triangles = 0; squares = 0;
for (i = 0; i < n; i++) {
roiManager('select', i);
run("Interpolate", "interval=1 smooth adjust");
List.setMeasurements;
circ = List.getValue("Circ.");
if(circ > 0.9)
circles++;
else if (circ<0.9 && circ > 0.78)
squares++;
else
triangles++;
}
print("No. of Circles = "+circles);
print("No. of Squares = "+squares);
print("No. of Triangles = "+triangles);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment