How to copy the Dicom private tags in another Dicom file using the dicomwrite function accurately?

Hi, I need to copy the Dicom info from one file to another. The Dicom file I have consists of many private tags and I want to copy them to the another file that I am writing using the MATLAB dicomwrite function.
info = dicominfo('ABCD.1.img'); % it has may private tags
fileID1 = fopen('data.s', 'r');
data1 = fread(fileID1); % I am writing this as a dicom file
fclose(fileID1);
dicomwrite(data1, 'new_image.img', info, 'CreateMode', 'Copy','WritePrivate', true);
metadata = dicominfo('new_image.img');
isequal(info, metadata) % .....> returns logical 0.
When I do this, all the private tags becomes uint8 and [0;0;0;0]...... format (some nonsense values). How can I copy the info accurately?

7 Comments

Since those private tags are not in the dicom dictionary Matlab is using, it will assume your data is uint8 to preserve everything, as that will read and write your data as bytes.
Did you attempt to unwind the structs to find which exact fields are different? I believe there is a submission on the FEX that can do that for you.
Using the following code I was able to pull down all the private dicom tags:
% Pull all private Dicom tags
fn=cell(1,4);
[fn{:}]=textread(dicomdict('get'),repmat('%s',1,4),'delimiter','\t','commentstyle','shell');
info=dicominfo('ABCD.1.img'); % This is where private tags are stored!
not_defined=setdiff(lower(fieldnames(info)),lower(fn{3}));
not_defined(cellfun(@(c) isempty(strfind(c,'private_')),not_defined))=[];
I am now wondering whether I need to write a dicom dictionary and adding these private tags and write a new dicom file using dicomwrite function, any thoughts on this? Is the FEX - file exchange? Do you have a link of that? Sorry for multiple questions.
Additional: I think I can modify the dicom dict but I am not sure about the VR and VM filed, any suggestions on that for private tags?
Thank you, Rik, in advance.
Although you could create a dicom dictionary for Matlab to use, that might not be necessary.
I don't have a link to the submission I mentioned, so you will have to do the same thing I would need to do: do a Google search. I believe the submission was able to merge structs as well, so you could also try that in your search terms. (and yes, I meant the file exchange)
As a last note: if you want a dictionary to extend with your own entries, here is the 2011 dictionary I adapted to be compatible with Matlab. I'm working on a dicom reader, but it is fairly low priority, resulting in the dictionary being ready 3 years ago, and the reader not being ready to publish yet.
If you're looking for the meaning of VR and VM: they stand for value representation and value multiplicity. VR is a shorthand for the data type and VM describes the number of bytes a tag can span (if I recall correctly). You should look up the NEMA document I mention in the comments of the dictionary file I linked. I know dicom can be very confusing and information about it can be unstructured and scattered. That's what you get if you start using something from the mid-80s.
In addition to dicom being old, not every dicom file will be compliant. One of the main reasons my dicom reader has been delayed is because it has trouble dealing with improper files. Ironically that is what makes it so much faster than the built-in Matlab tools.
FInally, I copied all dicom tags (including private tags) to my new dicom file by appending the dicom dictionary. Thank you again for the comprehensive comment and it was very helpful.
I am facing the same problem. Can you elaborate on your statement about appending the dicom dictionary? How did you manage to write the private tags such that they are not in uint8? Would you mind sharing the script for this?
Cheers
The private tags are private, so there isn't a general definition. The definition might be standard for your specific field/application. Once you know what a specific tag means, you can define the VR and VM. The VR will ensure the data is read in the correct data type.
Doing custom work with DICOM is not trivial. Know what you're getting yourself into. There will be a lot of googling involved and a lot of conflicting information.

Sign in to comment.

Answers (0)

Categories

Asked:

on 23 Feb 2021

Commented:

Rik
on 4 Jun 2021

Community Treasure Hunt

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

Start Hunting!