How to convert the ordinary jpeg image bytes to th

2019-08-02 02:53发布

问题:

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.

Is there any algorithm to convert the JPEG to DICOM.

回答1:

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


回答2:

please have a look at the mdcm C# DICOM library, originally written by Colby Dillion. 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.

Colby's original library has WinForms dependencies. I have created a Silverlight and WPF targeted fork of mdcm here. The WPF version of the library can fully utilize the same JPEG(-2000) codecs that Colby originally implemented.
The Silverlight version on the other hand currently cannot benefit from these codecs. 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.

Regards,
Anders @ Cureos



回答3:

There is no functionality in .NET itself for DICOM support. 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). But I would only give it a 4/10, and I would recommenced you look for other options too.



回答4:

You do not need to 'convert' JPEG to DICOM. DICOM is simply an 'envelope' for your JPEG stream. Here is what I did in GDCM to encapsulate (= put into a DICOM envelope) an existing MPEG2 file:

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

Simply adapt that code for an input JPEG file. For instance have a look at:

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

No need to decompress/recompress, that would be a waste of resource.