dicomwrite to save dicom image with window level setting

34 views (last 30 days)
Any one can help me?
I want to save my dicom images as dicom in right contrast.
I wrote like this
R = dicomread('1.dcm');
The image like below
then I wrote
T = imshow(R, [0 1844]);
then I want to save as new image, i wrote
dicomwrite(T, 'S.dcm');
But got ERROR
Error using dicom_prep_ImagePixel>getPixelStorage (line 204)
Invalid datatype for image pixels.
Error in dicom_prep_ImagePixel (line 13)
[ba, bs, hb, pr] = getPixelStorage(X, txfr, useExistingBitDepths, metadata,
Error in dicom_prep_metadata (line 51)
metadata = dicom_prep_ImagePixel(metadata, X, map, txfr,
useMetadataBitDepths, dictionary);
Error in dicom_create_IOD (line 26)
metadata = dicom_prep_metadata(IOD_UID, metadata, X, map, options.txfr,
options.usemetadatabitdepths, dictionary);
Error in dicomwrite>write_message (line 275)
[attrs, status] = dicom_create_IOD(SOP_UID, X, map, ...
Error in dicomwrite (line 211)
[status, options] = write_message(X, filename, map, metadata, options);

Accepted Answer

Simon Chan
Simon Chan on 18 Aug 2021
The matrix R coming from dicomread is the rawdata and you are not advised to change it.
Most probably you are only able to change the Default Window Level and Window Width in the DICOM headers. This default Window Level and Window Width are only used in application software which is able to retrieve this DICOM header information and hence display the image in an expected contrast level.
Hence, the displayed image will always be compelely black if you use image(R) instead of image(R,[]).
Following code provides a reference for you and you can observe the difference by usng imtool
clear; clc;
X = dicomread('old.dcm');
metadata = dicominfo('old.dcm');
WC = metadata.WindowCenter; % original [40; 40]
WW = metadata.WindowWidth; % original [80; 80]
imtool(X,'DisplayRange',[WC(1)-WW(1), WC(1)+WW(1)]); % Left figure below
metadata.WindowCenter = [1050; 1050];
metadata.WindowWidth = [400; 400];
dicomwrite(X, 'New.dcm', metadata, 'CreateMode', 'copy');
Y = dicomread('New.dcm');
newmetadata = dicominfo('New.dcm');
newWC = newmetadata.WindowCenter;
newWW = newmetadata.WindowWidth;
imtool(Y,'DisplayRange',[newWC(1)-newWW(1), newWC(1)+newWW(1)]); % Right figure below
Use the original Window Level and Window Width (Left), and revised WL & WW (right):
Walter Roberson
Walter Roberson on 1 Sep 2021
metadata = dicominfo('I10');
WC = metadata.WindowCenter; % original [40; 40]
WW = metadata.WindowWidth; % original [80; 80] % Left figure below
metadata.WindowCenter = [1200; 1200];
metadata.WindowWidth = [400; 400];
I = dicomread(metadata);
imshow3D(permute(I,[1 2 4 3])); %look for the WL and best picture
for k = 1:size(I,4)
dicomwrite(I(:,:,:,k),sprintf('%d.dcm',k), metadata, 'CreateMode', 'copy');

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 18 Aug 2021
T = imshow(R, [0 1844]);
the result of imshow() is a handle to a graphics image() object. You cannot write a graphics handle as an image.
You should use rescale(); older MATLAB would have to use the obscurely-named mat2gray()

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by