// Copyright 2018-2023 Zhou XU
//
// This file is part of ImageJ plugin EM-tool.
//
// EM-tool is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EM-tool is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EM-tool. If not, see .
//
//
// Apply flat field correction to all images in a folder
// Written by Zhou XU
// Last updated on 2019 Apr 30
RawImgFolder = getDirectory("Select the Raw Image Folder");
RawImgList = getFileList(RawImgFolder);
NumberOfFiles = RawImgList.length;
// print("Raw Images: \n", FlatImgName);
count = 0;
folder = 0;
countFiles(RawImgFolder);
print("Total number of .tif files: ", count);
run("Image Sequence...",
"open=[&RawImgFolder]"+
" number="+count+
" starting=1"+
" increment=1"+
" scale=100 "+
"file=[.tif] "+
"sort");
RawName = getTitle();
tempName = newArray(nSlices);
ImgLabel = getSliceLabel(RawName,tempName);
autoHeight = getHeight();
autoWidth = getWidth();
msg = "Please select the resolution of your \nraw image. This will crop the dark \nimage and flat field image using \nthe numbers to remove databar:";
Dialog.create("Image Processing");
Dialog.addChoice("Type:", newArray("8-bit", "16-bit", "32-bit"));
Dialog.addMessage(msg);
Dialog.addCheckbox("Auto (Use raw image resolution)", true);
Dialog.addNumber("Width:", autoWidth);
Dialog.addNumber("Height:", autoHeight);
Dialog.show();
width = Dialog.getNumber();
height = Dialog.getNumber();;
type = Dialog.getChoice();
auto = Dialog.getCheckbox();
DarkImgPath = File.openDialog("Select a Dark Image File");
//open(path); // open the file
DarkImgDir = File.getParent(DarkImgPath);
DarkImgName = File.getName(DarkImgPath);
print("DarkImgPath:", DarkImgPath);
print("Dark Img Name:", DarkImgName);
open(DarkImgPath);
if (auto==true)
{
width = autoWidth;
height = autoHeight;
}
makeRectangle(0, 0, width, height);
run("Crop");
FlatImgPath = File.openDialog("Select a Flat Image File");
//open(path); // open the file
FlatImgDir = File.getParent(FlatImgPath);
FlatImgName = File.getName(FlatImgPath);
print("FlatImgPath:", FlatImgPath);
print("Flat Img Name:", FlatImgName);
open(FlatImgPath);
if (auto==true)
{
width = autoWidth;
height = autoHeight;
}
makeRectangle(0, 0, width, height);
run("Crop");
selectWindow(FlatImgName);
selectWindow(DarkImgName);
run("Calculator Plus", "i1=[&FlatImgName] i2=[&DarkImgName] operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=0 create");
selectWindow("Result");
rename("F-D");
getStatistics(area, mean, min, max, std);
selectWindow(RawName);
selectWindow(DarkImgName);
run("Calculator Plus", "i1=&RawName i2=[&DarkImgName] operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=0 create");
selectWindow("Result");
rename("R-D");
selectWindow("R-D");
selectWindow("F-D");
run("Calculator Plus", "i1=R-D i2=F-D operation=[Divide: i2 = (i1/i2) x k1 + k2] k1=&mean k2=0 create");
selectWindow("Result");
rename("Corrected Image Stack");
setSliceLabel("Corrected Image Stack",ImgLabel);
print("\n");
close("\\Others");
File.makeDirectory(RawImgFolder + "Corrected")
selectWindow("Corrected Image Stack");
for (i=1; i<=nSlices; i++)
{
showProgress(i, nSlices);
setSlice(i);
fileName = getMetadata("Label");
newPath = RawImgFolder + "Corrected\\"+fileName;
run("Copy");
newImage("Untitled", type, width, height, 1);
run("Paste");
saveAs("Tiff", newPath);
close();
}
function countFiles(dir) {
list = getFileList(dir);
for (i=0; i