public void Display(DicomDirectoryRecord record)
        {
            switch (record.DirectoryRecordType)
            {
            case "PATIENT":
                ShowPatientLevelInfo(record);
                break;

            case "STUDY":
                ShowStudyLevelInfo(record);
                break;

            case "SERIES":
                ShowSeriesLevelInfo(record);
                break;

            case "IMAGE":
                ShowImageLevelInfo(record);
                break;

            default:
                _log($"Unknown directory record type: {record.DirectoryRecordType}. " +
                     $"Please check your inputs");
                break;
            }
            ;
        }
        public void Exact_Offsets_Should_All_Match()
        {
            var offsets = GenerateUniqueRandonmOffsets(155, 500000);
            var root    = new Container()
            {
                Sequence = new DicomDataset()
            };

            root.CreateChildren(offsets, 5, 3, 3, 2);

            var allChildren = root.AllContainer.Skip(1).ToArray();
            var sequence    = new DicomSequence(DicomTag.DirectoryRecordSequence);

            root.Sequence.AddOrUpdate(sequence);

            var byteSource = new TestByteSource();
            var observer   = new DicomDirectoryReaderObserver(root.Sequence);

            observer.OnBeginSequence(byteSource, DicomTag.DirectoryRecordSequence, 0);
            foreach (var container in allChildren)
            {
                sequence.Items.Add(container.Sequence);
                byteSource.Position = container.Offset + 8;
                observer.OnBeginSequenceItem(byteSource, 0);
                observer.OnEndSequenceItem();
            }
            observer.OnEndSequence();

            var rootRecord = new DicomDirectoryRecord
            {
                LowerLevelDirectoryRecord = observer.BuildDirectoryRecords(),
            };

            root.AssertRecord(rootRecord);
        }
Пример #3
0
    public byte[] DICOMSeriesToVolume(DicomDirectoryRecord series, out Int3 size)
    {
        size = GetSizeForRecord(series);
        var voxels = new VolumeBuffer <Color32>(size);

        var tex = new Texture2D(2, 2);

        var instanceNumbers = series.LowerLevelDirectoryRecordCollection.Select(x => x.Get <int>(DicomTag.InstanceNumber)).OrderBy(x => x).ToArray();

        for (var z = 0; z < instanceNumbers.Length; z++)
        {
            var frame = instanceNumbers[z];
            tex = loadDicomInstance.GetTexture2DForRecord(series, frame);
            var fromPixels = tex.GetPixels32();
            for (var y = 0; y < size.y; y++)
            {
                for (var x = 0; x < size.x; x++)
                {
                    var from = fromPixels[x * resolution + ((size.y - 1 - y) * size.x) * resolution * 2];
                    voxels.SetVoxel(new Int3(x, y, z), from);
                }
            }
        }

        voxels.ClearEdges(new Color32(0, 0, 0, 0));
        return(VolumeTextureUtils.Color32ArrayToByteArray(voxels.DataArray));
    }
Пример #4
0
        private static ImageRecord ToImage(DicomDirectoryRecord record)
        {
            ImageRecord image = new ImageRecord();

            image.SOPInstanceUID   = record.GetSingleValue <string>(DicomTag.ReferencedSOPInstanceUIDInFile);
            image.ImageNumber      = record.GetSingleValueOrDefault <string>(DicomTag.InstanceNumber, null);
            image.ReferencedFileID = record.GetValueOrDefault <string>(DicomTag.ReferencedFileID, 0, null);

            return(image);
        }
Пример #5
0
 static DicomDirectoryRecord GetSeries(DicomDirectoryRecord record)
 {
     if (record.DirectoryRecordType == "IMAGE")
     {
         return(record);
     }
     while (record.DirectoryRecordType != "SERIES")
     {
         record = record.LowerLevelDirectoryRecord;
     }
     return(record);
 }
        public void Duplicate_Offsets_Should_Not_Throw()
        {
            var offsets = new List <uint>(GenerateUniqueRandonmOffsets(155, 100000).Select(o => o * 5));

            for (int i = 0; i < offsets.Count - 2; i += 2)
            {
                offsets[i + 1] = offsets[i];
            }

            var root = new Container()
            {
                Sequence = new DicomDataset()
            };

            root.CreateChildren(offsets, 5, 3, 3, 2);

            var allChildren = root.AllContainer.Skip(1).ToArray();
            var sequence    = new DicomSequence(DicomTag.DirectoryRecordSequence);

            root.Sequence.AddOrUpdate(sequence);

            var byteSource = new TestByteSource();
            var observer   = new DicomDirectoryReaderObserver(root.Sequence);

            observer.OnBeginSequence(byteSource, DicomTag.DirectoryRecordSequence, 0);
            uint lastPosition = 0;

            foreach (var container in allChildren)
            {
                sequence.Items.Add(container.Sequence);
                var position = container.Offset + 8;
                if (position == lastPosition)
                {
                    position += 1;
                }

                lastPosition = position;

                byteSource.Position = position;
                observer.OnBeginSequenceItem(byteSource, 0);
                observer.OnEndSequenceItem();
            }
            observer.OnEndSequence();

            var rootRecord = new DicomDirectoryRecord
            {
                LowerLevelDirectoryRecord = observer.BuildDirectoryRecords(),
            };

            // Cannot assert all records when duplicate offsets exist.
            // root.AssertRecord(rootRecord);
        }
