Created
August 10, 2017 11:10
-
-
Save lacan/1e4fc1d6c8ad17eb24747e0117039995 to your computer and use it in GitHub Desktop.
[Compute Distance Based on Edges] Determine Length of a rather homogeneous structure using Find Edges on graylevel image #Macro #ImageJ #Fiji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Determine Length of a rather homogeneous structure using Find Edges on graylevel image | |
* | |
* using an iterative approach to end up having exactly two peaks on the | |
* maximum finder. | |
* | |
* By Olivier Burri | |
* BioImaging And Optics Platform. | |
* | |
* In response to http://forum.imagej.net/t/how-calculate-the-pixels-distance/6478?u=oburri | |
*/ | |
name = getTitle(); | |
setTool("line"); | |
if(selectionType != 5) | |
waitForUser("Draw a Line and press OK"); | |
setBatchMode(true); | |
// RGB to 8-bit | |
if(bitDepth() == 24) { | |
run("8-bit"); | |
} | |
// Preprocessing | |
run("Select None"); | |
run("Duplicate...", "title=Edges"); | |
run("Median...", "radius=2"); //Smooth out small features, preserve edges | |
run("Find Edges"); | |
// Get the 1D Plot | |
run("Restore Selection"); | |
coords = getProfile(); | |
// Get Parametric curve equation | |
getSelectionCoordinates(x,y); | |
a = (x[1] - x[0]) / coords.length; | |
b = (y[1] - y[0]) / coords.length; | |
// Retuns the position in the coords array where the peaks are located | |
res = getTwoPeaks(coords); | |
// Display the two peaks on the image | |
x1 = a*res[0]+x[0]+0.5; | |
x2 = a*res[1]+x[0]+0.5; | |
y1 = b*res[0]+y[0]+0.5; | |
y2 = b*res[1]+y[0]+0.5; | |
// Compute the distance between the two peaks | |
d = sqrt(pow(x1 - x2, 2) + pow(y1 - y2,2)); | |
//Add to results | |
nR = nResults; | |
// Image Name | |
setResult("Label", nR, name); | |
//Length In Pixels | |
setResult("Length", nR, d); | |
// Calibrated Length | |
getVoxelSize(vx,vy,vz,U); | |
setResult("Length ["+U+"]", nR, d*vx); | |
selectImage(name); | |
makeLine(x1,y1,x2,y2); | |
setBatchMode(false); | |
/* | |
* This function tries to find exactly two peaks | |
* Iteratively changing the tolerance until only the two largest peaks are found on the image | |
*/ | |
function getTwoPeaks(array) { | |
minTolerance = 2; | |
maxTolerance = 1000; | |
peaks = newArray(0); | |
i=0; | |
while (peaks.length != 2 && i< 1000) { | |
tol = (minTolerance + maxTolerance) /2; | |
peaks = Array.findMaxima(coords, tol, 1); | |
if(peaks.length > 2) { | |
minTolerance = tol; | |
} | |
if(peaks.length < 2) { | |
maxTolerance = tol; | |
} | |
i++; | |
} | |
return peaks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment