分享
 
 
 

Converting between dicom image and common graphics with DCMTK and CxImage

王朝other·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

http://www.codeproject.com/useritems/DCMConverter.asp

Download souce and executable-1037kb

IntroductionThis article presents a minimum runnable toy application as a starting point to show how to convert between dicom image and common graphics(i.e. bmp, jpg, tif, etc). The two open source libraries our sample application based on are DCMTK and CxImage.

Background The DICOM standard(Digital Imaging and Communications in Medicine) is a standard created by the National Electrical Manufacturers Association (NEMA) to ease the distribution and exchange of medical images, such as CT scans, MRIs, and ultrasound. In this article we focus on the file format conversions, the file format is mainly described in Part 10 of the DICOM standard, you can download it here[^]. There is also a brief introduction to the file format available here.

DCMTK is a widely used open source implemenation of the DICOM standard, it is a collection of C/C++ libraries and applications in complete source code. To compile the sample in this article you need to download DCMTK first. If you have trouble building the downloaded DCMTK package, please refer to DCMTK for Dummies.

Another library used in this article is CxImage, it is a C++ class that can load, save, display, transform images in a very simple and fast way. It supports almost all the common graphic types, such as bmp, jpg, tiff, png, etc. In this article we expand this library to support displaying and transforming dicom images by using the dicom format encoding/decoding features provided by DCMTK. Download CxImage and follow its usage guidance to make sure it can be compiled successfully on your machine.

Using the codeWe simply derive our CxImageDCM class from the base calss CxImage. Doing so, the CxImageDCM class can load and decode the common graphics using the methods inheriting from the base class. There are three extra methods in the derived class, LoadDCM(…), SaveAsDCM(…), SaveAsJPG(…), they are used to decode, encode and convert a dicom image, respectively.

//

class CxImageDCM : public CxImage

{

public:

CxImageDCM();

virtual ~CxImageDCM();

bool LoadDCM(const TCHAR* filename);

bool SaveAsDCM(const TCHAR* filename);

bool SaveAsJPG(const TCHAR* fileName);

};//

};//

Load DCM

In the sample application, a dicom image is loaded and decoded with the classes provided by DCMTK, then it is converted to a temporary bitmap file for later manipulations.

//

bool CxImageDCM::LoadDCM(const TCHAR* filename)

{

DcmFileFormat *dfile = new DcmFileFormat();

OFCondition cond = dfile->loadFile(filename, EXS_Unknown,

EGL_withoutGL,DCM_MaxReadLength,OFFalse);

if (cond.bad()) {

AfxMessageBox(cond.text());

}

E_TransferSyntax xfer = dfile->getDataset()->getOriginalXfer();

DicomImage *di = new DicomImage(dfile, xfer,

CIF_AcrNemaCompatibility ,

0, 1);

if (di->getStatus() != EIS_Normal)

AfxMessageBox(DicomImage::getString(di->getStatus()));

di->writeBMP("c:\\from_dicom.bmp",24);

return CxImage::Load("c:\\from_dicom.bmp",CXIMAGE_FORMAT_BMP);

}//

Converting from DCM

After loading a dcm file, you can save it as a common graphic file through the encoding features provided by CxImage, or you may also use DCMTK’s encoding plugins to do the converting(however, CxImage supports more formats).

//

bool CxImageDCM::SaveAsJPG(const TCHAR* fileName)

{//you may also use DCMTK's JPG encoding plug-in

return CxImage::Save(fileName,CXIMAGE_FORMAT_JPG);

}//

Converting to DCM

To convert a common graphic file to a dcm file, you need to load the common graphic first, then set the necessary tag and copy the pixel data to the destination dcm file.

//

bool CxImageDCM::SaveAsDCM(const TCHAR* filename)

{

CxImageDCM::IncreaseBpp(24);

char uid[100];

DcmFileFormat fileformat;

DcmDataset *dataset = fileformat.getDataset();

dataset->putAndInsertString(DCM_SOPClassUID,

UID_SecondaryCaptureImageStorage);

/* ... */

// dataset->putAndInsertUint32(DCM_MetaElementGroupLength,128);

dataset->putAndInsertUint16(DCM_FileMetaInformationVersion,

0x0001);

/* ... */

dataset->putAndInsertString(DCM_UID,

UID_MultiframeTrueColorSecondaryCaptureImageStorage);

dataset->putAndInsertString(DCM_PhotometricInterpretation,

"RGB");

//add more tags here

/* ... */

BYTE* pData=new BYTE[GetHeight()*info.dwEffWidth];

BYTE* pSrc=GetBits(head.biHeight-1);

BYTE* pDst=pData;

for(long y=0; y < head.biHeight; y++){

memcpy(pDst,pSrc,info.dwEffWidth);

pSrc-=info.dwEffWidth;

pDst+=info.dwEffWidth;

}

dataset->putAndInsertUint8Array(DCM_PixelData,

pData, GetHeight()*info.dwEffWidth);

delete[] pData;

OFCondition status = fileformat.saveFile(filename,

EXS_LittleEndianImplicit,EET_UndefinedLength,EGL_withoutGL);

if (status.bad())

AfxMessageBox("Error: cannot write DICOM file ");

return true;

}//

}//

Points of InterestIn this article, the encoding feature provided by CxImage is used to convert a dicom image to a jpg file(or other formats CxImage supports). Actually, DCMTK already has a full-fledged utility called dcmj2pnm to convert a dicom image to a bmp, png, tiff or jpg image. For other formats that dcmj2pnm doesn’t support, such as gif, png, tga, pcx, wbmp, etc, you may use CxImage’s encoding features to write your own converting functions. One thing I need to clarify is that our sample application is only a toy utility to give a starting point. To write a decent dicom image converter, there are much more dicom related options need to be considered. For more information, you can refer to the implementation of dcmj2pnm(It is included in the DCMTK source code package).

From my experience, CxImage is sure easy to use, it “can load, save, display, transform images in a simple and fast way”. However, I find it’s annoying when you derive a new image encoder/decoder from the base class CxImage, the base class must know all the derived classes to give a polymophic behavior, it’s somewhat astonishing. Fortunatly, in our sample the derived class CxImageDCM only need the encoding/decoding functions it inherits from the base class, so I didn’t bother to touch the source code of CxImage.

References

DCMTK online documentation.

DCMTK forum.

CxImage on codeproject. touch the source code of CxImage.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有