DICOM image data is typically stored as 16-bit unsigned integers, so you'll want to make sure that your image is stored within a uint16
matrix prior to saving so MATLAB knows to save it as such. Also, for some image formats, MATLAB requires that we explicitly state the bit depth.
% Save as a 16-bit Baseline JPEG with the highest quality
imwrite(uint16(data), 'image.jpg', 'Quality', 100, 'BitDepth', 16);
% Save as a 16-bit Lossless JPEG
imwrite(uint16(data), 'image.jpg', 'Mode', 'lossless', 'BitDepth', 16)
% Save as a 16-bit JPEG 2000 Image
imwrite(uint16(data), 'image.jp2', 'Mode', 'lossless')
If you don't need a JPEG for any particular reason, I would recommend a PNG (lossless).
% Save as 16-bit PNG
imwrite(uint16(data), 'image.png')
See the full list of available 16-bit formats here.
For visualization in MATLAB, you can specify the second input to imshow
(or use imagesc
) to automatically scale the displayed gray scale values to the data within the image
imshow(data, []) % or imagesc(data); axis image;