Converting JPEG2000 DICOMs to Uncompressed DICOM

TL;DR – Neuroimaging-specific post. Some files can't be converted by programs if they are encoded one way – present Matlab program to fix that.

JPEG2000 DICOM data

Recently, I had some DICOM data that is stored in the JPEG2000 format from OsiriX. I wanted to convert these to a NIfTI format using dcm2nii but it was not a fan of the compression, specifically the transfer syntax (which tells you how the data is encoded).

It spit out the error:

Unsupported Transfer Syntax 1.2.840.10008. Solution: use MRIcro

Using Matlab's Image Processing Toolbox

For most of my endeavors, I try to use R for everything, but that's not always possible. For example the oro.dicom package is great for working with DICOM data, but the JPEG2000 compression format is not supported. Therefore, I went into Matlab, which has the image processing toolbox. You will need an updated version, as described here, Matlab 2009b version will not work.

The Code for Conversion

Overall, the code takes a directory, lists all the files, removing directories. . The for loop goes through each DICOM file, reads in the header information with dicominfo, reads in the data matrix with dicomread. The transfer syntax is changed using newsyntax, creates a new filename (within a specified output directory outdir) and writes the DICOM file using dicomwrite. The createmode can either be Create or Copy. Using Create is the default option and has error and missing checking. The Copy mode bypasses many of these checks, but this may be required for certain DICOM files to be written.

rundir = pwd;
updir = fileparts(rundir);
outdir = fullfile(updir, 'Converted');
addon = '';
newsyntax = '1.2.840.10008.1.2';
createmode = 'Copy'; % could be 'Create';
x = dir(rundir);
nodir = ~[x.isdir]';
x = x(nodir);
x = {}';
ifile = 1;
for ifile = 1:length(x)
    name = x{ifile};
    stub = regexprep(name, '(.*)[.].*', '$1');
    stub = [stub addon '.dcm']; %#ok<AGROW>
    name = fullfile(rundir, name);
    stub = fullfile(outdir, stub);
    dinfo = dicominfo(name);
    dinfo.TransferSyntaxUID = newsyntax;
    X = dicomread(name);
    dicomwrite(X, stub, dinfo, 'CreateMode', createmode);

After this conversion, dcm2nii worked as usual for converting.

Other Options

Other options exist such as the DICOM JPEG 2000 Module, but this must be licensed and Matlab has implemented this in imread. Just figured if you ran into this problem, this may help.

6 thoughts on “Converting JPEG2000 DICOMs to Uncompressed DICOM

  1. Hi there,

    Thanks for the info. I ran this code and revived the following error. I am certain the contents of my pwd are “.dcm” files. Any thoughts as to why I might be getting this error?


    Error using dicominfo (line 39)
    The specified file is not in DICOM format.

      • That’s precisely the problem. I usually work with PAR/REC files and this current set of data is from a new scanner using .dcm files of which I have never worked with before. My end goal is to confer them to NIFTI for analysis in FSL. It didn’t work to use MRI convert or dcm2nii programs either. So, I am guessing they are not “standard” which is why I was trying to use your program in hopes that they were Jpeg and I might convert them to something more compatible with MRIconvert and dcm2nii. Do you have an email address I might send an example folder to?

    • Just thought I would update. In the end, I was able to convert the files from DICOM to NIFITI using a new version of dcm2nii, called dcm2niix. This newer version is able to convert .dcm files of many types, thus being more flexible than the previous dcm2nii. Thanks anyways!

  2. I know this post is about a year old but still wanted to comment to thank you for sharing your code. I just ran into the same problem and didn’t think the fix could actually be as simple as changing a single part of the DICOM information – seeing that your code worked was extremely helpful 🙂 Thanks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s