Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Private Dicom data in matlab without modifying the dictionary

Tags:

matlab

dicom

I am reading a dicom file in matlab and modifying some data of it and trying to save it into another file, but while doing so, the private dicom data are either not written at all (when 'WritePrivate' is set to 0) or it's written as a UINT8 array which become incomprehensible and useless. I even tried to copy the data that I get in from the original dicom file to a new structure and write to a new dicom file but even though the private data remains fine in new structure it doesn't remain so in the new dicom file. Is there any way to keep this private data intact while copying in to a new dicom file without changing the matlab dicom dictionary?

I have provided the following code to show what I'm trying to do.

X=dicomread('Bad011_4CH_01.dcm');
metadata = dicominfo('Bad011_4CH_01.dcm');
metadata.PatientName.FamilyName='LastName';
metadata.PatientName.GivenName='FirstName';
birthday=metadata.PatientBirthDate;
year=birthday(1,1:4);
newyear=strcat(year,'0101');
metadata.PatientBirthDate=newyear;
names=fieldnames(metadata);

h=metadata;

dicomwrite(X,'example.dcm',h,'CreateMode','copy');

newh=dicominfo('example.dcm');

Here the data in newh contains none of the private data. If I change the code to the following

dicomwrite(X,'example.dcm',h,'CreateMode','copy','WritePrivate',1);

In this case the private data gets totally changed to some UIN8 array and useless. The ideal solution for my task would be to enable keeping the private data in the newly created dicom file without changing the matlab dicom dictionary.

like image 497
the_naive Avatar asked Nov 03 '22 09:11

the_naive


2 Answers

Have you tried something like:

dicomwrite(uint16(image), fileName, 'ObjectType', 'MR Image Storage', ...
    'WritePrivate', true, header);

where "header" is a struct composed of name-value pairs using the same format as header data that you would get from MATLAB's dicominfo function? My general approach to image creation in MATLAB is to avoid using CreateMode 'copy' and instead build my own DICOM header by explicitly copying the attributes that it makes sense to copy and generating my own values for attributes that should have new values.

To write private tags, you would do something like:

header.Private_0045_10xx_Creator = 'MY_PRIVATE_BLOCK';
header.Private_0045_1001 = int32(65535);

If you then write this out using dicomwrite and read it back in using hdr = dicominfo('mynewimg');, you can see that it really did write the value as a 32-bit integer even though, unfortunately, if is always going to read the data in as a vector of uint8 values.

>> hdr.Private_0045_1001

ans =

  255
  255
    0
    0

As long as you know what type to expect, you should be able to typecast the data back to the desired type after you've read the header. For example:

>> typecast(hdr.Private_0045_1001, 'int32')

ans =

   65535
like image 63
Matt Avatar answered Nov 08 '22 03:11

Matt


I know I'm about 8 years late, but have you tried

dicomwrite(..., 'VR', 'explicit')

?

It solves the "reading as uint8" problem for me.

Edit:

Actually, it looks like you need to specify a dicom dictionary with the VR of that tag. If you combine this with 'VR', 'explicit', then the program reading the dicom won't need to dictionary file.

like image 29
stilley2 Avatar answered Nov 08 '22 05:11

stilley2