当前位置: 首页 > article >正文

将图像的rgb数据转成DICOM医学图像格式

dcmtk官方文档:https://support.dcmtk.org/docs/
dcmtk最新源码下载:https://www.dcmtk.org/en/dcmtk/dcmtk-software-development/
dcmtk旧版本源码下载:https://dicom.offis.de/download/dcmtk/

用DCMTK库实现将图像转成dcm格式

dcmtk库的编译这里就不叙述了,网上有很多这方面的详细内容。直接上代码:

#include <iostream>
#include "opencv2/opencv.hpp"
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmimgle/dcmimage.h"
#include "dcmtk/dcmdata/libi2d/i2djpgs.h"
#include "dcmtk/dcmjpeg/djencode.h"
#include "dcmtk/dcmjpeg/djrplol.h"

using namespace std;
bool ToDicom()
{
	DcmFileFormat dicomfile;
	DcmDataset *dataset = dicomfile.getDataset();

	Uint32 all_len = 0;
	E_TransferSyntax ts = EXS_LittleEndianExplicit;

	cv::Mat mt = cv::imread("test.jpg");
	cv::cvtColor(mt, mt, cv::COLOR_BGR2RGB);

	/*	添加患者信息	*/
	dataset->putAndInsertUint16(DCM_AccessionNumber, 0);
	dataset->putAndInsertString(DCM_PatientName, "李与白", true);
	dataset->putAndInsertString(DCM_PatientID, "201210409217");
	dataset->putAndInsertString(DCM_PatientBirthDate, "19900612");
	dataset->putAndInsertString(DCM_PatientSex, "男");
	dataset->putAndInsertString(DCM_PatientAge, "32");

	dataset->putAndInsertString(DCM_InstitutionName, "四川大学华西医院");
	dataset->putAndInsertString(DCM_InstitutionalDepartmentName, "InstitutionalDepartmentName");

	/*	添加Study信息	*/
	dataset->putAndInsertString(DCM_StudyDate, "StudyDate");
	dataset->putAndInsertString(DCM_StudyTime, "StudyTime");
	dataset->putAndInsertString(DCM_StudyDescription, "StudyDescription");
	char uid[100];
	dcmGenerateUniqueIdentifier(uid, SITE_STUDY_UID_ROOT);
	dataset->putAndInsertString(DCM_StudyInstanceUID, uid);
	dataset->putAndInsertString(DCM_StudyID, "SF11240921450001");

	/*	添加Series信息	*/
	dataset->putAndInsertString(DCM_SeriesDate, "SeriesDate");
	dataset->putAndInsertString(DCM_SeriesTime, "SeriesTime");
	dataset->putAndInsertString(DCM_SeriesDescription, "SeriesDescription");
	memset(uid, 0, sizeof(char) * 100);
	dcmGenerateUniqueIdentifier(uid, SITE_SERIES_UID_ROOT);
	dataset->putAndInsertString(DCM_SeriesInstanceUID, uid);

	/*	添加Image信息	*/
	dataset->putAndInsertString(DCM_PlanarConfiguration, "0");
	dataset->putAndInsertString(DCM_ImageType, "ORIGINAL\\PRIMARY\\AXIAL");
	dataset->putAndInsertString(DCM_ContentDate, "ContentDate");
	dataset->putAndInsertString(DCM_ContentTime, "ContentTime");
	dataset->putAndInsertString(DCM_InstanceCreationDate, "20231125");//年月日
	dataset->putAndInsertString(DCM_InstanceCreationTime, "090750");//09:07:50
	dataset->putAndInsertString(DCM_AcquisitionDate, "20231126");
	dataset->putAndInsertString(DCM_AcquisitionTime, "090751");
	dataset->putAndInsertString(DCM_InstanceNumber, "1");
	dataset->putAndInsertString(DCM_PixelSpacing, "0.3\\0.3");
	dataset->putAndInsertString(DCM_WindowCenter, "600"); //源图像的窗位
	dataset->putAndInsertString(DCM_WindowWidth, "800");//源图像的窗宽
	dataset->putAndInsertString(DCM_RescaleIntercept, "0");//灰度偏移
	dataset->putAndInsertString(DCM_RescaleSlope, "1");

	dataset->putAndInsertString(DCM_NumberOfFrames, "1");
	dataset->putAndInsertUint16(DCM_SamplesPerPixel, 3);
	dataset->putAndInsertUint16(DCM_Rows, mt.rows);
	dataset->putAndInsertUint16(DCM_Columns, mt.cols);
	dataset->putAndInsertUint16(DCM_BitsAllocated, 8);
	dataset->putAndInsertUint16(DCM_BitsStored, 8);
	dataset->putAndInsertUint16(DCM_HighBit, 7);
	dataset->putAndInsertUint8Array(DCM_PixelData, (Uint8*)mt.data, mt.channels()*mt.total());
	dataset->putAndInsertOFStringArray(DCM_PhotometricInterpretation, "RGB");
	if (dataset->canWriteXfer(ts))
	{
		OFCondition status = dicomfile.saveFile("test.dcm", ts);
		if (status.good())
		{
			std::cout << "save ok" << std::endl;
			return true;
		}
		else
		{
			std::cerr << "Error saving DICOM file: " << status.text() << std::endl;
			return false;
		}
	}
	else
	{
		cout << "can not write" << endl;
		return false;
	}
}