Пример #7
0
    public Texture2D GetTexture2DForRecord(DicomDirectoryRecord record, int frame = -1)
    {
        var tex = new Texture2D(1, 1);

        try
        {
            var img = GetImageForRecord(record, frame);
            //var s = Time.realtimeSinceStartup;
            tex = DicomToTex2D(img);
            //Debug.Log("DicomToText2D took " + (Time.realtimeSinceStartup - s));
        }
        catch
        {
        }
        return(tex);
    }
        public void AssertRecord(DicomDirectoryRecord record)
        {
            if (Id != 0)
            {
                Assert.Equal(Id.ToString(), record.GetSingleValue <string>(DicomTag.SOPInstanceUID));
            }

            DicomDirectoryRecord current = record.LowerLevelDirectoryRecord;

            foreach (var child in Children)
            {
                Assert.NotNull(current);
                child.AssertRecord(current);
                current = current.NextDirectoryRecord;
            }
            Assert.Null(current);
        }
Пример #9
0
    public DicomImage GetImageForRecord(DicomDirectoryRecord record, int frame = -1)
    {
        var directory        = rootDirectoryMap[record];
        var series           = GetSeries(record);
        var absoluteFilename = "";
        var instanceNumbers  = series.LowerLevelDirectoryRecordCollection.Select(x => x.Get <int>(DicomTag.InstanceNumber)).OrderBy(x => x).ToArray();

        if (frame == 0)
        {
            frame = instanceNumbers[0];
        }
        if (frame == -1)         // get thumbnail - get midpoint image
        {
            frame = instanceNumbers[instanceNumbers.Length / 2];
        }
        if (frame == -2)         // get last
        {
            frame = instanceNumbers[instanceNumbers.Length - 1];
        }
        try
        {
            var imageRecord = series.LowerLevelDirectoryRecordCollection.First(x => x.Get <int>(DicomTag.InstanceNumber) == frame);
            var filename    = Path.Combine(imageRecord.Get <string[]>(DicomTag.ReferencedFileID));
            absoluteFilename = Path.Combine(directory, filename);
            //Debug.Log("load image " + absoluteFilename);
            var img = new DicomImage(absoluteFilename);
            return(img);
        }
        catch (InvalidOperationException)
        {
            Debug.LogError("series does not contain an image of InstanceNumber=" + frame + "- valid instance numbers = " + string.Join(",", instanceNumbers));
            return(null);
        }
        catch (System.Exception e)
        {
            Debug.LogError("Failed to load " + absoluteFilename + ":" + e);
            return(null);
        }
    }
Пример #10
0
        string GetDicomName(DicomDirectoryRecord r, string infoParent, int count = -1)
        {
            string name = "";

            try
            {
                if (infoParent is null || infoParent.Length == 0)
                {
                    name = r.Get <string>(DicomTag.PatientName, -1);
                }
                else if (infoParent.StartsWith("dicom:patient"))
                {
                    try
                    {
                        name = r.Get <string>(DicomTag.StudyDescription, -1);
                    }
                    catch
                    {
                    }
                    name += " - " + r.Get <string>(DicomTag.StudyDate, -1) + " " + r.Get <string>(DicomTag.StudyTime, -1);
                }
                else if (infoParent.StartsWith("dicom:study"))
                {
                    try
                    {
                        name = r.Get <string>(DicomTag.SeriesNumber, -1);
                    }
                    catch
                    {
                    }
                    name += " - " + r.Get <string>(DicomTag.SeriesDate, -1) + " " + r.Get <string>(DicomTag.SeriesTime, -1);
                }
                else if (infoParent.StartsWith("dicom:series"))
                {
                    name = Path.GetFileName(r.Get <string>(DicomTag.ReferencedFileID, -1));
                }
            }
Пример #11
0
 public string GetDicomTag(DicomDirectoryRecord record, DicomTag tag)
 {
     return(string.Join(",", record.Get <string[]>(tag, new string[] { "" })));
 }
Пример #12
0
    public Int3 GetSizeForRecord(DicomDirectoryRecord series)
    {
        var img = loadDicomInstance.GetImageForRecord(series);

        return(new Int3(img.Width / resolution, img.Height / resolution, series.LowerLevelDirectoryRecordCollection.Count()));
    }