Cross-posted from Image SC Forum and Reddit
(Edit: I found the problem: my for loop isn’t working because it doesn’t recognize my table as the Results table. Anyone know how to make it do that?)
(Edit 2: Got it to recognize my table by renaming. Next problem is that I can't get the Math>Subtract command to work, even if I just hard code it with a number. It seems like the command is doing nothing at all. Even when I go through the GUI by clicking (which works) while recording and then re-do the recorder commands it doesn't work.)
I am writing a personal macro, and I am stuck on how to get a value from the Results table. Should be a simple fix.
Basically I want to search through the Results table by filename/label and pull out a value. I am using a for loop to query/match filename to label, and then using that index to update a pre-defined variable. But for some reason, the variable is not being updated. I think this is because the loop query is not matching the filename to a row Label.
The core portion of my script is below:
MeanBkgd = 0 // Setting 0 as a default placeholder
for(iRes=0; iRes<nResults; iRes++){
if (getResultString("Label", iRes) == file) {
MeanBkgd = getResult("Mean", iRes);
}
}
print(MeanBkgd); // Verify stored value
run("Subtract...", "value = MeanBkgd"); // Subtract background from image
Extra details:The macro I am writing is meant to process all of the images in a directory by subtracting out the mean background intensity of each image. In a previous step, I already measured the mean background intensities and saved them as a csv. Here in this macro, I am trying to pair those values back to the original images and then subtract them from the images.
However when I run the code below, the background is not removed from the images because the value of MeanBkgd is never changed from 0 (identified from printing MeanBkgd after the loop). Furthermore, I can guess that the query isn’t matching because it never prints “Match”.
// @File(label = "Input directory", style = "directory") input
// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = ".zvi") suffix
/*
* Macro to open all images in a folder, append to a stack and set thresholds for each channel
*/
function processFolder(input) {
// Setup
close("*");
open(input + "/BackgroundLevels.csv");
list = getFileList(input);
// Process Files by subtracting background
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + list[i]))
processFolder("" + input + list[i]);
if(endsWith(list[i], suffix))
SubtractBackground(input, output, list[i]);
}
// Combine and Threshold
run("Concatenate...", "all_open open");
selectWindow("Untitled");
setAutoThreshold("Triangle dark no-reset stack");
getThreshold(lower, upper);
// Output
print(lower); // Can't concatenate to text because technically an array
file = File.open(output + "/Threshold.txt");
print(file, lower);
}
function SubtractBackground(input, output, file) {
// Open Image
setBatchMode(false);
run("Bio-Formats", "open=[" + input + "/" + file +"] color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
// Set null value
MeanBkgd = 0;
// Get values from imported Results
for (iRes=0; iRes < nResults; iRes++) { // What if there are not 1 match (0 or 2+)?
if (getResultString("Label", iRes) == file) {
MeanBkgd = getResult("Mean", iRes);
print("Match"); // Verify match
}
}
print(MeanBkgd); // Verify stored value
// Subtract background from image
run("Subtract...", "value = MeanBkgd");
}
processFolder(input);
I adapted this code for finding a value from a Results table from https://stackoverflow.com/a/33723712/14860873
I have also tried treating MeanBkgd as an array and trying to update/append it, but I couldn’t get this to work either. I haven’t found any help online because the ImageJ language is different than other coding languages and less popular.
Anyone have any ideas?
I wonder if the spaces in the Label names could be causing an issue?
The reason the Math > Subtract command isn't working is because the line
uses the literal term
MeanBkgdas the value rather than the variable calledMeanBkgd. So if you change it tothe variable will be substituted in the call to Subtract.