int main()
{
	ToDicom();
	system("pause");
	return 0;
}

代码中用到了opencv图像处理库,主要是方便读取图像数据,而且可以通过opencv将其支持的任何格式转成dcm格式。

如果转换后的dcm图像打不开则可以用dcmdump.exe进行调试:

  1. 进入到dcmdump.exe所在文件夹
  2. 在地址栏输入cmd回车
  3. 在弹出的提示符中输入:

dcmdump.exe d:/test.dcm

然后回车就能看到文件信息
在这里插入图片描述

对未编码的dcm文件进行编码或改变其编码方式

int main()
{
	DJEncoderRegistration::registerCodecs(); // 注册JPEG编解码器
	DcmFileFormat fileformat;
	if (fileformat.loadFile("test.dcm").good())
	{
		DcmDataset *dataset = fileformat.getDataset();
		DcmItem *metaInfo = fileformat.getMetaInfo();

		// 创建数据集的无损JPEG版本
		if (dataset->chooseRepresentation(EXS_JPEGProcess14SV1, NULL).good() && dataset->canWriteXfer(EXS_JPEGProcess14SV1))
		{
			// store in lossless JPEG format
			OFCondition status = fileformat.saveFile("test_jpeg.dcm", EXS_JPEGProcess14SV1);
			if (status.good())
				std::cout << "encode jpeg ok" << std::endl;
			else
				std::cout << "encode jpeg err" << std::endl;
		}
		else
			std::cout << "chooseRepresentation err" << std::endl;
	}
	DJEncoderRegistration::cleanup();
	system("pause");
	return 0;
}

http://www.kler.cn/a/147806.html

相关文章:

  • PVE纵览-安装系统卡“Loading Driver”的快速解决方案
  • 数字IC后端实现之Innovus specifyCellEdgeSpacing和ICC2 set_placement_spacing_rule的应用
  • AUTOSAR_EXP_ARAComAPI的7章笔记(3)
  • Vue2:组件
  • 山泽光纤HDMI线:铜线的隐藏力量
  • HTTP协议基础
  • Git介绍和基础命令解析
  • 玩转微服务-技术篇-JSDOC教程
  • nvm安装以及解决踩坑
  • Java后端使用XWPFDocument生成word文档,踩坑
  • 【心得】XXE漏洞利用个人笔记
  • Python3.6.8升级Python3.12.0版本小记
  • Xshell远程登录AWS EC2 Linux实例
  • Linux—进程状态、僵尸进程、孤独进程、优先级
  • 【攻防世界-misc】reverseMe
  • LFM信号分析
  • 入侵redis之准备---Linux关于定时任务crontab相关知识了解配合理解shell反弹远程控制
  • 淘宝API接口系列:连接商户与消费者的桥梁
  • 【刷题笔记】分糖果||数组||暴力通过||符合思维方式||多案例分析
  • 饰品价格持续下跌,steam搬砖还有搞头吗?
  • 智能优化算法应用:基于蜻蜓算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 锐捷:下一代防火墙修改密码
  • 【Qt】QStackedWidget、QRadioButton、QPushButton及布局实现程序首页自动展示功能
  • Android中根据字符串动态获取资源文件ID
  • 食品行业研发知识管理:企业网盘的选择与优势
  • 人民币已初步具备了国际使用的网络效应/首批疏解的在京部委所属4所高校雄安校区开工建设/墨茉点心局撤出北京市场