How to use imwrite from dicom to png

6 views (last 30 days)
Hi all, I have 4D images. I want to exctract it to png every slice. Below my coding. But Got error. Anyone can help me?
I = dicomread('SPECT BENTUK LIVER.dcm');
I = squeeze(I);
I = uint8(255 * mat2gray( I));
imwrite(I,'SPECTbnetukLiver.png');
ERROR
Error using writepng (line 23)
PNG image data must be either MxN or MxNx3.
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});

Accepted Answer

Simon Chan
Simon Chan on 23 Aug 2021
It may contains more than one image and hence you need to save it one by one:
Following code for your reference:
[I, cmap] = dicomread('SPECT BENTUK LIVER.dcm');
J = squeeze(I);
[~,~,Nz] = size(J); % Check number of images
for k = 1:Nz
imwrite(J(:,:,k),cmap,sprintf('SPECTbnetukLiver_%02d.png',k)) % Save image one by one
end
  17 Comments
mohd akmal masud
mohd akmal masud on 1 Sep 2021
in your code,
for k = 1 : size(I8,3)
end
what is 3?
Walter Roberson
Walter Roberson on 2 Sep 2021
szdim = size(A,dim) returns the length of dimension dim when dim is a positive integer scalar. Starting in R2019b, you can also specify dim as a vector of positive integers to query multiple dimension lengths at a time. For example, size(A,[2 3]) returns the lengths of the second and third dimensions of A in the 1-by-2 row vector szdim.
In other words, size(I8,3) is the number of "pages" (third dimension) that I8 has. You said "my image have 128x128x90 uint16" so the third dimension of that woud be 90 .

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!