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:50dataset->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进行调试:
- 进入到dcmdump.exe所在文件夹
- 在地址栏输入cmd回车
- 在弹出的提示符中输入:
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 formatOFCondition status = fileformat.saveFile("test_jpeg.dcm", EXS_JPEGProcess14SV1);if (status.good())std::cout << "encode jpeg ok" << std::endl;elsestd::cout << "encode jpeg err" << std::endl;}elsestd::cout << "chooseRepresentation err" << std::endl;}DJEncoderRegistration::cleanup();system("pause");return 0;
}