DICOM medical image processing: Comparative Analysis of mdcm and DCMTK open source libraries (I). JPEG lossless compression of DCM Images

Source: Internet
Author: User
Background:

To meet recent project requirements, you must use C # To develop the latest UI and related dicom3.0 Medical Image modules. In the C ++ language, I use the most widely used DCMTK open source library. Most of the blog posts in the beginning of this column are about and learn about the DCMTK open source library. Currently, as the project needs to continue learning and analyzing the mdcm open-source library, most of the subsequent articles in this column will take the mdcm open-source library as an example to explain medical images, because DCMTK is developed in C ++, we did not give up learning the Open Source library of DCMTK because it serves as the original basis for me to learn and analyze the mdcm open source library, instead, by carefully studying and analyzing the C ++ source code of DCMTK, we can switch to the medical image processing in the C # language environment more quickly.

Relationship between DCMTK and mdcm (Fo-DICOM:

The DCMTK official website provides detailed instructions on the various classes of the open-source Library and the dependency between classes. It is an indispensable resource for learning the latest dicom3.0 medical standards. Its official website is http://www.dcmtk.org/. the active address of the Forum is http://forum.dcmtk.org/index.php.

Currently, mdcm is transferred from the DCMTK open source library, or another branch of the open source project, it is the re-organization and encapsulation of the open source library of medical images in C ++ in C # language. Its project is hosted on the official website of GitHub: https://github.com/rcd/mdcm. You need to mention fo-DICOM here. This open source library is an upgraded version of mdcm, which adds several features. For details, see the GitHub Website: https://github.com/rcd/fo-dicom.

In general, this is the relationship between the three, so it also shows that we still need to quickly learn and analyze the mdcm (Fo-DICOM) Open Source Library Based on the DCMTK open source library, with the help of the rich and detailed instruction documents of the DCMTK open-source database, as well as active developer forums. Next we will compare and learn the differences between the mdcm and DCMTK open source libraries by performing lossless compression on the DCM images.

Comparison between DCMTK and mdcm for JPEG lossless compression of DCM images:

DCMTK provides an example of JPEG lossless compression. The Code is as follows:

/*************************************** **************************************
Dcmjpeg package
Dcmjpeg provides a compression/Decompression library and available tools. This module contains classes that can convert DICOM image objects between non-compression and JPEG compression representation (Transfer Protocol. Distortion-free and distortion-free JPEG processing are supported. This module implements a codec family (which is derived from the dcmcodec class). You can register these codec classes in the codec list. The codec list is saved by the dcmdata module.

Main Interface Class:
-- Djencoderregistration: A singleton (isolated) class that registers encoder for all supported JPEG processing. Defined in djencode. h.
-- Djdecoderregistration: A singleton (isolated) class that registers the decoder for all supported JPEG processing. Defined in djdecode. h.
-- Djcodecencoder: an abstract codec class of the JPEG encoder. This abstract class contains most of the application logic needed for a dcmdata codec object that implements a jpeg encoder using the djencoder interface to the underlying JPEG implementation. this class only supports compression, it neither implements decoding nor transcoding. in djcodece. h.
-- Djcodecdecoder: an abstract codec class of the JPEG decoder. This abstract class contains most of the application logic needed for a dcmdata codec object that implements a jpeg Decoder Using the djdecoder interface to the underlying JPEG implementation. this class only supports decompression, it neither implements encoding nor transcoding.
Tools:
Dcmcjpeg: encode DICOM file to JPEG transfer syntax
Dcmdjpeg: Decode JPEG-compressed DICOM file
Dcmj2pnm: Convert DICOM images to PGM, ppm, BMP, Tiff or JPEG
Dcmmkdir: Create a dicomdir File
Example:
-- Compress a DICOM image file with a non-distortion JPEG file.
**************************************** *************************************/
Djencoderregistration: registercodecs (); // register JPEG codecs
Dcmfileformat fileformat;
If (fileformat. LoadFile ("test. DCM"). Good ())
{
Dcmdataset * dataset = fileformat. getdataset ();
Dcmitem * metainfo = fileformat. getmetainfo ();
Dj_rplossless Params; // codec parameters, we use the defaults
// This causes the lossless JPEG version of the dataset to be created
Dataset-> chooserepresentation (exs_receivprocess14sv1transfersyntax, & Params );
// Check if everything went well
If (dataset-> canwritexfer (exs_export process14sv1transfersyntax ))
{
// Force the meta-header UIDs to be re-generated when storing the file
// Since the UIDs in the data set may have changed
Delete metainfo-> remove (dcm_mediastoragesopclassuid );
Delete metainfo-> remove (dcm_mediastoragesopinstanceuid );
// Store in Lossless JPEG format
Fileformat. SaveFile ("test_0000.dcm", exs_0000process14sv1transfersyntax );
}
}
Djencoderregistration: cleanup (); // deregister JPEG codecs

(The specific project configuration is described in the previous blog)

This code can be used to smoothly perform JPEG lossless compression on the DCM image. As shown in,

The professional DCM image editing editor of Sante DICOM editor is used to open the image before and after compression. It is found that there is no difference in the image quality. The actual size before compression is 4572 K, and the size after compression is 1774 K. The compression effect is good.

Imagine:Since the mdcm open source library is the encapsulation of the DCMTK open source library, the two open source libraries should have the same or similar functions. The code in the DCMTK instance shows that only

Dcmfileformat's LoadFile, SaveFile, and dcmdataset's chooserepresentation and canwritexfer functions. In terms of function names, we know that the two functions of dcmdataset's chooserepresentation and canwritexfer actually achieve the compression effect, then let's take a look at whether the dcmdataset in the mdcm open source library has a corresponding function?

The OSS Object Browser shows that the dcmdataset class in the DICOM. Data namespace under the mdcm open source library does have a function similar to changetransfersyntax, for example:

Guess:Directly call the load, changetransfersyntax, and save functions of mdcm to achieve the same effect as DCMTK, that is, to complete JPEG lossless compression of DCM.

The Code is as follows,

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading. tasks;
Using DICOM;
Using DICOM. Data;
Using DICOM. codec;
Using DICOM. codec. javasls;
Namespace using lossless
{
Class Program
{
Static void main (string [] ARGs)
{
Dicomcodec. registercodecs ();
// DICOM. codec. codecls. dcmjpeglscodec. Register ();
String fname = string. Format ("D: \ DCM \ test. DCM ");
Dicomfileformat FF = new dicomfileformat ();
Ff. Load (fname, dicomreadoptions. Default | dicomreadoptions. deferloadingpixeldata );
// Ff. Load (fname, dicomreadoptions. None );
Dcmpixeldata pixels = new dcmpixeldata (FF. dataset );

Dcmjpeglsparameters parameters = new dcmjpeglsparameters ();
Ff. filemetainfo. transfersyntax = dicomtransfersyntax. Export process14sv1;
Ff. dataset. changetransfersyntax (dicomtransfersyntax. jpegprocess14sv1, jpegparameters );
String OUTFILE = string. Format (@ "D: \ DCM \ OUTFILE. DCM ");
Ff. Save (OUTFILE, dicomwriteoptions. Default );
}
}
}

After the project is successfully compiled and debugged, a file of 1774k size is also displayed. However, an error occurs when the file is opened using Sante DICOM Editor, as shown in:

Use dcmtkto open the source database tool package dw.ump.exe to view the file OUTFILE. DCM compressed by mdcm. the following error message is output:

Warning: the value of the data element (,) is incorrect. The error is an unrecognized tag and data (f752, 0e57). After checking the dicom3.0 standard, no (f752, 0e57) is found. Use ultraedit to open the files test_0000.dcm and mdcm compressed by DCMTK. DCM, through the search function, it is found that the (f752, 0e57) field is actually the value field content of the (7fe0, 0010) field after the standard JPEG lossless compression (as shown in ), therefore, it is suggested that a field in the file header after mdcm compression is written incorrectly, so that the data body is not read according to the original dicom3.0 standard.

Solution:

The DCMTK project is used to read the OUTFILE of the file compressed by mdcm. DCM. After the result is debugged in one step, when the load function is used to read the JPEG compressed image, there is no problem with the metainfo part. However, when the dataset element is read, the reading of the (,) element is incorrect. The correct (,) element parsing method is

Element label (group, element): 08 00 00 00 -- (0008, 0000)

Element type, that is, Vr: 55 4c--ul

Element length, that is, Vl is: 04 00--0004 (length is 4)

Element value field, that is, Value Field: B8 00 00 00--00000008 (value: 184)

However, when reading dataset, 55 4C 04 00 is regarded as the length, soGuessYesElements in the format of explicitul are read as implicitvr., The file stream pointer _ streamposition directly jumps to 0x000000000000000000044db5 from 0x00000000000000044db5, as shown in:

Therefore, add a statement to the C # project of mdcm to manually modify the semantics transmitted in the object metadata,

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading. tasks;
Using DICOM;
Using DICOM. Data;
Using DICOM. codec;
Using DICOM. codec. javasls;
Namespace using lossless
{
Class Program
{
Static void main (string [] ARGs)
{
/*************************************** *************
* Compare DCMTK in C ++ to perform JPEG lossless compression on DICOM to learn C #
* Use of DICOM Library
* 2014-08-06
* Zssure
**************************************** ************/
Dicomcodec. registercodecs ();
String fname = string. Format (@ "D: \ DCM \ test. DCM ");
Dicomfileformat FF = new dicomfileformat ();
Ff. Load (fname, dicomreadoptions. Default | dicomreadoptions. deferloadingpixeldata );
Dcmjpeglsparameters parameters = new dcmjpeglsparameters ();
Ff. filemetainfo. transfersyntax = dicomtransfersyntax. Export process14sv1;
Ff. dataset. changetransfersyntax (dicomtransfersyntax. jpegprocess14sv1, jpegparameters );
String OUTFILE = string. Format (@ "D: \ DCM \ outfile1_22.dcm ");
Ff. Save (OUTFILE, dicomwriteoptions. Default );
}
}
}

After the project compilation, the function of compressing DCM can be successfully completed. Now, the objective of using mdcm to perform JPEG lossless compression on DICOM images has been achieved.

Summary:

Part 1 of the dicom3.0 standard provides a detailed description of the DCM file storage format. The transmission semantics is described as follows:

"

1) Before t for the 128 byte preamble and the 4 byte prefix,File meta informationShall be encoded usingExplicit VR little endian transfer syntax (uid = 1.2.840.10008.1.2.1)As defined in dicom ps 3.5. values of each file meta element shall be padded when necessary to achieve an even length, as specified in PS 3.5 by their corresponding value representation. the unknown (un) value representation shall not be used in the file meta information. for compatibility with future versions of this standard, any tag (0002, xxxx) not defined in Table 7.1-1 shall be ignored. values of all tags (0002, xxxx) are reserved for use bythis standard and later versions of DICOM. data elements with a group of 0002 shall not be used in datasets other than within the file meta information

