-
Notifications
You must be signed in to change notification settings - Fork 2
/
Flat_Field_Correction.ijm
166 lines (139 loc) · 4.36 KB
/
Flat_Field_Correction.ijm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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 <http://www.gnu.org/licenses/>.
//
//
// 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<list.length; i++) {
if (endsWith(list[i], ".tif"))
count++;
else
folder++;
}
}
function getSliceLabel(StackName,tempName) {
selectWindow(StackName);
for (i=0; i<nSlices; i++) {
setSlice(i+1);
tempName[i]=getMetadata("Label");
}
return tempName;
}
function setSliceLabel(StackName,sliceName) {
selectWindow(StackName);
for (i=0; i<nSlices; i++) {
setSlice(i+1);
setMetadata(sliceName[i]);
}
}