0001 function d = read_dicom(filename)
0002
0003
0004
0005
0006
0007
0008
0009 fid = -1;
0010 if vb_matlab_version('>=', '7.2')
0011 fid = fopen(filename, 'r', 'ieee-le', 'US-ASCII');
0012 else
0013 fid = fopen(filename, 'r', 'ieee-le');
0014 end
0015
0016
0017 d = [];
0018
0019 ix = create_index_dicom(fid);
0020
0021
0022 t = fread_dicom(fid, ix, '0008', '0020', 1, 'char');
0023 d = setfield(d, 'study_date', char(t'));
0024
0025
0026 t = fread_dicom(fid, ix, '0008', '0030', 1, 'char');
0027 d = setfield(d, 'study_time', char(t'));
0028
0029
0030 t = fread_dicom(fid, ix, '0010', '0010', 1, 'char');
0031 d = setfield(d, 'patients_name', char(t'));
0032
0033
0034 t = fread_dicom(fid, ix, '0010', '0020', 1, 'char');
0035 d = setfield(d, 'patient_id', char(t'));
0036
0037
0038 t = fread_dicom(fid, ix, '0010', '4000', 1, 'char');
0039 d = setfield(d, 'patient_comments', char(t'));
0040
0041
0042 t = fread_dicom(fid, ix, '0018', '0020', 1, 'char');
0043 d = setfield(d, 'scanning_sequence', char(t'));
0044
0045
0046 t = fread_dicom(fid, ix, '0018', '0050', 1, 'char');
0047 d = setfield(d, 'slice_thickness', sscanf(char(t), '%f'));
0048
0049
0050 t = fread_dicom(fid, ix, '0018', '0080', 1, 'char');
0051 d = setfield(d, 'repetition_time', sscanf(char(t), '%f'));
0052
0053
0054 t = fread_dicom(fid, ix, '0018', '0081', 1, 'char');
0055 d = setfield(d, 'echo_time', sscanf(char(t), '%f'));
0056
0057
0058 t = fread_dicom(fid, ix, '0018', '0088', 1, 'char');
0059 x=t;
0060 d = setfield(d, 'spacing_between_slices', sscanf(char(t), '%f'));
0061
0062
0063 t = fread_dicom(fid, ix, '0018', '1250', 1, 'char');
0064 d = setfield(d, 'recieving_coil', char(t'));
0065
0066
0067 t = fread_dicom(fid, ix, '0020', '0010', 1, 'char');
0068 d = setfield(d, 'study_id', char(t'));
0069
0070
0071 t = fread_dicom(fid, ix, '0020', '0011', 1, 'char');
0072 d = setfield(d, 'series_number', sscanf(char(t), '%d'));
0073
0074
0075 t = fread_dicom(fid, ix, '0020', '0013', 1, 'char');
0076 d = setfield(d, 'image_number', sscanf(char(t), '%d'));
0077
0078
0079 t = fread_dicom(fid, ix, '0020', '0032', 1, 'char');
0080 d = setfield(d, 'image_position', sscanf(char(t), '%f\\%f\\%f'));
0081
0082
0083 t = fread_dicom(fid, ix, '0020', '0037', 1, 'char');
0084 d = setfield(d, 'image_orientation', sscanf(char(t),...
0085 '%f\\%f\\%f\\%f\\%f\\%f'));
0086
0087
0088 t = fread_dicom(fid, ix, '0028', '0002', 2, 'uint16');
0089 d = setfield(d, 'sample_per_pixel', t);
0090
0091
0092 t = fread_dicom(fid, ix, '0028', '0004', 1, 'char');
0093 d = setfield(d, 'photometric_interpretation', char(t'));
0094
0095
0096 t = fread_dicom(fid, ix, '0028', '0010', 2, 'uint16');
0097 d = setfield(d, 'rows', uint16(t));
0098
0099
0100 t = fread_dicom(fid, ix, '0028', '0011', 2, 'uint16');
0101 d = setfield(d, 'columns', uint16(t));
0102
0103
0104 t = fread_dicom(fid, ix, '0028', '0030', 1, 'char');
0105 d = setfield(d, 'pixel_spacing', sscanf(char(t), '%f\\%f'));
0106
0107
0108 t = fread_dicom(fid, ix, '0028', '0100', 2, 'uint16');
0109 d = setfield(d, 'bits_allocated', t);
0110
0111
0112 t = fread_dicom(fid, ix, '0028', '0101', 2, 'uint16');
0113 d = setfield(d, 'bits_stored', t);
0114
0115
0116 t = fread_dicom(fid, ix, '0028', '0102', 2, 'uint16');
0117 d = setfield(d, 'high_bit', t);
0118
0119
0120 t = fread_dicom(fid, ix, '0028', '0103', 2, 'uint16');
0121 d = setfield(d, 'pixel_representation', t);
0122
0123
0124 t = fread_dicom(fid, ix, '0028', '1050', 1, 'char');
0125 d = setfield(d, 'window_center', sscanf(char(t), '%f'));
0126
0127
0128 t = fread_dicom(fid, ix, '0028', '1051', 1, 'char');
0129 d = setfield(d, 'window_width', sscanf(char(t), '%f'));
0130
0131
0132 t = fread_dicom(fid, ix, '0028', '1052', 1, 'char');
0133 d = setfield(d, 'rescale_intercept', sscanf(char(t), '%f'));
0134
0135
0136 t = fread_dicom(fid, ix, '0028', '1053', 1, 'char');
0137 d = setfield(d, 'rescale_slope', sscanf(char(t), '%f'));
0138
0139
0140 t = fread_dicom(fid, ix, '7FE0', '0010', 2, 'uint16');
0141 d = setfield(d, 'pixel_data', reshape(t, d.rows, d.columns)');
0142
0143
0144 fclose(fid);
0145
0146
0147
0148 function dt = fread_dicom(fid, ix, grp, elm, sz, tp)
0149 [pos, len] = index_dicom(ix, grp, elm);
0150 fseek(fid, pos, 'bof');
0151 dt = fread(fid, len/sz, tp);