Reputation: 48916
How can I convert a DICOM image into a grayscale image in matlab?
Thanks.
Upvotes: 0
Views: 5232
Reputation: 2502
I'm not sure what specific DICOM image you are talking about that is not grayscale already. The following piece of code demonstrates how you read and display a DICOM image:
im = dicomread('sample.dcm'); % im will usually be of type uint16, already in grayscale
imshow(im, []);
If you want a uint8 grayscale image, use:
im2 = im2uint8(im);
However to keep as much of the precision as possible, it's best to do:
im2 = im2double(im);
To stretch the limits temporarily only when displaying the image, use:
imshow(im2, []);
To stretch the limits permanently (for visualization only not for analysis), use:
% im3 elements will be between 0 and 255 (uint8) or 0 and 1 (double)
im3 = imadjust(im2, stretchlim(im2, 0), []);
imshow(im3);
To write the grayscale image as jpg or png, use:
imwrite(im3, 'sample.png');
UPDATE
If your version of Matlab does not have im2uint8
or im2double
, assuming that your DICOM image is always uin16
a quick workaround to convert the DICOM image to a more manageable format would be:
% convert uint16 values to double values
% im = double(im);
% keep the current relative intensity levels for analysis
% involving real values of intensity
im2 = im/2^16
% if displaying, create a new image with limits stretched to maximum
% for visualization purposes. do not use this image for
% calculations involving intensity value.
im3 = im/(max(im(:));
Upvotes: 3