コード例 #1
0
        /// <summary>
        /// Handle options, if no errors.
        /// </summary>
        /// <param name="opts">Command line options</param>
        /// <returns>Exit code.</returns>
        static int RunOptionsAndReturnExitCode(Options opts)
        {
            try
            {
                RTConverters.ConvertNiftiToDicom(opts.InFile, opts.ReferenceSeries, opts.StructureNames,
                                                 opts.StructureColors, opts.FillHoles, opts.ROIInterpretedTypes, opts.OutFile, opts.ModelNameAndVersion, opts.Manufacturer, opts.Interpreter);

                Console.WriteLine($"Successfully written {opts.OutFile}");
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Error while processing: {e}");
                return(1);
            }

            return(0);
        }
コード例 #2
0
        /// <summary>
        /// Test Nifti file to DICOM-RT translation.
        /// </summary>
        /// <param name="niftiFilename">Source Nifti file.</param>
        /// <param name="referenceSeries">Reference DICOM series folder.</param>
        /// <param name="structureNames">List of structure names for DICOM-RT.</param>
        /// <param name="structureColors">List of structure colours for DICOM-RT.</param>
        /// <param name="fillHoles">List of fill hole flags for DICOM-RT.</param>
        /// <param name="roiInterpretedType">List of roiInterpretedType for DICOM-RT.</param>
        /// <param name="debugFolderName">If present, create a full set of debug images.</param>
        /// <param name="testVolumesMatch">If true, check the volumes match.</param>
        /// <param name="expectedNiftiFilename">Expect volume to match Nifti file.</param>
        public static void DoTestNiftiToDicom(
            string niftiFilename,
            string referenceSeries,
            string[] structureNames,
            RGBColorOption?[] structureColors,
            bool?[] fillHoles,
            ROIInterpretedType[] roiInterpretedTypes,
            string debugFolderName,
            bool testVolumesMatch,
            string expectedNiftiFilename)
        {
            var outputFileName = Path.GetRandomFileName() + ".dcm";

            RTConverters.ConvertNiftiToDicom(
                niftiFilename,
                referenceSeries,
                structureNames,
                structureColors,
                fillHoles,
                roiInterpretedTypes,
                outputFileName,
                "modelX:1",
                "manufacturer",
                "interpreter");

            // Open the newly created DICOM-RT file
            var dicomRTFile = DicomFile.Open(outputFileName);

            // Get the medical volume from the reference
            var acceptanceTests         = new ModerateGeometricAcceptanceTest(string.Empty, string.Empty);
            var referenceMedicalVolumes = MedIO.LoadAllDicomSeriesInFolderAsync(referenceSeries, acceptanceTests).Result;

            Assert.AreEqual(1, referenceMedicalVolumes.Count);

            var referenceMedicalVolume = referenceMedicalVolumes.First().Volume;

            var referenceIdentifiers = referenceMedicalVolume.Identifiers.First();

            // Extract the RTStruct from the DICOM-RT file
            var reloaded = RtStructReader.LoadContours(
                dicomRTFile.Dataset,
                referenceMedicalVolume.Volume.Transform.DicomToData,
                referenceIdentifiers.Series.SeriesInstanceUid,
                referenceIdentifiers.Study.StudyInstanceUid);

            Assert.IsNotNull(reloaded);

            var reloadedRTStruct = reloaded.Item1;

            Assert.AreEqual(referenceIdentifiers.Patient.Id, reloadedRTStruct.Patient.Id);
            Assert.AreEqual(referenceIdentifiers.Study.StudyInstanceUid, reloadedRTStruct.Study.StudyInstanceUid);
            Assert.AreEqual(DicomRTSeries.RtModality, reloadedRTStruct.RTSeries.Modality);

            // Load the nifti file
            var sourceVolume = MedIO.LoadNiftiAsByte(expectedNiftiFilename);

            // Split this volume from segment id to a set of mask volumes
            var labels = VolumeMetadataMapper.MultiLabelMapping(sourceVolume, structureNames.Length);

            // Make tuples of mask volume, color, names, fill holes.
            var volumesWithMetadata = VolumeMetadataMapper.MapVolumeMetadata(labels, structureNames, structureColors, fillHoles, roiInterpretedTypes).ToArray();

            Assert.AreEqual(structureNames.Length, reloadedRTStruct.Contours.Count);

            for (int i = 0; i < reloadedRTStruct.Contours.Count; i++)
            {
                var contourVolumes = new Volume3D <byte>(
                    referenceMedicalVolume.Volume.DimX,
                    referenceMedicalVolume.Volume.DimY,
                    referenceMedicalVolume.Volume.DimZ,
                    referenceMedicalVolume.Volume.SpacingX,
                    referenceMedicalVolume.Volume.SpacingY,
                    referenceMedicalVolume.Volume.SpacingZ,
                    referenceMedicalVolume.Volume.Origin,
                    referenceMedicalVolume.Volume.Direction);

                contourVolumes.Fill(reloadedRTStruct.Contours[i].Contours, (byte)1);

                var v = volumesWithMetadata[i];

                if (!string.IsNullOrWhiteSpace(debugFolderName))
                {
                    for (var z = 0; z < referenceMedicalVolume.Volume.DimZ; z++)
                    {
                        var actualFileName = Path.Combine(debugFolderName, "actual", $"slice_{v.name}_{z}.png");
                        var actualSlice    = contourVolumes.Slice(SliceType.Axial, z);
                        actualSlice.SaveBinaryMaskToPng(actualFileName);

                        var expectedFileName = Path.Combine(debugFolderName, "expected", $"slice_{v.name}_{z}.png");
                        var expectedSlice    = v.volume.Slice(SliceType.Axial, z);
                        expectedSlice.SaveBinaryMaskToPng(expectedFileName);
                    }
                }

                if (testVolumesMatch)
                {
                    VolumeAssert.AssertVolumesMatch(v.volume, contourVolumes, $"Loaded mask {i}");
                }

                Assert.AreEqual(v.name, reloadedRTStruct.Contours[i].StructureSetRoi.RoiName, $"Loaded mask name {i}");

                var expectedColor = Tuple.Create(v.color.R, v.color.G, v.color.B);
                Assert.AreEqual(expectedColor, reloadedRTStruct.Contours[i].DicomRtContour.RGBColor, $"Loaded color {i}");
            }
        }