-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_Breast_Masks_AIA.ijm
141 lines (123 loc) · 4.2 KB
/
Create_Breast_Masks_AIA.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
// Image/J (Fiji) macro to implement AIA Breast mask extraction.
// Input is a directory containing DCM format files.
// Second input is the complete path to the AIA Object Mask executable
// Separator between inputs is a space.
// Input DCMs should be inverted LUT images (air=dark body=bright).
// ex.) beware quotes
// java -jar E:\source\Java\Fiji\fiji-win64\Fiji.app\jars\ij-1.52p.jar -ijpath E:\source\Java\Fiji\fiji-win64\Fiji.app -batch "E:\source\Java\Fiji\fiji-win64\Fiji.app\macros\Create_Breast_Masks_AIA.ijm" "E:\data\CDor_3\mammo\Jordana_CEM\Patient_001 C:\Users\eric\kauai\bin\ObjectMask.exe"
// by keesh ([email protected])
// This function returns the value of the specified
// tag (e.g., "0010,0010") as a string. Returns ""
// if the tag is not found.
// TODO -- Fix this for
function getTag(tag) {
info = getImageInfo();
index1 = indexOf(info, tag);
if (index1==-1) return "";
index1 = indexOf(info, ":", index1);
if (index1==-1) return "";
index2 = indexOf(info, "\n", index1);
value = substring(info, index1+1, index2);
return value;
}
// Checks the 0008,0008 Image Type: ORIGINAL\PRIMARY\\LOW_ENERGY wrt HIGH_ENERGY for the active image
function isHighEnergy() {
high_image = -1;
image_type = getTag("0008,0008");
if (image_type != ""){
image_type = toLowerCase(image_type);
if (indexOf(image_type, "low") != -1){
high_image = 0;
}
else if (indexOf(image_type, "high") != -1){
high_image = 1;
}
}
return high_image;
}
function applyAIAMaskExtraction(input, output, filename, obj_mask_exec) {
// const
niiSuffix = ".nii";
// set up
print("Processing DCM file: " + input + filename);
open(input + filename);
run("Duplicate...", "title=I1");
dotIndex = lastIndexOf(filename, ".");
title = substring(filename, 0, dotIndex);
uid = getTag("0020,000E");
uid = String.trim(uid);
is_high = isHighEnergy();
if(is_high == -1) {
print("Image is not low or high energy: " + title);
return;
}
is_high_str = "high";
if (!is_high){
is_high_str = "low";
}
// May need to make easier for parser
resultName= "_" + is_high_str + "mask";
seriesId = "_" + uid;
origLabel = "_orig";
// DICOM processing assumed
// Invert image to obtain an attenuation image on write
run("Invert");
niiFile = output + title + origLabel + seriesId + niiSuffix;
print("Converted image: " + title);
run("NIfTI-1", "save=["+niiFile+"]");
open(niiFile);
maskInputFile = niiFile;
outputFile = output + title + resultName + seriesId + niiSuffix;
// need to have each arg as a sep string
// bgnd=0, fgnd=1
exec_out = exec(obj_mask_exec, "-t", "mammo", "-d", "uint8", "-i", maskInputFile, "-m", outputFile);
print(exec_out);
print("Created mask for energy image: " + title);
// BEG DBG CODE
open(outputFile);
run("Duplicate...", "title=M1_DBG");
selectImage("M1_DBG");
run("Multiply...", "value=255.000"); // for viz
selectImage("I1");
setOption("ScaleConversions", true);
run("8-bit");
run("Merge Channels...", "c1=M1_DBG c4=I1 create keep");
selectImage("Composite");
resultName="_thresh_overlay";
resultSuffix=".png";
outputFile = output + title + resultName + seriesId + resultSuffix;
saveAs("PNG", outputFile);
// END DBG CODE
//Clean up
close("*"); // Closes all images
} // apply...
//NOTE-- Using batch mode causes weird Windows focus errors on second pass after run LowpassFilters if the class is not compiled.
setBatchMode(true);
args = getArgument();
args_array = split(args, "");
if (args_array.length==1 && args_array[0]=="--version") {
print("0.0.1");
exit();
}
input_output = args_array[0];
obj_mask_exec = args_array[1];
low_series_inst_uid = newArray();
low_base_fname = newArray();
if ( !File.isDirectory(input_output)) {
print("Invalid input directory: " + input_output);
exit();
}
if ( ! File.exists(obj_mask_exec)) {
print("Invalid ObjectMask executable: " + obj_mask_exec);
exit();
}
list = getFileList(input_output);
input_output = input_output + File.separator;
for (i = 0; i < list.length; i++) {
if( endsWith(list[i], ".dcm") ){
// input dir, output dir, filename
applyAIAMaskExtraction(input_output, input_output, list[i], obj_mask_exec);
}
}
setBatchMode(false);
print("fin")