How to convert tif stack to dcm series?

I have a tif file that contains 1813 pages (images). I want to convert these to DICOM files within a single series. Currently, the code I am using writes over the .dcm file during each loop. How can I fix this?
fname = 'Sample19.tif';
info = imfinfo(fname);
imageStack = [];
numberOfImages = length(info);
for k = 1:numberOfImages
currentImage = imread(fname, k, 'Info', info);
dicomwrite(currentImage,'gear.dcm')
imageStack(:,:,k) = currentImage;
end

1 Comment

I encouter the same problem. Did you find a solution?

Sign in to comment.

Answers (1)

fname = 'Sample19.tif';
info = imfinfo(fname);
imageStack = [];
numberOfImages = length(info);
for k = 1:numberOfImages
currentImage = imread(fname, k, 'Info', info);
imageStack(:,:,k) = currentImage;
end
dicomwrite(imageStack,'gear.dcm')
However, in practice you almost always need a bunch of dicom metadata. The easiest way to get that is to use dicominfo() on a dcm file that has the same kind of attributes that you are going to need, and pass the resulting metadata to dicomwrite after the file name.

8 Comments

I updated the code so it is now:
otherinfo = dicominfo('image-000001.dcm')
fname = 'Sample19.tif';
info = imfinfo(fname);
imageStack = [];
% numberOfImages = length(info);
for k = 1:100
% for k = 1:numberOfImages
currentImage = imread(fname, k, 'Info', info);
imageStack(:,:,k) = currentImage;
end
dicomwrite(imageStack,'gear.dcm',otherinfo);
I am now getting the following errors ( I am not sure how to fix this).
Error using dicom_prep_ImagePixel>getPhotometricInterp (line 134)
Cannot determine photometric interpretation.
Error in dicom_prep_ImagePixel (line 9)
metadata.(dicom_name_lookup('0028', '0004', dictionary)) = getPhotometricInterp(metadata, X, map, txfr, dictionary);
Error in dicom_prep_metadata (line 14)
metadata = dicom_prep_ImagePixel(metadata, X, map, txfr, useMetadataBitDepths, dictionary);
Error in dicom_create_IOD (line 26)
metadata = dicom_prep_metadata(IOD_UID, metadata, X, map, options.txfr, options.usemetadatabitdepths, dictionary);
Error in dicomwrite>write_message (line 274)
[attrs, status] = dicom_create_IOD(SOP_UID, X, map, ...
Error in dicomwrite (line 210)
[status, options] = write_message(X, filename, map, metadata, options);
Error in convert (line 14)
dicomwrite(imageStack,'gear.dcm',otherinfo);
Please post the content of otherinfo
otherinfo =
struct with fields:
Filename: 'E:\Gear Model\image-000001.dcm'
FileModDate: '05-Oct-2018 12:02:18'
FileSize: 91494
Format: 'DICOM'
FormatVersion: 3
Width: 512
Height: 512
BitDepth: 12
ColorType: 'grayscale'
FileMetaInformationGroupLength: 230
FileMetaInformationVersion: [2×1 uint8]
MediaStorageSOPClassUID: '1.2.840.10008.5.1.4.1.1.2'
MediaStorageSOPInstanceUID: '1.2.826.0.1.3680043.8.1055.1.20111102150758825.42401392.26682309'
TransferSyntaxUID: '1.2.840.10008.1.2.4.91'
ImplementationClassUID: '1.2.826.0.1.3680043.8.1055.1'
ImplementationVersionName: 'dicomlibrary-100'
SourceApplicationEntityTitle: 'DICOMLIBRARY'
SpecificCharacterSet: 'ISO_IR 100'
ImageType: 'ORIGINAL\PRIMARY\AXIAL\HELIX'
InstanceCreationDate: '20061012'
InstanceCreationTime: '091606.000000'
SOPClassUID: '1.2.840.10008.5.1.4.1.1.2'
SOPInstanceUID: '1.2.826.0.1.3680043.8.1055.1.20111102150758825.42401392.26682309'
StudyDate: '20061012'
AcquisitionDate: '20061012'
ContentDate: '20061012'
StudyTime: '090258.000000'
AcquisitionTime: '085229.000000'
ContentTime: '085229.750000'
Modality: 'CT'
StudyDescription: 'CT1 abdomen'
ProcedureCodeSequence: [1×1 struct]
SeriesDescription: 'ARTERIELLE'
ReferencedPerformedProcedureStepSequence: [1×1 struct]
PatientName: [1×1 struct]
PatientID: '0'
PatientAge: '000Y'
ContrastBolusAgent: 'CONTRAST'
ScanOptions: 'HELIX'
SliceThickness: 1
KVP: 120
SpacingBetweenSlices: 0.5000
DataCollectionDiameter: 302
ProtocolName: 'ART.RENALES 12/Abdomen/Hx'
ReconstructionDiameter: 302
GantryDetectorTilt: 0
TableHeight: 151
RotationDirection: 'CW'
XrayTubeCurrent: 400
Exposure: 300
FilterType: 'B'
ConvolutionKernel: 'B'
PatientPosition: 'FFS'
StudyInstanceUID: '1.2.826.0.1.3680043.8.1055.1.20111102150758591.92402465.76095170'
SeriesInstanceUID: '1.2.826.0.1.3680043.8.1055.1.20111102150758591.96842950.07877442'
SeriesNumber: 6168
InstanceNumber: 2
ImagePositionPatient: [3×1 double]
ImageOrientationPatient: [6×1 double]
FrameOfReferenceUID: '1.2.840.113704.1.111.3704.1160637109.3'
SliceLocation: -324.5000
ImageComments: 'JPEG 2000 lossless - Version 4.0.2 (c) Image Devices GmbH'
SamplesPerPixel: 1
PhotometricInterpretation: 'MONOCHROME2'
Rows: 512
Columns: 512
PixelSpacing: [2×1 double]
BitsAllocated: 16
BitsStored: 12
HighBit: 11
PixelRepresentation: 0
WindowCenter: [2×1 double]
WindowWidth: [2×1 double]
RescaleIntercept: -1000
RescaleSlope: 1
LossyImageCompression: '01'
LossyImageCompressionRatio: 5.8667
ScheduledProcedureStepDescription: 'CT1 abdomen'
ScheduledProtocolCodeSequence: [1×1 struct]
ScheduledProcedureStepID: 'A10026177758'
PerformedProcedureStepDescription: 'CT1 abdomen'
PerformedProtocolCodeSequence: [1×1 struct]
RequestAttributesSequence: [1×1 struct]
RequestedProcedureID: 'A10026177757'
I tried to get the dicom meta data from an DICOM image from a CT scan that I have one my computer. I am trying to convert a .tif file of a gear into .dcm files.
Sorry, I do not know at the moment. That is the sort of thing where I would want to use a debugger to figure out why it cannot determine the photometric interpretation.
TransferSyntaxUID: '1.2.840.10008.1.2.4.91'
ImageComments: 'JPEG 2000 lossless - Version 4.0.2 (c) Image Devices GmbH'
That is odd. Transfer Syntax 1.2.840.10008.1.2.4.91 is JPEG 2000 Image Compression, not lossless -- lossless is 1.2.840.10008.1.2.4.90, or 1.2.840.10008.1.2.4.92 (multicomponent)
Here are a couple of things to try/keep in mind:
  • Use dicomwrite(img, filename, 'CreateMode', 'copy', info) to preserve as much metadata (in info) as possible.
  • To make all of the images be part of the same series and study, use dicomuid to create a couple of UIDs and update the metadata fields info.SeriesInstanceUID and info.StudyInstanceUID.
  • You'll need to update the info.ImagePositionPatient metadata value for each slice, otherwise they'll all be on top of each other. The field specifies the (x/y/z) location of the upper-left pixel in the patient coordinate system (measured in milimeters). If you know the stepping in the Z direction, this should be easy to compute and update for each loop. If not, you'll have to make something up.
  • For broader compatibility, it might be a good idea to try to use a transfer syntax other than JPEG-2000. Consider setting info.TransferSyntaxUID to 1.2.840.10008.1.2.5 (for RLE lossless) or 1.2.840.10008.1.2.4.57 (for JPEG lossless).
I admit, this could be a little simpler.
I encouter the same problem, did you get a solution?
Thank you Julianna Mather you solved my four days problem!

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 9 Aug 2020

Commented:

on 20 Feb 2024

Community Treasure Hunt

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

Start Hunting!