Example #1
0
File: IOD.cs Project: AndrDm/uImage
        public bool IsPixelDataProcessable()
        {
            // The 'DICONDE Viewer' can only process the pixel data, if:

            // a) the IOD is a CT Slice (SOPClassName: 'Computed Tomography Image', SOPClassUID: '1.2.840.10008.5.1.4.1.1.2')
            if (!this.SOPClassUID.Equals("1.2.840.10008.5.1.4.1.1.2"))
            {
                return(false);
            }

            // b) the Transfer Syntax of the IOD does not indicate a compression of the pixel data. Supported Transfer Syntaxes are:
            //    - Explicit VR Encoding, little endian: '1.2.840.10008.1.2.1'
            //    - Implicit VR Encoding, little endian: '1.2.840.10008.1.2'
            //    - Explicit VR Encoding, big endian:    '1.2.840.10008.1.2.2'
            //
            // Compressed pixel data is not supported by the 'DICONDE Viewer'
            //
            if (!(this.TransferSyntaxUID.Equals("1.2.840.10008.1.2.1") ||
                  this.TransferSyntaxUID.Equals("1.2.840.10008.1.2") ||
                  this.TransferSyntaxUID.Equals("1.2.840.10008.1.2.2")))
            {
                return(false);
            }

            // c) the IOD must contain pixel data (DICONDE attribute (7FE0,0010) has to be present)
            if (!DICONDEParserUtility.DoesDICONDEAttributeExist(this.XDocument, "(7FE0,0010)"))
            {
                return(false);
            }

            // All above criterias are fullfilled, return true
            return(true);
        }
Example #2
0
File: IOD.cs Project: AndrDm/uImage
        public IOD(string theFileName)
        {
            DICONDEParser aDICONDEParser = new DICONDEParser(theFileName);

            this.XDocument = aDICONDEParser.GetXDocument();

            this.FileName = theFileName;

            this.StudyInstanceUID  = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0020,000D)");
            this.SeriesInstanceUID = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0020,000E)");

            this.SOPInstanceUID = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0008,0018)");

            this.SOPClassUID  = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0008,0016)");
            this.SOPClassName = SOPClassDictionary.GetSOPClassName(this.SOPClassUID);
            //DICONDE:Component
            this.ComponentName = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0010,0010)");

            this.TransferSyntaxUID = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0002,0010)");

            // The SortOrder attribute is used for sorting the CT Slices within one series.
            // For all other IOD's, the SortOrder attribute has no meaning.
            // For sorting the CT Slices, the Z-Value is used.
            // The Z-Value is tried to be determined either from
            //    - 'Image Position Component' attribute (0020,0032) or from
            //    - 'Slice Location' attribute (0020,1041)
            if (DICONDEParserUtility.DoesDICONDEAttributeExist(this.XDocument, "(0020,0032)"))              //"(0020,0032)", "Image Position (Component)"
            // 'Image Position Component' attribute will be encoded as "x\y\z"
            {
                string   aImagePositionComponent = DICONDEParserUtility.GetDICONDEAttributeAsString(this.XDocument, "(0020,0032)");
                string[] split = aImagePositionComponent.Split(new Char[] { '\\' });
                this.SortOrder = Convert.ToDouble(split[2], CultureInfo.InvariantCulture);
            }
            else
            {
                if (DICONDEParserUtility.DoesDICONDEAttributeExist(this.XDocument, "(0020,1041)"))
                {
                    this.SortOrder = DICONDEParserUtility.GetDICONDEAttributeAsDouble(this.XDocument, "(0020,1041)");
                }
            }
        }
