admin管理员组

文章数量:1609900

背景:

        本专栏“DICOM医学图像处理”受众较窄,起初只想作为自己学习积累和工作经验的简单整理。前几天无聊浏览了一下,发现阅读量两极化严重,主要集中在“关于BMP(JPG)与DCM格式转换”和“DICOM 通讯协议”,尤其是许久前的第一篇博文DCMTK开源库的学习笔记1:将DCM文件保存成BMP文件或数据流(即数组)。因此在2014年底前打算写几篇关于DCM格式转换的文章,此次主要聚焦“如何将BMP、JPG等常规图像保存成DCM文件”,以DCMTK库为基础,给出简单的实例。

        这几篇博文采用倒叙的方式,先给出可直接运行的源码,然后重点讲解其中易犯的错误,最后是知识点补充。

利用DCMTK实现Multi-BMP存入DCM:

        源码以DCMTK为基础,思路参照DCMTK的img2dcm工具包,依赖库包含:netapi32.lib; wsock32.lib; ofstd.lib; oflog.lib; dcmimgle.lib; ijg8.lib; ijg12.lib; ijg16.lib; dcmdata.lib; dcmimage.lib; dcmjpeg.lib; dcmnet.lib; zlib.lib;libi2d.lib;(【注】:libi2d.lib库是用于导入BMP文件的)

源码如下:

 

// DcmPixelDataTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmdata/dcistrmf.h"
#include "dcmtk/dcmdata/libi2d/i2dbmps.h"
#include "DicomUtils.h"
#include <direct.h>

int _tmain(int argc, _TCHAR* argv[])
{
	OFCondition status;

	DcmFileFormat fileformat;
	DcmDataset* mydatasete=fileformat.getDataset();
	DicomUtils::AddDicomElements((DcmDataset*&)mydatasete);
	Uint16 rows,cols,samplePerPixel,bitsAlloc,bitsStored,highBit,pixelRpr,planConf,pixAspectH,pixAspectV;
	OFString photoMetrInt;
	Uint32 length;
	E_TransferSyntax ts;
	char* mydata=new char[1024*1024*10];
	memset(mydata,0,sizeof(char)*1024*1024*10);
	char* tmpData=mydata;
	char curDir[100];
	getcwd(curDir,100);
	//循环添加4张图片
	for(int i=0;i<4;++i)
	{
		OFString num;
		char numtmp[100];
		memset(numtmp,0,sizeof(char)*100);
		sprintf(numtmp,"%s\\test\\%d.bmp",curDir,i+1);
		OFString filename=OFString(numtmp);
		I2DBmpSource* bmpSource=new I2DBmpSource();
		bmpSource->setImageFile(filename);

		char* pixData=NULL;
		bmpSource->readPixelData(rows,cols,samplePerPixel,photoMetrInt,bitsAlloc,bitsStored,highBit,pixelRpr,planConf,pixAspectH,pixAspectV,pixData,length,ts);
		memcpy(tmpData,pixData,length);
		tmpData+=length;

		delete bmpSource;
	};

	mydatasete->putAndInsertUint16(DCM_SamplesPerPixel,samplePerPixel);
	mydatasete->putAndInsertString(DCM_NumberOfFrames,"4");
	mydatasete->putAndInsertUint16(DCM_Rows,rows);
	mydatasete->putAndInsertUint16(DCM_Columns,cols);
	mydatasete->putAndInsertUint16(DCM_BitsAllocated,bitsAlloc);
	mydatasete->putAndInsertUint16(DCM_BitsStored,bitsStored);
	mydatasete->putAndInsertUint16(DCM_HighBit,highBit);
	mydatasete->putAndInsertUint8Array(DCM_PixelData,(Uint8*)mydata,4*length);
	mydatasete->putAndInsertOFStringArray(DCM_PhotometricInterpretation,photoMetrInt);
	//mydatasete->putAndInsertString(DCM_PlanarConfiguration,"1");
	status=fileformat.saveFile("c:\\Multibmp2dcmtest.dcm",ts);
	if(status.bad())
	{
		std::cout<<"Error:("<<status.text()<<")\n";
	}
	return 0;
}

代码中的DicomUtils类是一个方法类,提供了一个静态方法AddDicomElement构造DICOM基本元素,代码如下:

 

本文标签: 图像处理多幅图像操作医学