-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_dicom_info.pl
executable file
·328 lines (277 loc) · 8.41 KB
/
get_dicom_info.pl
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env perl
#
# $Id: get_dicom_info.pl 8 2007-12-18 04:51:00Z zijdenbos $
#
# Routines for converting mri files to minc format. Must be included
# with routines for reading in data structures, specific to the input
# file format.
#
$VERSION = sprintf "%d", q$Revision: 8 $ =~ /: (\d+)/;
use FindBin;
use Getopt::Tabular qw(GetOptions);
use lib "$FindBin::Bin";
use DICOM::DICOM;
my $Help;
my $Usage;
my @Variables = ();
my $ProgramName = $0;
my @input_list = ();
my $PrintLabels = 0;
my $PrintedLabels = 0;
my $ErrorString = 'UNDEF';
my $UseSTDIN = 0;
&CreateInfoText;
my @args = &SetupArgTables;
&GetOptions (\@args, \@ARGV, \@input_list) || die "\n";
if($UseSTDIN) {
while(<STDIN>) {
chomp $_;
push @input_list, $_;
}
}
if (@input_list <= 0)
{
warn $Usage;
die "Please specify one or more input DICOM files\n";
}
if(@Variables <= 0)
{
warn $Usage;
die "Please specify one or more fields to display\n";
}
foreach my $filename (@input_list) {
my $dicom = DICOM->new();
$dicom->fill($filename);
# Get slice position and orientation (row and column vectors)
my(@position) =
# ImagePositionPatient (0x0020, 0x0032)
&convert_coordinates(&split_dicom_list(&trim($dicom->value('0020', '0032'))));
if (scalar(@position) != 3) {
warn "Warning: The file: $filename is not DICOM!\n";
push my @croft, $filename;
next;
}
# ImageOrientationPatient (0x0020, 0x0037)
my(@orientation) = &split_dicom_list(&trim($dicom->value('0020', '0037')));
if (scalar(@orientation) != 6) {
warn "************* Error reading slice orientation *************\n";
}
my(@column) = &convert_coordinates(@orientation[0..2]);
my(@row) = &convert_coordinates(@orientation[3..5]);
# Figure out normal and orientation
my(@normal) =
&vector_cross_product(\@column, \@row);
my @slc_dircos = &get_dircos(@normal);
my $slicepos = &vector_dot_product(\@position, \@slc_dircos);
# Print out variable labels
if(!$PrintedLabels && $PrintLabels) {
foreach $var (@Variables) {
if($$var[0] == -1) {
print $$var[1]."\t";
} else {
print $dicom->field(@$var, 'name')."\t";
}
}
print "\n";
$PrintedLabels = 1;
}
# Print out the requested vars
foreach $var (@Variables) {
if($$var[0] == -1) {
if($$var[1] eq 'filename') {
print $filename."\t";
} elsif($$var[1] eq 'slicepos') {
print $slicepos."\t";
}
} else {
if ($dicom->value(@$var) eq '') {
print "$ErrorString\t";
}
else {
print $dicom->value(@$var)."\t";
}
}
}
print "\n";
}
&showcroft();
# Subroutine to clean up files and exit
sub cleanup_and_die {
# Get message to print and exit status
local($message,$status) = @_;
if (!defined($status)) {$status = 0;}
if (defined($message)) {
print STDERR $message;
if ($message !~ /\n$/) {print STDERR "\n";}
}
$SIG{'INT'} = 'IGNORE';
$SIG{'TERM'} = 'IGNORE';
$SIG{'QUIT'} = 'IGNORE';
# Check for temp files
if (defined($tmpdir) && -e $tmpdir) {
print STDERR "Cleaning up temporary files.\n";
system "rm -rf $tmpdir";
}
exit($status);
}
# Subroutine to get a direction cosine from a vector, correcting for
# magnitude and direction if needed (the direction cosine should point
# along the positive direction of the nearest axis)
sub get_dircos {
if (scalar(@_) != 3) {
die "Argument error in get_dircos\n";
}
local($xcos, $ycos, $zcos) = @_;
# Get magnitude
local($mag) = sqrt($xcos**2 + $ycos**2 + $zcos**2);
if ($mag <= 0) {$mag = 1};
# Make sure that direction cosine is pointing along positive axis
local($max) = $xcos;
if (abs($ycos) > abs($max)) {$max= $ycos;}
if (abs($zcos) > abs($max)) {$max= $zcos;}
if ($max < 0) {$mag *= -1;}
# Correct components
$xcos /= $mag;
$ycos /= $mag;
$zcos /= $mag;
return ($xcos, $ycos, $zcos);
}
# Routine to convert world coordinates
sub convert_coordinates {
my(@coords) = @_;
$coords[0] *= -1;
$coords[1] *= -1;
return @coords;
}
# Routine to compute a dot product
sub vector_dot_product {
my($vec1, $vec2) = @_;
my($result, $i);
$result = 0;
for $i (0..2) {
$result += $$vec1[$i] * $$vec2[$i];
}
return $result;
}
# Routine to compute a vector cross product
sub vector_cross_product {
my($vec1, $vec2) = @_;
my(@result);
$#result = 2;
$result[0] = $$vec1[1] * $$vec2[2] - $$vec1[2] * $$vec2[1];
$result[1] = $$vec1[2] * $$vec2[0] - $$vec1[0] * $$vec2[2];
$result[2] = $$vec1[0] * $$vec2[1] - $$vec1[1] * $$vec2[0];
return @result;
}
sub trim {
local($input) = @_;
$input =~ s/^\s+//;
$input =~ s/\s+$//;
return $input;
}
sub showcroft {
return @croft;
}
# Routine to split a DICOM list into a perl list
sub split_dicom_list {
my($dlist) = @_;
my(@values) = split(/\\/, $dlist);
foreach $value (@values) {
$value += 0;
}
return (scalar(@values) > 1) ? @values : $values[0];
}
sub SetupArgTables
{
my (@args) =
(
["Slice info options", "section"],
["-image", "call", ['0020','0013'], \&InfoOption,
"Print image number"],
["-exam", "call", ['0020','0010'], \&InfoOption,
"Print exam number"],
["-studyuid", "call", ['0020','000D'], \&InfoOption,
"Print study uid"],
["-series", "call", ['0020','0011'], \&InfoOption,
"Print series number"],
["-echo", "call", ['0018','0086'], \&InfoOption,
"Print echo number"],
["-width", "call", ['0028','0011'], \&InfoOption,
"Print width"],
["-height", "call", ['0028','0010'], \&InfoOption,
"Print height"],
["-slicepos", "call", [-1,'slicepos'], \&InfoOption,
"Print slice position"],
["-slice_thickness", "call", ['0018','0050'], \&InfoOption,
"Print slice thickness"],
["-tr", "call", ['0018','0080'], \&InfoOption,
"Print repetition time"],
["-te", "call", ['0018','0081'], \&InfoOption,
"Print echo time"],
["-ti", "call", ['0018','0082'], \&InfoOption,
"Print inversion time"],
["-date", "call", ['0008','0022'], \&InfoOption,
"Print acquisition date"],
["-time", "call", ['0008','0032'], \&InfoOption,
"Print acquisition time"],
["-file", "call", [-1,'filename'], \&InfoOption,
"Print filename"],
["Patient info options", "section"],
["-pname", "call", ['0010','0010'], \&InfoOption,
"Print patient name"],
["-pdob", "call", ['0010','0030'], \&InfoOption,
"Print patient date of birth"],
["-pid", "call", ['0010','0020'], \&InfoOption,
"Print patient id"],
["Other info options", "section"],
["-institution", "call", ['0008','0080'], \&InfoOption,
"Print institution name"],
["-series_description", "call", ['0008','103E'], \&InfoOption,
"Print series description"],
["-sequence_name", "call", ['0018','0024'], \&InfoOption,
"Print sequence name"],
["-scanner", "call", ['0008','1090'], \&InfoOption,
"Print scanner"],
["-attvalue", "call", undef, \&TwoArgInfoOption,
"Print the value(s) of the specified attribute"],
["-stdin", "boolean", 1, \$UseSTDIN,
"Use STDIN for the list of dicom files"],
["Formatting options", "section"],
["-labels", "boolean", 1, \$PrintLabels,
"Print one line of labels before the rest of the output"],
["-error_string", "string", 1, \$ErrorString,
"String to use for reporting empty fields"],
);
return @args;
}
sub InfoOption {
my ($option, $rest, @addr) = @_;
my $group = shift @addr;
my $element = shift @addr;
push(@Variables, [$group, $element]);
1;
}
sub TwoArgInfoOption {
my ($option, $rest) = @_;
my $group = shift @$rest;
my $element = shift @$rest;
if (!defined($element) || !defined($group)) {
&cleanup_and_die("$option requires two arguments", 1);
}
push(@Variables, [$group, $element]);
1;
}
sub CreateInfoText
{
$Usage = <<USAGE;
Usage: get_dicom_info [options] <dicomfile> [<dicomfile> ...]
get_dicom_info -help
USAGE
$Help = <<HELP;
get_dicom_info reads info out of the DICOM file headers, based on some code chunks written by Peter Neelin for dicom_to_minc.
Author: Jonathan Harlap
Date: 2003/01/16
Last modified: 2003/01/31
HELP
&Getopt::Tabular::SetHelp ($Help, $Usage);
}