Getting length measurements in Plot Profile Macro

Hello.
With the help of a colleague, I’ve made a fiji macro while ago, to facilitate my analysis.
It basically opens images from an input folder, one at a time, makes a composite projection, and asks me to draw a line on the image. Once I’ve done that, the macro automatically measures fluorescence intensity along the line (using the getProfile function), for each channel, and gives me an excel file with the measurements.
The problem is that getProfile does not keep the length measurement of each pixel. Instead, it labels pixels as 1,2,3,4…n, based on their row number.
I’ve added a line in my code to work around that:
for (j=0, scale=0; j<profile.length; j++, scale+=0.071831)
By doing this, each pixel is asigned a “length value” (basically the scaling factor in bold, multiplied the row number).
I want to not have to rely on that manually determined scaling factor, but keep the original length measurements from the “plot profile” function, that are lost when using “getProfile”.
I’ve spent the past few hours working on that, unsuccessfully, as my programming skills are rather limited.
Could anyone help, please?

George

P.S. Here’s my macro:

dir1=getDirectory(“Input”);
print(dir1);
dir2=getDirectory(“Output”);
list=getFileList(dir1);

for(i=0; i<list.length; i++){
roiManager(“reset”);
path=dir1+list[i];
print(path);
run(“Bio-Formats Importer”, “open=[”+path+"] color_mode=Default view=Hyperstack stack_order=XYCZT");
name=File.nameWithoutExtension;
originalID=getImageID();
Stack.getDimensions(width, height, channels, slices, frames);
run(“Z Project…”, “projection=[Max Intensity]”);
run(“Channels Tool…”);
Stack.setChannel(1);
run(“Blue”);
Stack.setChannel(2);
run(“Green”);
Stack.setChannel(3);
run(“Red”);
Stack.setDisplayMode(“composite”);
Stack.setChannel(2);
setTool(“line”);
waitForUser(“Draw a line and press OK”);
roiManager(“add”);

run("Clear Results");
for(n=1;n<=channels; n++ ){
Stack.setChannel(n);
// Get profile and display values in "Results" window
profile = getProfile();
print(profile.length);
for (j=0, scale=0; j<profile.length; j++, scale+=0.071831){
setResult("distance",j, scale);
setResult("Channel"+n,j, profile[j]);
setOption("ShowRowNumbers", true);
updateResults;
}

}
path = dir2+name+“channel”+n+"_"+"_profile.csv";
selectWindow(“Results”);
saveAs(“Results”, path);
selectImage(originalID);
saveAs(“tiff”,dir2+name);
close();
}

if (isOpen(“Results”)) {
selectWindow(“Results”);
run(“Close”);
}

selectWindow(“ROI Manager”);
run(“Close”);

The x coordinate is just the distance in pixels.
The scale corresponds to the pixelsize.
This you can extract from the image using:

getPixelSize(unit, pixelWidth, pixelHeight);
1 Like

Hi @GeorgeKan,

This slight modification in the script should work (just pasting the middle part):

run("Clear Results");
getPixelSize(unit, pixelWidth, pixelHeight);
for(n=1;n<=channels; n++ ){
	Stack.setChannel(n);
	// Get profile and display values in "Results" window
	profile = getProfile();
	print(profile.length);
	for (j=0; j<profile.length; j++){
	setResult("distance ("+unit+")", j, j*pixelWidth);
	setResult("Channel"+n, j, profile[j]);
	setOption("ShowRowNumbers", true);
	updateResults;
	}

Cheers,
Nico

1 Like

Your modification works perfectly Nico.
Thanks a lot to both of you!
Cheers,

George