Beispiel #1
0
        public static DoseMatrix ResampleToImage(this DoseMatrix d1, ImageMatrix im)
        {
            var dm = new DoseMatrix();

            dm.Origin             = im.Origin;
            dm.PlanUID            = d1.PlanUID;
            dm.BytesAllocated     = d1.BytesAllocated;
            dm.DimensionX         = im.DimensionX;
            dm.DimensionY         = im.DimensionY;
            dm.DimensionZ         = im.DimensionZ;
            dm.XRes               = im.XRes;
            dm.YRes               = im.YRes;
            dm.ZRes               = im.ZRes;
            dm.PrescriptionDoseGy = d1.PrescriptionDoseGy;
            dm.DoseUnit           = d1.DoseUnit;
            var values = new float[dm.DimensionX * dm.DimensionY * dm.DimensionZ];

            var imageSlices = Enumerable.Range(0, im.DimensionZ).Select(i =>
            {
                return(new { Z = i * im.ZRes + im.Origin.Z, NSlice = i });
            }).ToList();

            foreach (var slice in imageSlices)
            {
                using (var mat = ProjectDoseToZPlaneMM(d1, im, slice.Z))
                {
                    var projected = new float[im.DimensionX * im.DimensionY];
                    mat.GetArray(out projected);

                    var indexStart = IndexHelper.LatticeXYZToIndex(0, 0, slice.NSlice, dm.DimensionX, dm.DimensionY);
                    var indexEnd   = projected.Length;
                    for (int i = 0; i < projected.Length; i++)
                    {
                        values[i + indexStart] = projected[i];
                    }
                }
            }

            dm.MaxDose = values.Max();
            var indexOfMax = values.ToList().IndexOf(dm.MaxDose);

            var(_, _, z)        = IndexHelper.IndexToLatticeXYZ(indexOfMax, dm.DimensionX, dm.DimensionY);
            dm.MaxDoseSlice     = z;
            dm.ImageOrientation = (d1.ImageOrientation.xDir.Copy(), d1.ImageOrientation.yDir.Copy(), d1.ImageOrientation.zDir.Copy());
            dm.CreateMatrix(values);
            return(dm);
        }
Beispiel #2
0
        public static DoseMatrix ParseDICOM(string dcmFile, double?prescribedDoseGy = null, bool convertToRelative = true)
        {
            var dcm = DICOMObject.Read(dcmFile);

            var matrix = new DoseMatrix();

            matrix.PrescriptionDoseGy = prescribedDoseGy;

            FillMetadata(matrix, dcm);

            //SET METADATA
            var sel = dcm.GetSelector();

            matrix.ZRes = sel.GridFrameOffsetVector.Data_[1] - sel.GridFrameOffsetVector.Data_[0];
            var offsets = sel.GridFrameOffsetVector.Data_;

            matrix.DoseUnit = sel.Dose​Units.Data == "GY" ? DoseUnit.ABSOLUTE :
                              DoseUnit.RELATIVE;
            var sumType = sel.DoseSummationType.Data;

            matrix.SumType = sumType == "PLAN" ? DoseSumType.PLAN : DoseSumType.BEAM;
            matrix.PlanUID = dcm.GetSelector().ReferencedRTPlanSequence?.Items.FirstOrDefault()?
                             .GetSelector().ReferencedSOPInstanceUID.Data;
            matrix.DimensionZ = sel.NumberOfFrames.Data;

            //FILL VOXELS
            Func <BinaryReader, float> valueConverter = null;

            switch (matrix.BytesAllocated)
            {
            case 1: valueConverter = (br) => (int)br.ReadByte(); break;

            case 2: valueConverter = (br) => br.ReadInt16(); break;

            case 4: valueConverter = (br) => br.ReadInt32(); break;

            case 8: valueConverter = (br) => br.ReadInt64(); break;
            }

            var m = dcm.GetSelector().DoseGridScaling.Data;

            if (convertToRelative && matrix.DoseUnit != DoseUnit.RELATIVE && prescribedDoseGy.HasValue)
            {
                m = m / prescribedDoseGy.Value;
                matrix.DoseUnit = DoseUnit.RELATIVE;
            }

            var values = new List <float>();

            using (BinaryReader br = new BinaryReader(dcm.GetPixelStream()))
            {
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    values.Add((float)(m * valueConverter(br)));
                }
            }

            //CALCULATE DOSE STATS
            matrix.MaxDose = values.Max();
            var indexOfMax = values.IndexOf(matrix.MaxDose);

            var(_, _, z)        = IndexHelper.IndexToLatticeXYZ(indexOfMax, matrix.DimensionX, matrix.DimensionY);
            matrix.MaxDoseSlice = z;
            matrix.CreateMatrix(values.ToArray());
            return(matrix);
        }