2
votes

I have a series of DICOM Images which I want to anonymize, I found few Matlab codes and some programs which do the job, but none of them export a .txt file of removed personal information. I was wondering if there is a function which can also save removed personal information of a DICOM images in .txt format for features uses. Also, I am trying to create a table which shows the corresponding new images ID to their real name.(subjects real name = personal-information-removed image ID)

Any thoughts?

Thanks for considering my request!

2

2 Answers

1
votes

I'm guessing you only want to output to your text file the fields that are changed by anonymization (either modified, removed, or added). First, you may want to modify some dicomanon options to reduce the number of changes, in particular passing the arguments 'WritePrivate', true to ensure private extensions are kept.

First, you can perform the anonymization, saving structures of pre- and post-anonymization metadata using dicominfo:

preAnonData = dicominfo('input_file.dcm');
dicomanon('input_file.dcm', 'output_file.dcm', 'WritePrivate', true);
postAnonData = dicominfo('output_file.dcm');

Then you can use fieldnames and setdiff to find fields that are removed or added by anonymization, and add them to the post-anonymization or pre-anonymization data, respectively, with a nan value as a place holder:

preFields = fieldnames(preAnonData);
postFields = fieldnames(postAnonData);

removedFields = setdiff(preFields, postFields);
for iField = 1:numel(removedFields)
  postAnonData.(removedFields{iField}) = nan;
end

addedFields = setdiff(postFields, preFields);
for iField = 1:numel(addedFields)
  preAnonData.(addedFields{iField}) = nan;
end

It will also be helpful to use orderfields so that both data structures have the same ordering for their field names:

postAnonData = orderfields(postAnonData, preAnonData);

Finally, now that each structure has the same fields in the same order we can use struct2cell to convert their field data to a cell array and use cellfun and isequal to find any fields that have been modified by the anonymization:

allFields = fieldnames(preAnonData);
preAnonCell = struct2cell(preAnonData);
postAnonCell = struct2cell(postAnonData);
index = ~cellfun(@isequal, preAnonCell, postAnonCell);
modFields = allFields(index);

Now you can create a table of the changes like so:

T = table(modFields, preAnonCell(index), postAnonCell(index), ...
          'VariableNames', {'Field', 'PreAnon', 'PostAnon'});

And you could use writetable to easily output the table data to a text file:

writetable(T, 'anonymized_data.txt');

Note, however, that if any of the fields in the table contain vectors or structures of data, the formatting of your output file may look a little funky (i.e. lots of columns, most of them empty, except for those few fields).

0
votes

One way to do this is to store the tags before and after anonymisation and use these to write your text file. In Matlab, dicominfo() will read the tags into a structure:

% Get tags before anonymization
tags_before = dicominfo(file_in);

% Anoymize
dicomanon(file_in, file_out); % Need to set tags values where required

% Get tags after anonymization
tags_after = dicominfo(file_out);

% Do something with the two structures
disp(['Patient ID:', tags_before.PatientID ' -> ' tags_after.PatientID]);
disp(['Date of Birth:', tags_before.PatientBirthDate ' -> ' tags_after.PatientBirthDate]);
disp(['Family Name:', tags_before.PatientName.FamilyName ' -> ' tags_after.PatientName.FamilyName]);

You can then write out the before/after fields into a text file. You'd need to modify dicomanon() to choose your own values for the removed fields, since by default they are set to empty.