imwrite() with PGM option actually write in PBM

2 views (last 30 days)
Hi all, I am trying to convert DICOM images in PGM images for a software I am using. dicomread() reads them correctly, but when it comes to use imwrite(), it provides PBM images (I check it using Mac infos, and using the fact that on the program I am writing gives me an error when I use those images, a wrong-extension-error). Note that if I try other formats like png it works.
The code is the following:
clear all; close all; clc;
MM = 1686; % maximum value for normalization
mm = -2048; % minimum value
for ii=1:331
str = strcat('/my/path/IM-0001-',num,'-0001.dcm'); % DICOM image name
info = dicominfo(str,'UseDictionaryVR',true); % DICOM info
Y = double(dicomread(info)); % read
Y = (Y-mm)./(MM-mm); % normalize
titleSlice = strcat('/my/path/slice',num2str(ii),'.pgm'); % output name
imwrite(Y,titleSlice,'pgm'); % imwrite() which provides PBM and not PGM
end

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 24 Nov 2018
Edited: KALYAN ACHARJYA on 24 Nov 2018
Follow this way, its works to write the dicom in pgm format
path_directory='dcm_sample_images'; % 'Folder name'
original_files=dir([path_directory '/*.dcm']);
for j=1:3
filename=[path_directory '/' original_files(j).name];
I1=double(dicomread(filename));
%do your findings, say I2 is results
path_folder='D:\JNU jaipur\PhD\Matlab Work\dicom_write_test';
baseFileName=sprintf('%d.pgm',j);
fullFileName=fullfile(path_folder,baseFileName); % No need to worry about slashes now!
imwrite(I2,fullFileName);
end
222.png
111.png

Categories

Find more on Convert Image Type 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!