Analysis of profile plot

macro
Tags: #<Tag:0x00007fb87acdfd18>

#1

Hello,

I have profile plots like this:
Plot

And I would like to write a macro to identify the x coordinates of the beginning and end of the data different from “0” (excluding the high peak on the right).

I have tried to create an array, an index of this array, and then a loop to identify these values but it does not work because I struggle manipulating arrays… Could anyone spare a minute to get me started?


#2

Good day Olivier,

here is a mcro that does what you’ve described:

// IJ-macro begin
// create test image
newImage("Untitled", "8-bit random", 256, 256, 1);
makeRectangle(0, 0, 64, 256);
setBackgroundColor(0, 0, 0);
run("Clear", "slice");
makeRectangle(168, 0, 32, 256);
run("Clear", "slice");
makeRectangle(201, 0, 55, 256);
run("Clear", "slice");
// get desired x-values
run("Select All");
p = getProfile();
zeroBound( p );
p = Array.findMaxima( p, 0 );
Array.sort( p );
print( p[0]+1, p[1] );
run( "Select None" );
exit();
///////////////////////
function zeroBound( a ) {
	for ( i = 1; i < a.length; i++ ) {
		if ( (a[i] > 0 && a[i-1] == 0) || (a[i-1] > 0 && a[i] == 0) ) a[i-1] = 1; else a[i-1] = 0;
	}
	a[a.length-1] = 0;
}
// IJ-macro end

Regards

Herbie


#3

Thank you so much Herbie! This does the trick. The only thing is that the coordinates are based on 256 pixels now, instead of the original 1234. I tried to amend your script but didn’t succeed…


#4

Olivier,

I don’t understand

The only thing is that the coordinates are based on 256 pixels now […]

Did you try to understand the macro?

The call

getProfile();

does the same as “Plot Profile”, it gets the values of the profile but without plotting it.

So if you run the macro starting from the comment

// get desired x-values

on your image (not the profile plot), you will get the desired coordinates.

Please learn how to write ImageJ-macros:
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/developer/macro/functions.html

HTH

Herbie


#5

Ok, I misunderstood (should have checked the code carefully), I thought you analysed the line plot. Thank you again!