Newsletters - MATLAB Digest
Accessing data in DICOM files
(Part 2 of 3)
Accessing DICOM Metadata
In the previous example, we made several assumptions as we preallocated the storage array. All of these assumptions were based on metadata that exists in the DICOM file. Furthermore, the values of the image pixels are limited to a narrow band in the total possible dynamic range of the image, so we had to pass an empty array as a special argument to montage to rescale the data. Let's use the metadata attributes in the DICOM files to (1) intelligently preallocate our array and (2) rescale the data values to fill the 16-bit dynamic range.
Because all images in a series must have the same dimensions and bit-depth, we only need to get the metadata from one image in the series to preallocate the array. The Image Processing Toolbox function dicominfo returns the metadata from a DICOM file.
![]() |
As you can see, there are many metadata values, or attributes. The values for "Rows," "Columns," and "BitsStored" tell us exactly what we need to know, and we can rewrite the code that reads the image stack.
nRows = info.Rows;
nCols = info.Columns;
nPlanes = info.SamplesPerPixel;
nFrames = 20; % The number of files in the directory
X = repmat(int16(0), [nRows, nCols, nPlanes, nFrames]); for p = 1:nFrames
fname = sprintf('brain_%03d.dcm', p);
X(:,:,:,p) = dicomread(fname);
end
We still have to take for granted that the image contains signed data&msdash;a "PixelRepresentation" value of "1" indicates that type of data-but the rest of the information that we need is easily obtained. We can use this metadata along with the imlincomb function in the Image Processing Toolbox to rescale the image data to fill the entire 16-bit dynamic range. The linear combination to rescale the grayscale values is "y = (x - b) * m", where b is the minimum x value and m is a constant ratio derived from the input and output ranges.
% Keep track of the minimum and maximum pixel values.
minPixels = repmat(0, [1, nFrames]);
maxPixels = repmat(0, [1, nFrames]);
for p = 1:nFrames
fname = sprintf('brain_%03d.dcm', p);
info = dicominfo(fname);
minPixels(p) = info.SmallestImagePixelValue;
maxPixels(p) = info.LargestImagePixelValue;
end
% Rescale image to start at 0.
b = min(minPixels);
m = 2^16/(max(maxPixels) - b);
Y = imlincomb(m, X, -(m * b), 'uint16');
Store

