from Anonymizing a batch of Dicom Files together by Jaydev
To perform batch processing for anonymizing dicom files

dcm_batch_anony.m
% October 15,2009 - Written by Jaydev Dave

% This code performs batch processing on dicom files to
% anonymize the batch for removing patient identifiers. 

% Disclaimer: Note the removal of patient identifiers is based on 'dicomanon' function provided by Matlab
% and thus the user needs to independently verify if the anonymizing is as per the rules set by the required governing instituition

% User is required to place each series in different folders
% The program will ask the user to point to the first DICOM file in a
% particular series

% Generally for the medical images the Dicom filename is a long list of
% characters with many periods 
% eg: x.x.xxxxx.xxxxxxxxxx.xxx.000.dcm

% This program will work only in the case where the series files are in
% increasing order from "000" to "999"... i.e the series has filename that
% is ordered such that the numbers change only between the second-last and
% last period

% Example of filelist:
% Dicom File 1: x.x.xxxxx.xxxxxxxxxx.xxx.522.dcm
% Dicom File 2: x.x.xxxxx.xxxxxxxxxx.xxx.523.dcm
% Dicom File 3: x.x.xxxxx.xxxxxxxxxx.xxx.524.dcm
% ...
% Dicom File n: x.x.xxxxx.xxxxxxxxxx.xxx.793.dcm

% The program will create the anonymized files with the name as
% "anon_filelist(i) where i represents the image number. The program uses
% the dicomanon function provided in MATLAB.

clear all
clc
[filename,pathname]=uigetfile('*.*','Select the first dicom file...');
cd(pathname);

numfiles=0;
tailtxt=filename(length(filename)-3:length(filename));
for i=1:1000
    if exist (filename,'file')
        numfiles=numfiles+1;
        filelist{numfiles}=filename;
    end
    filechar=double(filename);
    loc=find(filechar==46);
    changingnum=(filename(loc(end-1)+1:loc(end)-1));
    temp=str2num(changingnum);
    newnumber=temp+1;
    filename=[filename(1:loc(end-1)) num2str(newnumber)  tailtxt];
end
s=size(filelist);

for i=1:max(s)
    anon_filelist{i}=['anon_' num2str(i) tailtxt];
end

for i=1:max(s)
    dicomanon(filelist{i},anon_filelist{i});
end




    

Contact us at files@mathworks.com