本文介绍了如何使用C#将普通jpeg图像字节转换为DICOM本机图像字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前,我正在编写一个用于将JPEG
转换为DICOM Image
的模块.经过分析,我已经完成了标签渲染,现在在DICOM
文件中无法正确渲染图像.
Currently I'm writing a module for the conversion of JPEG
to DICOM Image
Conversion. On analysis I've completed the tag rendering, now the image is not properly rendered in the DICOM
file.
是否存在将JPEG
转换为DICOM
的算法.
Is there any algorithm to convert the JPEG
to DICOM
.
推荐答案
继续Matthieu的回答,这是使用出色的GDCM库及其引用的示例为JPEG流创建DICOM信封的一种非常简单的方法(请注意,我有使用了一些帮助程序类,但非常简单):
Continuing from Matthieu's response, here is a very simple way of creating a DICOM envelope for a JPEG stream using the excellent GDCM library and his referenced example (note I have used some helper classes, but is quite simple):
ImageReader r = new ImageReader();
gdcm.Image image = r.GetImage();
image.SetNumberOfDimensions(2);
DataElement pixeldata = DataElementHelper.PixelData;
string file1 = @"D:\testfil.jpeg";
System.IO.FileStream infile =
new System.IO.FileStream(file1, System.IO.FileMode.Open, System.IO.FileAccess.Read);
//uint fsize = gdcm.PosixEmulation.FileSize(file1);
//byte[] jstream = new byte[fsize];
//infile.Read(jstream, 0, jstream.Length);
byte[] jstream = System.IO.File.ReadAllBytes(file1);
uint fsize = (uint) jstream.Length;
SmartPtrFrag sq = SequenceOfFragments.New();
Fragment frag = new Fragment();
frag.SetByteValue(jstream, new gdcm.VL((uint)jstream.Length));
sq.AddFragment(frag);
pixeldata.SetValue(sq.__ref__());
// insert:
image.SetDataElement(pixeldata);
PhotometricInterpretation pi = new PhotometricInterpretation(PhotometricInterpretation.PIType.MONOCHROME2);
image.SetPhotometricInterpretation(pi);
// FIXME hardcoded:
PixelFormat pixeltype = new PixelFormat(PixelFormat.ScalarType.UINT8);
image.SetPixelFormat(pixeltype);
TransferSyntax ts = new TransferSyntax(TransferSyntax.TSType.JPEGBaselineProcess1);
image.SetTransferSyntax(ts);
image.SetDimension(0, (uint)1700);
image.SetDimension(1, (uint)2200);
ImageWriter writer = new ImageWriter();
gdcm.File file = writer.GetFile();
var ds = file.GetDataSet();
DataElement patientID = DataElementHelper.PatientID;
DataElement patientName = DataElementHelper.PatientName;
DataElement accessionNumber = DataElementHelper.AccessionNumber;
DataElement studyDate = DataElementHelper.StudyDate;
DataElement studyTime = DataElementHelper.StudyTime;
DataElement studyInstanceUID = DataElementHelper.StudyInstanceUID;
DataElement seriesInstanceUID = DataElementHelper.SeriesInstanceUID;
DataElement mediaStorage = DataElementHelper.SOPClassUID;
string studyUID = new gdcm.UIDGenerator().Generate();
string seriesUID = new gdcm.UIDGenerator().Generate();
//pixelData.SetArray(b, (uint)b.Length);
DataElementHelper.SetDataElement(ref patientName, "TEST^MARCUS");
DataElementHelper.SetDataElement(ref patientID, "0000000801");
DataElementHelper.SetDataElement(ref accessionNumber, "0000000801-12345");
DataElementHelper.SetDataElement(ref studyDate, DateTime.Now.ToString("yyyyMMdd"));
DataElementHelper.SetDataElement(ref studyTime, DateTime.Now.ToString("HHmmss"));
DataElementHelper.SetDataElement(ref studyInstanceUID, studyUID);
DataElementHelper.SetDataElement(ref seriesInstanceUID, seriesUID);
DataElementHelper.SetDataElement(ref mediaStorage, "1.2.840.10008.5.1.4.1.1.7");
ds.Insert(patientID);
ds.Insert(patientName);
ds.Insert(accessionNumber);
ds.Insert(studyDate);
ds.Insert(studyTime);
ds.Insert(studyInstanceUID);
ds.Insert(seriesInstanceUID);
ds.Insert(mediaStorage);
writer.SetImage(image);
writer.SetFileName("gdcm_test.dcm");
bool ret = writer.Write();
这篇关于如何使用C#将普通jpeg图像字节转换为DICOM本机图像字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!