Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
multiframe dicomwrite

Subject: multiframe dicomwrite

From: sara sacchi

Date: 14 Dec, 2008 23:19:02

Message: 1 of 18

Hi!
I'm working with multiframe dicom files.
When reading them with dicomread function, MATLAB create a 4D-array (size NxMxDxF) where image data are stored.
When I try to create a new multiframe dicom file using dicomwrite, MATLAB creates F new dicom files, where F is the number of frames.
On the contrary, I'd like to create only ONE dicom file, in which all F frames are stored.
Is it possible?
How can I do that? I have to modify dicomwrite function? Anybody has already done that?
Thank you so much for your help!!!
Sara.

Subject: multiframe dicomwrite

From: sara sacchi

Date: 28 Dec, 2008 12:46:02

Message: 2 of 18

I did it: dicomwrite function modified. Now it can write multiframe dicom files, and I can view these files also with some other (few) dicom viewers. For simiplicity, I implemented only ad hoc ultrasound case. (For the time being, I'm just experimenting with it!!!).
I have some problems with quality of the image: it decreases when writing a new multiframe dicom file using my function. I haven't discovered the reason why yet. I think it could be something about data classes (uint8, uint16, double)... I'll think about it! If any suggestion, please let me know! Thank you for help!
Sara.



"sara sacchi" <sara_sacchi@hotmail.com> wrote in message <gi4496$5k$1@fred.mathworks.com>...
> Hi!
> I'm working with multiframe dicom files.
> When reading them with dicomread function, MATLAB create a 4D-array (size NxMxDxF) where image data are stored.
> When I try to create a new multiframe dicom file using dicomwrite, MATLAB creates F new dicom files, where F is the number of frames.
> On the contrary, I'd like to create only ONE dicom file, in which all F frames are stored.
> Is it possible?
> How can I do that? I have to modify dicomwrite function? Anybody has already done that?
> Thank you so much for your help!!!
> Sara.

Subject: multiframe dicomwrite

From: Jeffrey

Date: 13 May, 2009 19:28:01

Message: 3 of 18

How did you modify it. Is it working well? It would be great if matlab implemented this.

-Jeff


"sara sacchi" <sara_sacchi@hotmail.com> wrote in message <gj7sea$93r$1@fred.mathworks.com>...
> I did it: dicomwrite function modified. Now it can write multiframe dicom files, and I can view these files also with some other (few) dicom viewers. For simiplicity, I implemented only ad hoc ultrasound case. (For the time being, I'm just experimenting with it!!!).
> I have some problems with quality of the image: it decreases when writing a new multiframe dicom file using my function. I haven't discovered the reason why yet. I think it could be something about data classes (uint8, uint16, double)... I'll think about it! If any suggestion, please let me know! Thank you for help!
> Sara.
>
>
>
> "sara sacchi" <sara_sacchi@hotmail.com> wrote in message <gi4496$5k$1@fred.mathworks.com>...
> > Hi!
> > I'm working with multiframe dicom files.
> > When reading them with dicomread function, MATLAB create a 4D-array (size NxMxDxF) where image data are stored.
> > When I try to create a new multiframe dicom file using dicomwrite, MATLAB creates F new dicom files, where F is the number of frames.
> > On the contrary, I'd like to create only ONE dicom file, in which all F frames are stored.
> > Is it possible?
> > How can I do that? I have to modify dicomwrite function? Anybody has already done that?
> > Thank you so much for your help!!!
> > Sara.

Subject: multiframe dicomwrite

From: Sune Buhl

Date: 14 May, 2009 07:38:01

Message: 4 of 18

Hi.

Sounds great - I am also really interested in how you did the multiframe dicomwrite to a single file.

Can you maybe upload your code or give a hint?

And yes, an implementation by MATLAB would be great.

Subject: multiframe dicomwrite

From: sara sacchi

Date: 16 May, 2009 11:58:01

Message: 5 of 18

Hi guys,
it's true I modified the dicomwrite function in order to write multiframe dicom files, but I didn't succeed in solving problems with the quality of the image... and a lot of other details... which make my function not usefull in practice. As you certainly know, dicom is a standard, and files have to be validated and follow a lot ofstrict rules. Well my function create something like a dicom multiframe, but for sure it's a NOT VALIDATED dicom multiframe. Thus... unluckly, I don't know if it could be actually usefull for you.
To be honest, since I'm very busy in this periode, I stopped thinking about it: it remain just a draft. As I've just written, I didin't succeed in solving all its bugs. If you want to have a look on the code (I warn you: it's a very rough code!) I can post it. I need some time to do it, because my notebook crashed, I have to olay some time with it to extract my data. But I can do it. Give me just some time!
bye!
sara.

"Jeffrey " <ios@removeforspam.jhu.edu> wrote in message <guf701$18r$1@fred.mathworks.com>...
> How did you modify it. Is it working well? It would be great if matlab implemented this.
>
> -Jeff
>
>
> "sara sacchi" <sara_sacchi@hotmail.com> wrote in message <gj7sea$93r$1@fred.mathworks.com>...
> > I did it: dicomwrite function modified. Now it can write multiframe dicom files, and I can view these files also with some other (few) dicom viewers. For simiplicity, I implemented only ad hoc ultrasound case. (For the time being, I'm just experimenting with it!!!).
> > I have some problems with quality of the image: it decreases when writing a new multiframe dicom file using my function. I haven't discovered the reason why yet. I think it could be something about data classes (uint8, uint16, double)... I'll think about it! If any suggestion, please let me know! Thank you for help!
> > Sara.
> >
> >
> >
> > "sara sacchi" <sara_sacchi@hotmail.com> wrote in message <gi4496$5k$1@fred.mathworks.com>...
> > > Hi!
> > > I'm working with multiframe dicom files.
> > > When reading them with dicomread function, MATLAB create a 4D-array (size NxMxDxF) where image data are stored.
> > > When I try to create a new multiframe dicom file using dicomwrite, MATLAB creates F new dicom files, where F is the number of frames.
> > > On the contrary, I'd like to create only ONE dicom file, in which all F frames are stored.
> > > Is it possible?
> > > How can I do that? I have to modify dicomwrite function? Anybody has already done that?
> > > Thank you so much for your help!!!
> > > Sara.

Subject: multiframe dicomwrite

From: Lead Bucket

Date: 21 May, 2010 18:25:22

Message: 6 of 18

I have struggled with this same issue. After exploring the dicomwrite function a bit, it looks like the functionality is there for a multiframe dicomwrite, but its just not activated (at least as of my version which is R2008b). To get a multiframe write to a single file, I had to modify the private function dicom_encode_jpeg_lossy.m (and any other encoding function I planned on using - there seemed to be 3 provided) to cycle through the multiple frames (they are currently set to implement only a single frame). Second, I had to modify dicomwrite.m to remove the for loop associated with the multiple frames along with the three lines that had dependencies on the loop variable p (the dicom_create_IOD function call, the dicom_copy_IOD function call, and the get_filename function call which I just changed to destination = filename;).

The rest of the functionality for a multiframe write is there, so once you implement the above changes, it seems to work fine. I'm no expert on dicom format and I don't know if the resulting dicom file meets dicom specs, but all I needed was the data and not the remaining dicom info, so this worked for me.

Subject: multiframe dicomwrite

From: Lead Bucket

Date: 21 May, 2010 18:25:22

Message: 7 of 18

I have struggled with this same issue. After exploring the dicomwrite function a bit, it looks like the functionality is there for a multiframe dicomwrite, but its just not activated (at least as of my version which is R2008b). To get a multiframe write to a single file, I had to modify the private function dicom_encode_jpeg_lossy.m (and any other encoding function I planned on using - there seemed to be 3 provided) to cycle through the multiple frames (they are currently set to implement only a single frame). Second, I had to modify dicomwrite.m to remove the for loop associated with the multiple frames along with the three lines that had dependencies on the loop variable p (the dicom_create_IOD function call, the dicom_copy_IOD function call, and the get_filename function call which I just changed to destination = filename;).

The rest of the functionality for a multiframe write is there, so once you implement the above changes, it seems to work fine. I'm no expert on dicom format and I don't know if the resulting dicom file meets dicom specs, but all I needed was the data and not the remaining dicom info, so this worked for me.

Subject: multiframe dicomwrite

From: lovely_m M

Date: 30 Jun, 2010 09:47:03

Message: 8 of 18

Hi Lead Bucket,

Great that you figured it out :) I've been trying to modify the functions according to your description but I can't get it to work. Could you maybe post the exact changes you made?

Many thanks,
M

Subject: multiframe dicomwrite

From: Lead Bucket

Date: 20 Jul, 2010 22:11:03

Message: 9 of 18

"lovely_m M" <lovely_m@hotmail.nl> wrote in message <i0f3qn$eng$1@fred.mathworks.com>...
> Hi Lead Bucket,
>
> Great that you figured it out :) I've been trying to modify the functions according to your description but I can't get it to work. Could you maybe post the exact changes you made?
>
> Many thanks,
> M

Hi M,

First, I created a copy of the private directory .../toolbox/images/iptformats/private and dicomwrite so I didn't muck things up with the original version. Then, in the dicom_encode_jpeg_lossy.m file, I modified frames to reflect the true number of frames and added a for loop:

frames = size(X,4);
for p = 1:frames
    tempfile = tempname;
    imwrite(X(:,:,:,p), tempfile, 'jpeg');
.....
   %frames = 1;
.....
end

Next, in dicomwrite.m I commented out the for loop that cycled through the frames, removed the dependency of X on p, and changed the destination to filename, all in the write_message subfunction:

.....
%for p = 1:num_frames
.....
    [attrs, msg, status] = dicom_create_IOD(SOP_UID, X, map, ...
.....
    [attrs, msg, status] = dicom_copy_IOD(X, map, ...
,....
    destination = filename;
    %destination = get_filename(filename, p, num_frames);
.....
%end

That was it. It seemed to work fine for me here, although I was using the 'copy' option. I didn't test it for the other options. It gave me one or two warnings on one of the dicom variables not being correct, but I was okay with that.

Hope this works for you!
-LB

Subject: multiframe dicomwrite

From: K P

Date: 5 Jan, 2011 18:33:05

Message: 10 of 18

Hi Lead Bucket,

Could you please give me more info on the code for multiframe dicomwrite. I am creating a multiframe dicom file with several tif images.
I tried the changes in the two functions as you mentioned but how am i suppose to add multiple frames using dicomwrite function( ie. in the programme code what is the syntax for adding multiple frames at a time). I am still ale to add only one frame.
Looking forward for your help.

Thank You
Kamlesh

"Lead Bucket" wrote in message <i256tn$gj5$1@fred.mathworks.com>...
> "lovely_m M" <lovely_m@hotmail.nl> wrote in message <i0f3qn$eng$1@fred.mathworks.com>...
> > Hi Lead Bucket,
> >
> > Great that you figured it out :) I've been trying to modify the functions according to your description but I can't get it to work. Could you maybe post the exact changes you made?
> >
> > Many thanks,
> > M
>
> Hi M,
>
> First, I created a copy of the private directory .../toolbox/images/iptformats/private and dicomwrite so I didn't muck things up with the original version. Then, in the dicom_encode_jpeg_lossy.m file, I modified frames to reflect the true number of frames and added a for loop:
>
> frames = size(X,4);
> for p = 1:frames
> tempfile = tempname;
> imwrite(X(:,:,:,p), tempfile, 'jpeg');
> .....
> %frames = 1;
> .....
> end
>
> Next, in dicomwrite.m I commented out the for loop that cycled through the frames, removed the dependency of X on p, and changed the destination to filename, all in the write_message subfunction:
>
> .....
> %for p = 1:num_frames
> .....
> [attrs, msg, status] = dicom_create_IOD(SOP_UID, X, map, ...
> .....
> [attrs, msg, status] = dicom_copy_IOD(X, map, ...
> ,....
> destination = filename;
> %destination = get_filename(filename, p, num_frames);
> .....
> %end
>
> That was it. It seemed to work fine for me here, although I was using the 'copy' option. I didn't test it for the other options. It gave me one or two warnings on one of the dicom variables not being correct, but I was okay with that.
>
> Hope this works for you!
> -LB

Subject: multiframe dicomwrite

From: sara sacchi

Date: 21 Jan, 2011 10:06:04

Message: 11 of 18

Hi guys,
Although I stopped thinking about the multiframe dicomwrite beacuse of other commitments, reading your updates makes me want to handle again this issue.
I tried the solution suggested by LB and it works fine!
Of course, you cannot guaranteed about DICOM specs validation, but in my opinion it's a good solution to be used for unofficial or academic purposes.
I agree with you: an official and validated MATLAB implementation of this functionality would be great!

Just a clarification for Kamlesh: maybe you've already suceeded in making it work, but if not... see below!
The instructions that LB gave us are corrected, but the following oversight:
in the dicom_encode_jpeg_lossy.m file, also del line fragments{1}=..... need to be changed in order to take into account the p parameter.
See below.

frames = size(X,4);
 for p = 1:frames
     tempfile = tempname;
     imwrite(X(:,:,:,p), tempfile, 'jpeg');
     .....
     fragments{p} = fread(fid, inf, 'uint8=>uint8');
     %fragments{1} = fread(fid, inf, 'uint8=>uint8');
     %frames = 1;
     .....
 end

Hoping this helps!
Thank you guys!
Best regards,
Sara.



"K P" <kamleshprakash.p@gmail.com> wrote in message <ig2dh1$np7$1@fred.mathworks.com>...
> Hi Lead Bucket,
>
> Could you please give me more info on the code for multiframe dicomwrite. I am creating a multiframe dicom file with several tif images.
> I tried the changes in the two functions as you mentioned but how am i suppose to add multiple frames using dicomwrite function( ie. in the programme code what is the syntax for adding multiple frames at a time). I am still ale to add only one frame.
> Looking forward for your help.
>
> Thank You
> Kamlesh
>
> "Lead Bucket" wrote in message <i256tn$gj5$1@fred.mathworks.com>...
> > "lovely_m M" <lovely_m@hotmail.nl> wrote in message <i0f3qn$eng$1@fred.mathworks.com>...
> > > Hi Lead Bucket,
> > >
> > > Great that you figured it out :) I've been trying to modify the functions according to your description but I can't get it to work. Could you maybe post the exact changes you made?
> > >
> > > Many thanks,
> > > M
> >
> > Hi M,
> >
> > First, I created a copy of the private directory .../toolbox/images/iptformats/private and dicomwrite so I didn't muck things up with the original version. Then, in the dicom_encode_jpeg_lossy.m file, I modified frames to reflect the true number of frames and added a for loop:
> >
> > frames = size(X,4);
> > for p = 1:frames
> > tempfile = tempname;
> > imwrite(X(:,:,:,p), tempfile, 'jpeg');
> > .....
> > %frames = 1;
> > .....
> > end
> >
> > Next, in dicomwrite.m I commented out the for loop that cycled through the frames, removed the dependency of X on p, and changed the destination to filename, all in the write_message subfunction:
> >
> > .....
> > %for p = 1:num_frames
> > .....
> > [attrs, msg, status] = dicom_create_IOD(SOP_UID, X, map, ...
> > .....
> > [attrs, msg, status] = dicom_copy_IOD(X, map, ...
> > ,....
> > destination = filename;
> > %destination = get_filename(filename, p, num_frames);
> > .....
> > %end
> >
> > That was it. It seemed to work fine for me here, although I was using the 'copy' option. I didn't test it for the other options. It gave me one or two warnings on one of the dicom variables not being correct, but I was okay with that.
> >
> > Hope this works for you!
> > -LB

Subject: multiframe dicomwrite

From: Lead Bucket

Date: 26 Jan, 2011 17:45:05

Message: 12 of 18


> Just a clarification for Kamlesh: maybe you've already suceeded in making it work, but if not... see below!
> The instructions that LB gave us are corrected, but the following oversight:
> in the dicom_encode_jpeg_lossy.m file, also del line fragments{1}=..... need to be changed in order to take into account the p parameter.
> See below.
>
> frames = size(X,4);
> for p = 1:frames
> tempfile = tempname;
> imwrite(X(:,:,:,p), tempfile, 'jpeg');
> .....
> fragments{p} = fread(fid, inf, 'uint8=>uint8');
> %fragments{1} = fread(fid, inf, 'uint8=>uint8');
> %frames = 1;
> .....
> end
>

Thanks for the correcting my oversight Sara! Kamlesh, I think that should solve your problem. Sorry about the omission.

-LB

Subject: multiframe dicomwrite

From: Patrick

Date: 19 May, 2011 21:50:07

Message: 13 of 18

I have gotten these modifications to work for me ... i would appear you need a multiframe dicom image to use as "template" in order for it to work. Read in the metadata from the template using dicominfo, then pass the metadata when you use dicomwrite with the 'CreateMode', 'copy' paramaeter as Lead Bucket suggests.

At least when I read this back in from MATLAB dicomread, this works and returns an 4 D arrray with my image data. The NEMA reader I have is complaining about not supporting jpeg.

If I don't do this, it won't work ... reading the file back in results in only a single frame and the NumberOfFrames field is missing from dicominfo. There may be a way to add the 'NumberOfFrames' field to the metadata, so you don't need a template file, but not sure if I have the time to bother with that.

Subject: multiframe dicomwrite

From: Patrick

Date: 19 May, 2011 22:05:05

Message: 14 of 18

Looks like I spoke to soon, it is giving me back an array of the correct dimensions, but the data is junk... *sigh*

Subject: multiframe dicomwrite

From: Yao

Date: 6 Jul, 2011 02:44:12

Message: 15 of 18

"Patrick " <praphael@gmail.com> wrote in message <ir446h$nb4$1@newscl01ah.mathworks.com>...
> Looks like I spoke to soon, it is giving me back an array of the correct dimensions, but the data is junk... *sigh*

Hello everyone, are you still working on this function?

I guess I kind of succeeded in the first part where I got a 1xp cell named fragments. Then I followed the instructions on dicomwrite modification and stopped with the following error reported:

Undefined function or method 'dicom_create_IOD' for input arguments of type 'cell'.

Can anybody tell me what I should do next? Is there anything wrong with my program? If not, how can I convert cell to dcm (or other formats recognizable)?

many thanks!

ZZ

Subject: multiframe dicomwrite

From: Lead Bucket

Date: 9 Jul, 2011 00:26:09

Message: 16 of 18

"Patrick " <praphael@gmail.com> wrote in message <ir43af$kr9$1@newscl01ah.mathworks.com>...
> I have gotten these modifications to work for me ... i would appear you need a multiframe dicom image to use as "template" in order for it to work. Read in the metadata from the template using dicominfo, then pass the metadata when you use dicomwrite with the 'CreateMode', 'copy' paramaeter as Lead Bucket suggests.
>
> At least when I read this back in from MATLAB dicomread, this works and returns an 4 D arrray with my image data. The NEMA reader I have is complaining about not supporting jpeg.
>
> If I don't do this, it won't work ... reading the file back in results in only a single frame and the NumberOfFrames field is missing from dicominfo. There may be a way to add the 'NumberOfFrames' field to the metadata, so you don't need a template file, but not sure if I have the time to bother with that.

Patrick, I'm not sure why NumberOfFrames is missing from dicominfo, mine seems to be retained. However, I do recall that I had an issue with dicomread when it tried to read multiframe dicom file and the dicom data did not contain an offset table. I think I mentioned this somewhere in another thread, but the basic problem was that dicomread looks for an offset table, and if none exists it assumes that there is only one image and just loads the first frame (it completely disregards NumberOfFrames).

I believe that no offset table is produced for the modifications I provided to dicomwrite, so the same problem may persist when you try to use dicomread. I've had to modify dicomread so that it uses NumberOfFrames to read in the frames. That will probably be an issue if NumberOfFrames doesn't exist for you. Perhaps your data is coming out junk due to one of these issues, but I'm really not that familiar with dicom data formats.

As for your NEMA reader, I am unfamiliar with that (I don't really know much about dicom files/formats either - its not my area of expertise), but I do recall that ImageJ has an issue with compressed JPEG in dicom files. Maybe its the same thing with your NEMA reader.

Yao - The issue might be that you are using the 'Create' option for 'CreateMode' (this is the default, by the way, if you don't specify 'Copy'). I always needed to use the 'Copy' option because Matlab does not support the data type I was working with (which was ultrasound data). I do get a few warnings about some of the attributes being wrong in the dicom info, but for my work I don't really need the dicom info, so its not an issue for me.

-LB

Subject: multiframe dicomwrite

From: Jaroslav

Date: 1 Jun, 2012 10:50:12

Message: 17 of 18

"Lead Bucket" wrote in message <iv8771$i9m$1@newscl01ah.mathworks.com>...
> "Patrick " <praphael@gmail.com> wrote in message <ir43af$kr9$1@newscl01ah.mathworks.com>...
> > I have gotten these modifications to work for me ... i would appear you need a multiframe dicom image to use as "template" in order for it to work. Read in the metadata from the template using dicominfo, then pass the metadata when you use dicomwrite with the 'CreateMode', 'copy' paramaeter as Lead Bucket suggests.
> >
> > At least when I read this back in from MATLAB dicomread, this works and returns an 4 D arrray with my image data. The NEMA reader I have is complaining about not supporting jpeg.
> >
> > If I don't do this, it won't work ... reading the file back in results in only a single frame and the NumberOfFrames field is missing from dicominfo. There may be a way to add the 'NumberOfFrames' field to the metadata, so you don't need a template file, but not sure if I have the time to bother with that.
>
> Patrick, I'm not sure why NumberOfFrames is missing from dicominfo, mine seems to be retained. However, I do recall that I had an issue with dicomread when it tried to read multiframe dicom file and the dicom data did not contain an offset table. I think I mentioned this somewhere in another thread, but the basic problem was that dicomread looks for an offset table, and if none exists it assumes that there is only one image and just loads the first frame (it completely disregards NumberOfFrames).
>
> I believe that no offset table is produced for the modifications I provided to dicomwrite, so the same problem may persist when you try to use dicomread. I've had to modify dicomread so that it uses NumberOfFrames to read in the frames. That will probably be an issue if NumberOfFrames doesn't exist for you. Perhaps your data is coming out junk due to one of these issues, but I'm really not that familiar with dicom data formats.
>
> As for your NEMA reader, I am unfamiliar with that (I don't really know much about dicom files/formats either - its not my area of expertise), but I do recall that ImageJ has an issue with compressed JPEG in dicom files. Maybe its the same thing with your NEMA reader.
>
> Yao - The issue might be that you are using the 'Create' option for 'CreateMode' (this is the default, by the way, if you don't specify 'Copy'). I always needed to use the 'Copy' option because Matlab does not support the data type I was working with (which was ultrasound data). I do get a few warnings about some of the attributes being wrong in the dicom info, but for my work I don't really need the dicom info, so its not an issue for me.
>
> -LB

I would like to ask, if this all is possible also for writing without compression. Better said, what kind of modifications and where should be made to enable this.
Thank you very much.

Subject: multiframe dicomwrite

From: Lead Bucket

Date: 9 Jul, 2012 21:19:08

Message: 18 of 18


> I would like to ask, if this all is possible also for writing without compression. Better said, what kind of modifications and where should be made to enable this.
> Thank you very much.

It is possible. Either direct your program to use dicom_encode_jpeg_lossless or modify the function to use whatever lossless encoder you wish (with the modifications for multiframe writes, of course!). This function just uses imwrite to created the dicom image data, and you can (probably) use any of the available formats under imwrite. I do not know, however, if you will violate any dicom standards by using the other encoders.

-LB

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us