Posted by
Rasband, Wayne (NIH/NIMH) [E] on
URL: http://imagej.273.s1.nabble.com/How-do-I-get-the-pixel-coordinates-of-the-minimum-value-tp5009363p5009420.html
On Aug 26, 2014, at 8:43 PM, TLowe wrote:
> This routine draws a line, gets the profile and returns the pixel values. How
> do I get the pixel coordinates of the minimum value?
The macro below demonstrates how to get the pixel coordinates of the minimum value along a line selection. It opens the "Cell Colony" sample image, creates a line selection, uses Edit>Selection>Interpolate to resample the line at 1 pixel intervals, gets the coordinates of the points along the line, finds the coordinates of the point with the smallest value, uses Image>Zoom>To Selection to zoom in on the line, adds the line (in red) to an overlay, adds the point found (in yellow) to the overlay, and prints the coordinates and value to the "Log" window. The image at
http://wsr.imagej.net/images/MinValueAlongLine.pngshows what the output of the macro looks like.
The 'adjust' option of the "Interpolate" command ("Adjust interval to match") adjusts the sampling interval so that it divides evenly into the length of the line and the end of the last interval is an the end of the original line. This feature, added in ImageJ 1.49e, was contributed by Norbert Vischer.
The yellow min value point will not be exactly on the red line unless you upgrade to the latest ImageJ daily build (1.49h6), which fixes a bug that caused the makePoint() macro function to not work correctly with non-integer coordinates.
-wayne
run("Cell Colony (31K)");
makeLine(36, 142, 89, 98);
run("Interpolate", "interval=1 adjust");
getSelectionCoordinates(x, y);
min = 99999;
minx=0; miny=0;
for (i=0; i<x.length; i++) {
value = getPixel(x[i],y[i]);
if (value<min) {
min = value;
minx=x[i];
miny=y[i];
}
}
run("To Selection");
Overlay.addSelection("red");
makePoint(minx, miny);
Overlay.addSelection("yellow");
print("x="+minx+", y="+miny," value="+min);
> //setTool("line");
> makeLine(746, 344, 534, 582);
> run("Clear Results");
> profile = getProfile();
> for (i=0; i<profile.length; i++)
> setResult("Value", i, profile[i]);
> updateResults;
> --
> View this message in context:
http://imagej.1557.x6.nabble.com/How-do-I-get-the-pixel-coordinates-of-the-minimum-value-tp5009363.html> Sent from the ImageJ mailing list archive at Nabble.com.
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html