 # Measuring distance between maximum points of a plot profile

Hello community!

I am trying to develop a macro, where I draw a line across an image, locate the points of the line which have the maximum intensities and measure the distance between these points.
This is as far as I managed to come, but I figured that the command “Array.findMaxima();” sorts the points according to ascending intensity and not to the x- or y-position of the line drawn:

intProf = getProfile();
maxValues = Array.findMaxima(intProf, 3600);
getSelectionCoordinates(xpoints, ypoints);
xmaxValues = newArray(maxValues.length);
ymaxValues = newArray(maxValues.length);
dxmaxValues = newArray(maxValues.length - 1);
dymaxValues = newArray(maxValues.length - 1);
distance = newArray(maxValues.length - 1);
for (i = 0; i < maxValues.length; i++){
xmaxValues[i] = xpoints[maxValues[i]];
ymaxValues[i] = ypoints[maxValues[i]];
}
for (i = 0; i < maxValues.length - 1; i++) {
dxmaxValues[i] = xmaxValues[i + 1] - xmaxValues[i];
dymaxValues[i] = ymaxValues[i + 1] - ymaxValues[i];
distance[i] = pow((pow(dxmaxValues[i], 2) + pow(dymaxValues[i], 2)), 0.5);
}
makeSelection(“point”,xmaxValues,ymaxValues);
Array.show(Distance);

I would be very glad to know, if there is a way to sort the maximum points to their x-position, in order for the distance calculation to work fine.
Thank you in advance!

1 Like

Hey @ThanosT,

the following macro determines maxima in an array and sort them:

``````values = newArray(0, 0, 1, 2, 3, 2, 1, 0, 0, 0, 1, 4, 0);

// determine local maxima
tolerance = 1;
maxima = Array.findMaxima(values, tolerance);
Array.print(maxima);

// sort them
Array.sort(maxima);
Array.print(maxima);
`````` Please let us know if this helps!

Cheers,
Robert

Hello Robert!
I forgot that these values are the x-positions and I can just simply sort them… Thank you for noticing!

It works a lot better now, but I still have some minor problems with the values that the commands “xmaxValues[i] = xpoints[maxValues[i]];” and “ymaxValues[i] = ypoints[maxValues[i]];” get… They don´t fit the maximum values, they are a bit off…

I added these commands after getting the selection coordinates:
“xcoordinates = Array.resample(xpoints,intProf.length);” and “ycoordinates = Array.resample(ypoints,intProf.length);”
and then got the xmaxValues and ymaxValues from these new arrays, but the problem is still there.

Important to notice here, is that I am using a freehand line. With a straight line it works perfectly fine… Also I needed to increase the width of the line to 2, because with a width of one, the intensity profile looks totally wrong…

1 Like

Hi @ThanosT, did you manage to make this macro work? I am currently trying to make something very similar only I want to also identify the slice that have the higher maxima (a bit like finding the focus point).

I am equally struggling to extract the x and y points for the intensity peaks identified on my line selection. I am also using Array.findMaxima(); for that. But getSelectionCoordinates(xpoints, ypoints); only gives me the coordinates of the 2 extremities of the drawn line, not the identified points/peaks.

if you or anyone reading this have a solution that will be amazing
Thanks