简体   繁体   English

如何使用C#将普通jpeg图像字节转换为DICOM本机图像字节

[英]How to convert the ordinary jpeg image bytes to the DICOM native image bytes using C#

Currently I'm writing a module for the conversion of JPEG to DICOM Image Conversion. 目前,我正在编写一个用于将JPEG转换为DICOM Image转换的模块。 On analysis I've completed the tag rendering, now the image is not properly rendered in the DICOM file. 经过分析,我已经完成了标签渲染,现在图像未正确显示在DICOM文件中。

Is there any algorithm to convert the JPEG to DICOM . 是否有将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): 继续Matthieu的回答,这是使用出色的GDCM库和他引用的示例为JPEG流创建DICOM信封的非常简单的方法(请注意,我已经使用了一些辅助类,但是非常简单):

       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();

please have a look at the mdcm C# DICOM library, originally written by Colby Dillion. 请查看最初由Colby Dillion编写的mdcm C#DICOM库。 He has developed managed C++ "bridges" to the IJG/LibJPEG and OpenJPEG code bases, so mdcm provides both 8/12/16-bit lossy and lossless JPEG support as well as JPEG-2000 support. 他已经开发了到IJG / LibJPEGOpenJPEG代码库的托管C ++“桥”,因此mdcm提供8/12/16位有损和无损JPEG支持以及JPEG-2000支持。

Colby's original library has WinForms dependencies. Colby的原始库具有WinForms依赖项。 I have created a Silverlight and WPF targeted fork of mdcm here . 我创建了一个Silverlight和WPF针对性MDCM叉这里 The WPF version of the library can fully utilize the same JPEG(-2000) codecs that Colby originally implemented. 该库的WPF版本可以完全利用Colby最初实现的相同JPEG(-2000)编解码器。
The Silverlight version on the other hand currently cannot benefit from these codecs. 另一方面,Silverlight版本当前无法从这些编解码器中受益。 I have made some attempts to apply the FJCore and LibJpeg.Net libraries for lossy JPEG support in Silverlight, but these libraries only support 8-bit images at the moment. 我已经尝试过将FJCoreLibJpeg.Net库应用于Silverlight中的有损JPEG支持,但是这些库目前仅支持8位图像。

Regards, 问候,
Anders @ Cureos 安德斯@Cureos

You do not need to 'convert' JPEG to DICOM. 您无需将JPEG“转换”为DICOM。 DICOM is simply an 'envelope' for your JPEG stream. DICOM只是JPEG流的“信封”。 Here is what I did in GDCM to encapsulate (= put into a DICOM envelope) an existing MPEG2 file: 这是我在GDCM中封装(=放入DICOM信封中)现有MPEG2文件的操作:

http://gdcm.sourceforge.net/html/MpegVideoInfo_8cs-example.html http://gdcm.sourceforge.net/html/MpegVideoInfo_8cs-example.html

Simply adapt that code for an input JPEG file. 只需将代码修改为输入的JPEG文件即可。 For instance have a look at: 例如看一下:

http://gdcm.sourceforge.net/html/DecompressJPEGFile_8cs-example.html http://gdcm.sourceforge.net/html/DecompressJPEGFile_8cs-example.html

No need to decompress/recompress, that would be a waste of resource. 无需解压缩/重新压缩,这将浪费资源。

There is no functionality in .NET itself for DICOM support. .NET本身没有用于DICOM支持的功能。 You will need to use a library. 您将需要使用一个库。 I do not know of any free ones but I have used LeadTools and it will do it (for a price). 我不知道有没有免费的,但我已经使用了LeadTools,它将(收费)使用它。 But I would only give it a 4/10, and I would recommenced you look for other options too. 但我只给它4/10,我建议您也寻找其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM