Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lacan
Created September 3, 2020 16:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/ecbd3e836934d0f39022570130fd795d to your computer and use it in GitHub Desktop.
Save lacan/ecbd3e836934d0f39022570130fd795d to your computer and use it in GitHub Desktop.
[Use of Hough Transform to locate incomplete circles in images] This script segments SEM images containing pores and tries to fit circles to the result
// Hough Transform to find circles,
// Author: Olivier Burri, from a request of the Image.sc forum:
// https://forum.image.sc/t/find-circles-with-incomplete-boundaries/42357
// Due to the simple nature of this code, no copyright applies
#@ImagePlus image
#@Double hough_threshold (value=0.55)
// SOme housekeeping
original = getTitle();
close("\\Others");
roiManager("Reset");
//Remove the bottom of the image
run("Specify...", "width=1024 height=884 x=0 y=0");
run("Crop");
// Prepare for Segmentation
run("Duplicate...", "title=[Hough]");
run("Smooth");
// Remove unvanted background using rolling ball algorithm
run("Subtract Background...", "rolling=10");
run("Smooth");
// Apply laplacian to enhance edges
run("FeatureJ Laplacian", "compute smoothing=2");
// Auto-threshold and over-estimate particles
setAutoThreshold("Moments");
// Remove particles smaller than 50px2
run("Analyze Particles...", "size=50-Infinity show=Masks");
// Use a Hough transform on the mask to find likely candidates
run("Hough Circle Transform","minRadius=10, maxRadius=24, inc=1, minCircles=1, maxCircles=500, threshold="+hough_threshold+", resolution=200, ratio=1.0, bandwidth=10, local_radius=10, reduce show_mask show_scores results_table");
// BUG: ImageJ does not wait for the Hough plugin to finish, so we have to check and wait until it is done
// we do this by checking the existence of the 'Score map' image
done = false;
while (!done) {
done = isOpen("Score map");
wait(500);
}
// Merge all images for easy viewing
selectImage(image);
// Need to make all 32-bit to match score image
run("32-bit");
selectImage("Mask of Hough Laplacian");
run("32-bit");
// Give nice lookup table
selectImage("Score map");
resetMinAndMax();
run("mpl-viridis");
// Merge and display as stack
run("Merge Channels...", "c1=["+original+"] c2=[Mask of Hough Laplacian] c3=[Score map] create keep");
Stack.setDisplayMode("color");
rename(orignal+"_Results");
// Results table now contains number of circles and sizes
@NJump24
Copy link

NJump24 commented May 11, 2023

Has anyone had the problem where the circle diameter is being underestimated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment