1
votes

I have a dicom file that contains 110 images and their names are random. I am trying to rename them according to their SliceLocations (n) and rewrite them with the dcm extension, i.e., n(1).dcm, n(2).dcm, …

Any suggestions would be appreciated.

I tried

image_list=dir('*.dcm');
for i=1:110
img=dicomread(image_list);
imgHdr = dicominfo(image_list(i).name);
  for j = size(img,4);
    dicomwrite(img(:,:,:,j),['n(' int2str(j) ').dcm'],imgHdr,'CreateMode','Copy');

  end
end
2

2 Answers

2
votes

Slice location (0020,1041) is an optional attribute for image plane module. There is no guarantee that tag will have any value. Your best option is to use the Image Position (Patient) (0020, 0032) attribute. This will have x, y, and z coordinates of the upper left hand corner (center of the first voxel transmitted) of the image. See DICOM standard PS 3.3 - 2011 (PDF), Annex C.7.6.2.1, for further explanation.

1
votes

NOTE: This answer applies to "frames," not "slices." The two are apparently different things for DICOM files.

One convenient option, if you don't need exactly the naming scheme in your question, is to use the 'MultiframeSingleFile' option in the dicomwrite function:

X = dicomread('MultiFrameFile.dcm');
dicomwrite(X,'n.dcm','MultiframeSingleFile',false);

This will produce files named 'n_01.dcm', 'n_02.dcm', ...

Otherwise you can use a simple for loop in conjunction with int2str:

X = dicomread('MultiFrameFile.dcm');
for i = size(X,4);
    dicomwrite(X(:,:,:,i),['n(' int2str(i) ').dcm']);
end