Example #3
0
        public static void µReadDICONDEFile(µImage destination, string fileName)
        {
            string aFileName = fileName;

            var mIOD = (new IOD(aFileName));

            int mColumnCount = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0011)");          //width
            int mRowCount    = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0010)");          //height

            bool aPixelPaddingValueExist = DICONDEParserUtility.DoesDICONDEAttributeExist(mIOD.XDocument, "(0028,0120)");
            int  aPixelPaddingValue      = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0120)");

            int aPixelBitsAllocated = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0100)");

            ushort aPixelPaddingValue16;

            aPixelPaddingValue16 = 0;

            int aWindowCenter = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,1050)");
            int aWindowWidth  = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,1051)");

            var aPixelDataQuery = from Element in mIOD.XDocument.Descendants("DataElement")
                                  where Element.Attribute("Tag").Value.Equals("(7FE0,0010)")
                                  select Element;

            // Get the start position of the stream for the pixel data attribute
            long aStreamPosition = Convert.ToInt64(aPixelDataQuery.Last().Attribute("StreamPosition").Value);

            BinaryReader aBinaryReader = new BinaryReader(File.Open(aFileName, FileMode.Open, FileAccess.Read, FileShare.Read));

            // Set the stream position of the binary reader to first pixel
            aBinaryReader.BaseStream.Position = aStreamPosition;

            OpenCvSharp.Mat image = null;

            var size = new OpenCvSharp.Size();

            size.Width  = mColumnCount;
            size.Height = mRowCount;

            long length = aBinaryReader.BaseStream.Length;             //! Important to get length outside of the cycle, otherwise too sloooow

            if (16 == aPixelBitsAllocated)
            {
                image = new OpenCvSharp.Mat(size, OpenCvSharp.MatType.CV_16U);
                var mat16   = new OpenCvSharp.Mat <ushort>(image);
                var indexer = mat16.GetIndexer();

                for (int aRowIndex = 0; aRowIndex < mRowCount; aRowIndex++)
                {
                    for (int aColumnIndex = 0; aColumnIndex < mColumnCount; aColumnIndex++)
                    {
                        // For some images, the pixel buffer is smaller than '2Byte * RowCount * ColumnCount'
                        // That's why we need the check...
                        if (aBinaryReader.BaseStream.Position - 2 < length)
                        {
                            byte   aByte0      = aBinaryReader.ReadByte();
                            byte   aByte1      = aBinaryReader.ReadByte();
                            ushort aPixelValue = Convert.ToUInt16((aByte1 << 8) + aByte0);
                            // Check for Pixel Padding Value
                            if ((aPixelPaddingValueExist) && (aPixelValue == aPixelPaddingValue16))
                            {
                                aPixelValue = UInt16.MinValue;
                            }
                            indexer[aRowIndex, aColumnIndex] = aPixelValue;
                            // Rescale handling
                            //aPixelValue = aPixelValue * aRescaleSlope + aRescaleIntercept;
                            // Value of the voxel is stored in Hounsfield Units
                            //mHounsfieldPixelBuffer[aRowIndex, aColumnIndex] = aPixelValue;
                        }
                    }
                }
                OpenCvSharp.Cv2.CopyTo(mat16, destination._image);
            }
            else
            {
                image = new OpenCvSharp.Mat(size, OpenCvSharp.MatType.CV_8U);
                var mat8    = new OpenCvSharp.Mat <byte>(image);
                var indexer = mat8.GetIndexer();
                for (int aRowIndex = 0; aRowIndex < mRowCount; aRowIndex++)
                {
                    for (int aColumnIndex = 0; aColumnIndex < mColumnCount; aColumnIndex++)
                    {
                        if (aBinaryReader.BaseStream.Position - 2 < length)
                        {
                            byte aPixelValue = aBinaryReader.ReadByte();
                            if ((aPixelPaddingValueExist) && (aPixelValue == aPixelPaddingValue16))
                            {
                                aPixelValue = 0;
                            }
                            indexer[aRowIndex, aColumnIndex] = aPixelValue;
                        }
                    }
                }
                OpenCvSharp.Cv2.CopyTo(mat8, destination._image);
            }
        }