0

How can I convert a DICOM image into a grayscale image in matlab?

Thanks.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Simplicity
  • 47,404
  • 98
  • 256
  • 385

1 Answers1

3

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(:));
Bee
  • 2,472
  • 20
  • 26
  • Thanks for your reply. It seems that `im2uint8` doesn't work, at least on `Matlab 2013 student version` which I have tried this function on, and seems that `unit8` should be used – Simplicity Jun 02 '13 at 18:28
  • I have updated my original post to include alternative approaches. – Bee Jun 03 '13 at 00:11