Writing a macro to save image stack as series of separate files
Posted by jlw on Sep 10, 2014; 10:51pm
URL: http://imagej.273.s1.nabble.com/Writing-a-macro-to-save-image-stack-as-series-of-separate-files-tp5009571.html
Hi, I'm trying to write a macro to open image stacks, manually look at them and adjust brightness/contrast, then save as a series of separate files, each in its own folder. The way the files are named is not flexible because they have to be named in a certain way to feed into another Matlab program that is already written. Here is the code I have:
function prepareForMatlab(input, output, filename) {
setBatchMode(false);
open(input + filename);
run("Brightness/Contrast...");
waitForUser('Adjust brightness and contrast');
run("Close"); //The filename is automatically extracted but we have to modify this to get it without the extension
name = getTitle; //Includes .tif
dotIndex = indexOf(name, ".");
title = substring(name, 0, dotIndex); //name without the extension
mouseAndRegionNumber = substring(output,33); //Extracts the mouse and region number, in this case delta_1. Will change if file structure changes
print(mouseAndRegionNumber);
newFileName = mouseAndRegionNumber + "_" + title + "_";
print(newFileName);
firstPlaneName = output + "/" + title + "/";
print(firstPlaneName);
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3 save=firstPlaneName");
run("Close All");
}
input = "/C:/.../Raw/"; //Here i've omitted some directories with a ... just for the purpose of posting this question
output = "/C:/.../delta_1";
list = getFileList(input);
for (i = 0; i < list.length; i++){
setBatchMode(false);
prepareForMatlab(input, output, list[i]);
}
I know this code is probably not the most elegant way to do this (accepting suggestions on that as well!) but the problem I'm having now is that in the line
run("Image Sequence... ", "format=TIFF name=newFileName start=0 digits=3 save=firstPlaneName")
newFileName is not treated as a variable with a string value but rather as a string itself (so all the files end up having the word newFileName in them). How do I fix this?
This code also has another weird problem where the images open one by one and allow the user to move the sliders to see different z planes and then adjust brightness/contrast. This works fine unless you try to switch back and forth between toggling z planes and toggling brightness. (Then after the user has clicked ok on the current image, it cannot open the next one - there is an error "no image open") No idea why this would happen...it works fine if you do one and then the other, just not if you switch back and forth.
Any help would be greatly appreciated!!!