How to convert DICOM image to JPEG/PNG with out loosing it's metadata?
32 views (last 30 days)
Show older comments
I need to convert my dicom image to jpg or png format without loosing it's meta data,I have already refered this link,but the solution given on that link is not working at all,cannot save the image as jpg. Please suggest some links or code to this conversion.
1 Comment
ismail tepedag
on 23 May 2021
try below trick
b=mat2gray(output_image);
d=im2uint8(b);
new image is d without loosing much details...
Answers (2)
Gopichandh Danala
on 16 Sep 2016
% code
% 793 is a dicom image
DICOM_image = dicomread('793');
info = dicominfo('793');
window_center = info.WindowCenter;
window_width = info.WindowWidth;
rescale_slope = info.RescaleSlope;
rescale_intercept = info.RescaleIntercept;
low = abs(info.RescaleIntercept)-abs(info.WindowCenter)+30;
high = low+info.WindowWidth;
figure, imshow(DICOM_image, [low high]);
[nrows, ncols] = size(DICOM_image);
output_image = zeros(size(DICOM_image));
new_image = zeros(nrows, ncols);
max_pixel_intensity = 255;
for i = 1:nrows
for j = 1:ncols
new_image(i, j) = DICOM_image(i, j) * rescale_slope + rescale_intercept;
if (new_image(i, j) < (window_center - window_width / 2))
output_image(i, j) = 0;
else if (new_image(i, j) > (window_center + window_width / 2))
output_image(i, j) = max_pixel_intensity;
else
output_image (i, j) = (max_pixel_intensity / window_width) * (new_image(i, j) + window_width / 2 - window_center);
end
end
end
end
output_image = uint8(output_image);
figure, imshow(output_image);
imwrite(output_image,'converted.jpg');
Hope it helps, Let me Know
7 Comments
ismail tepedag
on 23 May 2021
- What is 30 ? it works but why ?
low = abs(info.RescaleIntercept)-abs(info.WindowCenter)+30;
2. +window_width/2 shall not be existing in below code.
output_image (i, j) = (max_pixel_intensity / window_width) * (new_image(i, j) + window_width / 2 - window_center);
Have nice day !
Walter Roberson
on 16 Sep 2016
Edited: Walter Roberson
on 17 Sep 2016
It is not possible to convert DICOM to JPG or PNG without losing metadata.
For JPEG files you could convert convert the metadata to some kind of string encoding, and then include it in the file as an EXIF "Description" field, provided that the encoding of the metadata does not occupy more than 64 Kb; see https://en.wikipedia.org/wiki/Exif
PNG does not have EXIF. You can, though, encode arbitrary metadata as name=value strings; see http://stackoverflow.com/questions/9542359/does-png-contain-exif-data-like-jpg -- or at least you could in theory, as imwrite() does not support that. You could, though, instead use the same technique as described above, encoding the metadata as strings (somehow) and then use imwrite() to PNG with the 'Description' name/value pair.
Working out how to convert all of the metadata to strings that could later be parsed would be up to you.
See Also
Categories
Find more on DICOM Format in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!