//It might be easier to process images from a folder
//You would need to customize this path to your computer
InputDir="/Users/nicolas/Desktop/Input/";
// You could also get a prompt to select the InputDir
// InputDir = getDirectory("Choose a Directory ");
// And to save results in an output folder
// You would need to customize this path to your computer
//OutputPath="/Users/nicolas/Desktop/Output/";
//You could also create a new folder based on the name of the input folder
ParentPath=File.getParent(InputDir);
InputDirName=File.getName(InputDir);
OutputDir=ParentPath+File.separator+InputDirName+"_Results";
i=1;
while(File.exists(OutputDir)){
OutputDir=ParentPath+File.separator+InputDirName+"_Results"+"-"+i;
i++;
}
File.makeDirectory(OutputDir);
OutputPath=OutputDir+File.separator;
function GetBnCValues(ChI) {
ListOfMin=newArray(ListFile.length);
ListOfMax=newArray(ListFile.length);
// Measure Min and Max for all open images
for(Filei=0; Filei<ListFile.length; Filei++){
FilePath=InputDir+ListFile[Filei];
open(FilePath);
ImageName=getTitle();
//selectWindow(ImageName);
Stack.setChannel(ChI);
resetMinAndMax();
run("Enhance Contrast", "saturated=0.35");
getMinAndMax(min, max);
ListOfMin[Filei]=min;
ListOfMax[Filei]=max;
selectWindow(ImageName); run("Close");
}// end of for FileI
// Get Statistics
Array.getStatistics(ListOfMin, MinMin, MaxMin, MeanMin, StdDevMin);
Array.getStatistics(ListOfMax, MinMax, MaxMax, MeanMax, StdDevMax);
return newArray(MinMin, MaxMax);
}// End of GetBnCValues function
function ApplyBnC(ChI, MinMin, MaxMax) {
for(Filei=0; Filei<ListFile.length; Filei++){
FilePath=InputDir+ListFile[Filei];
open(FilePath);
ImageName=getTitle();
//selectWindow(ImageName);
Stack.setChannel(ChI);
setMinAndMax(MinMin, MaxMax);
saveAs("Tiff", OutputPath+ImageName);
selectWindow(ImageName); run("Close");
}// end for Filei
}//end of function
ListFile=getFileList(InputDir);
run("Set Measurements...", "area mean standard modal min centroid center perimeter bounding fit shape feret's integrated median skewness kurtosis area_fraction stack display redirect=None decimal=3");
run("Clear Results");
// It might be faster to work in batchmdoe
setBatchMode(true);
// Create Arrays to store image info
ListOfCh=newArray(ListFile.length);
ListOfSlices=newArray(ListFile.length);
ListOfFrames=newArray(ListFile.length);
for (Filei=0; Filei<ListFile.length; Filei++){
FilePath=InputDir+ListFile[Filei];
open(FilePath);
ImageName=getTitle();
//selectWindow(ImageName);
getDimensions(width, height, channels, slices, frames);
ListOfCh[Filei]=channels;
ListOfSlices[Filei]=slices;
ListOfFrames[Filei]=frames;
selectWindow(ImageName); run("Close");
}// end for FileI
// Get some statistics
Array.getStatistics(ListOfCh, MinCh, MaxCh, MeanCh, StdDevCh);
Array.getStatistics(ListOfSlices, MinSlice, MaxSlice, MeanSlice, StdDevSlice);
Array.getStatistics(ListOfFrames, MinFrame, MaxFrame, MeanFrame, StdDevFrame);
//for (Filei=0; Filei<ListFile.length; Filei++){
//FilePath=InputDir+ListFile[Filei];
//open(FilePath);
//ImageName=getTitle();
////selectWindow(ImageName);
// Process all chanels using two functions
for(ChI=1; ChI<MaxCh+1;ChI++){
MinAndMax=GetBnCValues(ChI);
MinMin=MinAndMax[0];
MaxMax=MinAndMax[1];
ApplyBnC(ChI,MinMin, MaxMax);
}
|