Code covered by the BSD License  

Highlights from
Create a quadratic Matrix of DICOM images

Create a quadratic Matrix of DICOM images



25 Feb 2013 (Updated )

Load all DICOM images, change the Matrix size and save them again, keeping the header (Metadata).

% Create a quadratic Matrix of all DICOM images in the specified folder
% The files are neither cropped, nor are the images stretched, only the
% Matrixsize is changed (all pixels without image data are then filled up with
% zeros)

% The DICOM header remains unchanged, except for the Matrix size
% Just run the Script
% Mind the following points:

% 1. Foldername and -location containing the DICOM files
% 2. Fileformat of the images to load
% 3. Output File Format
% 4. Output File Folder

% Provided by Thomas Lindner

clear all
close all


% Path needs to lie in the same folder as the m-File
% "Images" is the name of the folder containing all DICOM images
addpath Images

% Read all dicom files in the folder
% mind the data ending, default is '*.IMA', change if needed
dicomlist = dir(fullfile(pwd,'Images','*.IMA'));
for cnt = 1 : numel(dicomlist)
    I{cnt} = dicomread(fullfile(pwd,'Images',dicomlist(cnt).name)); 

% read the size of the !!first!! image
[nbrow,nbcol] = size(I{1});

% Do the size change
for k = 1:cnt
    %select the specific image
    var = dicomlist(k).name;
    %read out the metadata (DICOM header)
    metadata = dicominfo(var);
    % look up, which size is bigger (rows or columns)
if nbrow > nbcol
    x = nbrow - nbcol;
    C = padarray(I{k},[0 x/2], 0);
elseif nbrow < nbcol
    x = nbcol - nbrow;
    C = padarray(I{k},[x/2 0], 0);
% specify output name mind the data ending, default is '*.IMA', change if needed ('#%d' creates an ascending number)
outputFileName = sprintf('Changed_Image #%d.IMA',k);
% write the new DICOM file(s) into a specified folder, Default is: C:\
dicomwrite(C, ['C:\' outputFileName], metadata,'CreateMode','copy');

Contact us