2) The transfer syntax used to encode the dataset cannot be changed within the data set; I. E ., the transfer syntax uid data element may not occur anywhere within the data set, e.g ., nested within a sequence item.

"

Therefore, the label (0002,0010) in the meta information of the DCM file, that is, the transmission semantics, plays a key role in reading the data body dataset of the DCM file. The comparison between the mdcm open source Library and the DCMTK open source library shows that although most of the functions are the same and the names and functions are similar, you should pay attention to the details.

The following table lists the differences between the two open-source libraries that need to be compared and analyzed for the JPEG lossless compression function of the DCM files.

Mdcm

DCMTK

1) dicomfileformat. Load, open the file (also read the information of the DCM file one by one to the memory through the file Stream)

2) dicomfileformat. dataset. changetransfersyntax. This function is similar to the chooserepresentation function in DCMTK. New transmission semantics must be specified in the parameters. The function modifies the storage mode of the Data body based on the new transmission semantics. This function provides the following functions:

Compare the New and Old transmission semantics, and determine whether the data body is decompressed or compressed based on the new and old semantics (DICOM. codec. encode or DICOM. codec. Decode ).

3) dicomfileformat. Save: stores files, but the function does not need to fill in new transmission semantics.

[Note]: This is different from the SaveFile function in DCMTK. This is why the mdcm of C # in the previous week cannot be read smoothly after JPEG lossless compression of the DCM data. Because the data body storage format is not stored according to the transmission semantics specified in the object metadata, or the transmission semantics in the object metadata is not modified to JPEG lossless compression.

4) dicomfileformat: LoadFile: Import file, which consists of dc1_ainfo and dcmdataset;

5) dataset: choosereresentation. The new and old transmission semantics transfersyntax appear in the parameter. The function processes the corresponding data (mainly pixel data) based on the new semantics and calls dcmpixeldata :: canchooserepresentation, dcmpixeldata: chooserepresentation

6) dataset: canwritexfer. The parameter indicates the new transmission semantics.

7) dcmfileformat: SaveFile. The parameter must indicate the modified transmission semantics.--"Then, the validatemetainfo function in the dcfilefo. CC file will be called (new transmission semantics must be specified in this function ).--"Call the dcmmetainfo: Search and chekmetaheadervalue functions for each element of the object metadata respectively. (In this function, the metadata element is checked to see whether it exists. If it does not exist, it is created and inserted, the new transmission semantics must be specified in the parameters.) -- "dcmelement: putstring writes the new transmission protocol to metainfo. (The Basic Call process is shown in.

 

[Email protected]

Time: 2014-08-11

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.