public static IPresentationImage PresentationImageFromPositionOrientation(
            double[] imagePositionPatientValues,
            double[] imageOrientationPatientValues,
            IDisplaySet displaySet,
            string frameOfReferenceUid)
        {
            Platform.CheckForNullReference(displaySet, "displaySet");

            if (imagePositionPatientValues != null && imagePositionPatientValues.Length > 2 &&
                imageOrientationPatientValues != null && imageOrientationPatientValues.Length > 5)
            {
                var imagePositionPatient = new ImagePositionPatient(
                    imagePositionPatientValues[0],
                    imagePositionPatientValues[1],
                    imagePositionPatientValues[2]);
                var imageOrientationPatient = new ImageOrientationPatient(
                    imageOrientationPatientValues[0],
                    imageOrientationPatientValues[1],
                    imageOrientationPatientValues[2],
                    imageOrientationPatientValues[3],
                    imageOrientationPatientValues[4],
                    imageOrientationPatientValues[5]);

                return(PresentationImageFromPositionOrientation(
                           imagePositionPatient,
                           imageOrientationPatient,
                           displaySet,
                           frameOfReferenceUid));
            }

            return(null);
        }
        private static void AssertVolumeSlice(VolumeSlice actualSlice,
                                              int expectedRows, int expectedColumns,
                                              Vector3D expectedPositionPatient,
                                              Vector3D expectedRowOrientationPatient, Vector3D expectedColumnOrientationPatient,
                                              float expectedRowSpacing, float expectedColumnSpacing,
                                              float expectedSliceThickness, float?expectedSpacingBetweenSlices,
                                              string message = null, params object[] args)
        {
            const float tolerance = 1e-5f;
            var         msg       = !string.IsNullOrEmpty(message) ? "{0} (" + string.Format(message, args) + ")" : "{0}";

            Assert.AreEqual(expectedRows, actualSlice.Rows, msg, "Rows");
            Assert.AreEqual(expectedColumns, actualSlice.Columns, msg, "Columns");

            var imagePositionPatient = ImagePositionPatient.FromString(actualSlice.ImagePositionPatient);

            Assert.IsNotNull(imagePositionPatient, "ImagePositionPatient");

            var actualPositionPatient = new Vector3D((float)imagePositionPatient.X, (float)imagePositionPatient.Y, (float)imagePositionPatient.Z);

            if (!Vector3D.AreEqual(expectedPositionPatient, actualPositionPatient, tolerance))
            {
                Assert.AreEqual(expectedPositionPatient, actualPositionPatient, msg, "ImagePositionPatient");
            }

            var imageOrientationPatient = ImageOrientationPatient.FromString(actualSlice.ImageOrientationPatient);

            Assert.IsNotNull(imageOrientationPatient, "ImageOrientationPatient");

            var actualRowOrientationPatient    = new Vector3D((float)imageOrientationPatient.RowX, (float)imageOrientationPatient.RowY, (float)imageOrientationPatient.RowZ);
            var actualColumnOrientationPatient = new Vector3D((float)imageOrientationPatient.ColumnX, (float)imageOrientationPatient.ColumnY, (float)imageOrientationPatient.ColumnZ);

            if (!Vector3D.AreEqual(expectedRowOrientationPatient, actualRowOrientationPatient, tolerance))
            {
                Assert.AreEqual(expectedRowOrientationPatient, actualRowOrientationPatient, msg, "ImageOrientationPatient.Row");
            }
            if (!Vector3D.AreEqual(expectedColumnOrientationPatient, actualColumnOrientationPatient, tolerance))
            {
                Assert.AreEqual(expectedColumnOrientationPatient, actualColumnOrientationPatient, msg, "ImageOrientationPatient.Column");
            }

            var actualPixelSpacing = PixelSpacing.FromString(actualSlice.PixelSpacing);

            Assert.IsNotNull(actualPixelSpacing, "PixelSpacing");
            Assert.AreEqual(expectedRowSpacing, actualPixelSpacing.Row, tolerance, msg, "PixelSpacing.Row");
            Assert.AreEqual(expectedColumnSpacing, actualPixelSpacing.Column, tolerance, msg, "PixelSpacing.Column");

            Assert.AreEqual(expectedSliceThickness, float.Parse(actualSlice.SliceThickness), tolerance, msg, "SliceThickness");

            if (expectedSpacingBetweenSlices.HasValue)
            {
                Assert.AreEqual(expectedSpacingBetweenSlices.Value, float.Parse(actualSlice.SpacingBetweenSlices), tolerance, msg, "SpacingBetweenSlices");
            }
            else
            {
                Assert.IsEmpty(actualSlice.SpacingBetweenSlices, msg, "SpacingBetweenSlices");
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing)
        {
            Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient");
            Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient");
            Platform.CheckForNullReference(pixelSpacing, "pixelSpacing");

            PixelSpacing               = pixelSpacing;
            ImageOrientationPatient    = imageOrientationPatient;
            ImagePositionPatient       = imagePositionPatient;
            ImagePositionPatientVector = new Vector3D((float)ImagePositionPatient.X, (float)ImagePositionPatient.Y, (float)ImagePositionPatient.Z);
        }
Esempio n. 4
0
        /// <summary>
        /// Constructor.
        /// </summary>
	    public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing)
		{
            Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient");
            Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient");
            Platform.CheckForNullReference(pixelSpacing, "pixelSpacing");

            PixelSpacing = pixelSpacing;
		    ImageOrientationPatient = imageOrientationPatient;
	        ImagePositionPatient = imagePositionPatient;
            ImagePositionPatientVector = new Vector3D((float)ImagePositionPatient.X, (float)ImagePositionPatient.Y, (float)ImagePositionPatient.Z);
		}
Esempio n. 5
0
		/// <summary>
		/// Initializes a new instance of <see cref="ImagePlaneHelper"/> from the specified image plane details.
		/// </summary>
		public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing, int rows, int columns)
		{
			Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient");
			Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient");
			Platform.CheckForNullReference(pixelSpacing, "pixelSpacing");
			Platform.CheckPositive(rows, "rows");
			Platform.CheckPositive(columns, "columns");

			_rows = rows;
			_columns = columns;
			_pixelSpacing = pixelSpacing;
			_imageOrientationPatient = imageOrientationPatient;
			_imagePositionPatient = imagePositionPatient;
		}
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of <see cref="ImagePlaneHelper"/> from the specified image plane details.
        /// </summary>
        public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing, int rows, int columns)
        {
            Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient");
            Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient");
            Platform.CheckForNullReference(pixelSpacing, "pixelSpacing");
            Platform.CheckPositive(rows, "rows");
            Platform.CheckPositive(columns, "columns");

            _rows                    = rows;
            _columns                 = columns;
            _pixelSpacing            = pixelSpacing;
            _imageOrientationPatient = imageOrientationPatient;
            _imagePositionPatient    = imagePositionPatient;
        }
Esempio n. 7
0
        public Vector3D ConvertToPatient(PointF positionPixels, int z)
        {
            ImagePositionPatient pos = this._sourceFrame.ImagePositionPatient;
            Vector3D             _imagePositionPatient = new Vector3D((float)pos.X, (float)pos.Y, (float)pos.Z);

            ImageOrientationPatient orientation = this._sourceFrame.ImageOrientationPatient;

            Vector3D left   = new Vector3D((float)orientation.RowX, (float)orientation.RowY, (float)orientation.RowZ);
            Vector3D normal = left.Cross(new Vector3D((float)orientation.ColumnX, (float)orientation.ColumnY, (float)orientation.ColumnZ));


            PixelSpacing pixelSpacing = this._sourceFrame.PixelSpacing;

            if (orientation.IsNull || pixelSpacing.IsNull)
            {
                return(null);
            }

            Vector3D position = _imagePositionPatient;

            if (positionPixels.X == 0F && positionPixels.Y == 0F)
            {
                return(position);
            }

            Matrix _pixelToPatientTransform = new Matrix(4, 4);

            _pixelToPatientTransform.SetColumn(0, (float)(orientation.RowX * pixelSpacing.Column),
                                               (float)(orientation.RowY * pixelSpacing.Column),
                                               (float)(orientation.RowZ * pixelSpacing.Column), 0);

            _pixelToPatientTransform.SetColumn(1, (float)(orientation.ColumnX * pixelSpacing.Row),
                                               (float)(orientation.ColumnY * pixelSpacing.Row),
                                               (float)(orientation.ColumnZ * pixelSpacing.Row), 0);

            _pixelToPatientTransform.SetColumn(2, (float)(normal.X * pixelSpacing.Row),
                                               (float)(normal.Y * pixelSpacing.Row),
                                               (float)(normal.Z * pixelSpacing.Row), 0);

            _pixelToPatientTransform.SetColumn(3, position.X, position.Y, position.Z, 1F);


            Matrix columnMatrix = new Matrix(4, 1);

            columnMatrix.SetColumn(0, positionPixels.X, positionPixels.Y, z, 1F);
            Matrix result = _pixelToPatientTransform * columnMatrix;

            return(new Vector3D(result[0, 0], result[1, 0], result[2, 0]));
        }
        public static IPresentationImage PresentationImageFromPositionOrientation(
            ImagePositionPatient imagePositionPatient,
            ImageOrientationPatient imageOrientationPatient,
            IDisplaySet displaySet,
            string frameOfReferenceUid)
        {
            var point = new Vector3D(
                (float)imagePositionPatient.X,
                (float)imagePositionPatient.Y,
                (float)imagePositionPatient.Z);

            if (displaySet != null)
            {
                var firstSop = displaySet.PresentationImages.OfType <IImageSopProvider>().FirstOrDefault();
                // Match Frame of Reference UID, if present
                if (firstSop == null || string.IsNullOrEmpty(frameOfReferenceUid) ||
                    string.IsNullOrEmpty(firstSop.Frame.FrameOfReferenceUid) || frameOfReferenceUid == firstSop.Frame.FrameOfReferenceUid)
                {
                    foreach (IPresentationImage image in displaySet.PresentationImages)
                    {
                        var sop = image as IImageSopProvider;
                        if (sop != null)
                        {
                            Vector3D planeRow = new Vector3D(
                                (float)sop.Frame.ImageOrientationPatient.RowX,
                                (float)sop.Frame.ImageOrientationPatient.RowY,
                                (float)sop.Frame.ImageOrientationPatient.RowZ).Normalize();
                            Vector3D planeColumn = new Vector3D(
                                (float)sop.Frame.ImageOrientationPatient.ColumnX,
                                (float)sop.Frame.ImageOrientationPatient.ColumnY,
                                (float)sop.Frame.ImageOrientationPatient.ColumnZ).Normalize();
                            Vector3D planeNormal = planeRow.Cross(planeColumn);

                            var planePosition = new Vector3D(
                                (float)sop.Frame.ImagePositionPatient.X,
                                (float)sop.Frame.ImagePositionPatient.Y,
                                (float)sop.Frame.ImagePositionPatient.Z);
                            if (PointIsInPlane(point, planeNormal, planePosition))
                            {
                                return(image);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 9
0
        /// <summary>
        /// Initializes a new instance of <see cref="ImagePlaneHelper"/> from the specified image plane details.
        /// </summary>
        public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing, int rows, int columns)
        {
            Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient");
            Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient");
            Platform.CheckForNullReference(pixelSpacing, "pixelSpacing");
            try
            {
                Platform.CheckPositive(rows, "rows");
                Platform.CheckPositive(columns, "columns");
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, "the row is " + rows.ToString());
            }

            _rows                    = rows;
            _columns                 = columns;
            _pixelSpacing            = pixelSpacing;
            _imageOrientationPatient = imageOrientationPatient;
            _imagePositionPatient    = imagePositionPatient;
        }
        private static void InitializePlanePositionPatientFunctionalGroup(FunctionalGroupsSequenceItem functionalGroupsSequenceItem,
																		  ImagePositionPatient imagePositionPatient)
        {
            if (imagePositionPatient.IsNull)
                return;

            var planePositionPatientFunctionalGroup = functionalGroupsSequenceItem.GetFunctionalGroup<PlanePositionPatientFunctionalGroup>();
            var planePositionSequence = planePositionPatientFunctionalGroup.CreatePlanePositionSequence();
            planePositionSequence.ImagePositionPatient = new[] { imagePositionPatient.X, imagePositionPatient.Y, imagePositionPatient.Z };

            planePositionPatientFunctionalGroup.PlanePositionSequence = planePositionSequence;
        }
        public static void CreateSeriesGraphicsForSeg(IPresentationImage presentationImage, Seg seg,
                                                      SegmentationDocument segmentationDocument,
                                                      IDicomMessageSopDataSource dicomMessageSopDataSourceSop)
        {
            Platform.CheckForNullReference(presentationImage, "presentationImage");
            Platform.CheckForNullReference(seg, "seg");
            Platform.CheckForNullReference(segmentationDocument, "segmentationDocument");

            SegmentImageData segImageData = seg.SegmentImageData;

            if (segImageData == null)
            {
                Platform.Log(LogLevel.Error,
                             "Cannot create segmentation graphics when no segmentation imaging data is provided");
                return;
            }

            var imageSopProvider = presentationImage as IImageSopProvider;

            if (imageSopProvider == null)
            {
                Platform.Log(LogLevel.Error,
                             "Failed to populate SegFrameGraphics collection. Image is not an ImageSopProvider");
                return;
            }

            DicomPixelData segmentationPixelData = DicomPixelData.CreateFrom(dicomMessageSopDataSourceSop.SourceMessage);
            var            rawPixelData          =
                (byte[])dicomMessageSopDataSourceSop.SourceMessage.DataSet.GetAttribute(DicomTags.PixelData).Values;
            var pixelDataGetter = new Func <int, byte[]>(frameIndex =>
            {
                if (segImageData.BitsStored == 1)
                {
                    // Do unpacking
                    int frameLength = segImageData.Rows *
                                      segImageData.Columns;
                    var overlayData =
                        new OverlayData(frameIndex * frameLength,
                                        segImageData.Rows,
                                        segImageData.Columns, false,
                                        rawPixelData);
                    return(overlayData.Unpack());
                }
                if (segImageData.BitsStored == 8)
                {
                    return(segmentationPixelData.GetFrame(frameIndex));
                }
                throw new InvalidDataException(
                    "Segmentation objects need to have BitsStored as either 1 or 8");
            });

            // NOTE: SegmentFrameData was already sorted
            foreach (SegmentFrameData segmentFrameData in segImageData.SegmentFrameData)
            {
                IPresentationImage segPresentationImage = null;

                // Get the presentation image if we have an image reference
                string referencedSopInstanceUid   = segmentFrameData.ReferencedSopInstanceUid;
                int    referencedImageFrameNumber = segmentFrameData.ReferencedFrameNumber ?? 1;
                if (!string.IsNullOrEmpty(referencedSopInstanceUid))
                {
                    segPresentationImage =
                        presentationImage.ParentDisplaySet.PresentationImages.OfType <IImageSopProvider>()
                        .FirstOrDefault(curImageSopProvider => curImageSopProvider != null &&
                                        curImageSopProvider.ImageSop.SopInstanceUid ==
                                        referencedSopInstanceUid &&
                                        curImageSopProvider.Frame.FrameNumber ==
                                        referencedImageFrameNumber) as IPresentationImage;
                }

                // Location defaults to 0, 0 unless determined otherwise from image position and image orientation
                var segLocation = new PointF();

                // Get the presentation image from the image position and orientation
                if (segmentFrameData.ImagePositionPatient != null &&
                    segmentFrameData.ImagePositionPatient.Count() > 2 &&
                    segmentFrameData.ImageOrientationPatient != null &&
                    segmentFrameData.ImageOrientationPatient.Count() > 5)
                {
                    var imagePositionPatient = new ImagePositionPatient(
                        segmentFrameData.ImagePositionPatient[0],
                        segmentFrameData.ImagePositionPatient[1],
                        segmentFrameData.ImagePositionPatient[2]);
                    var imageOrientationPatient = new ImageOrientationPatient(
                        segmentFrameData.ImageOrientationPatient[0],
                        segmentFrameData.ImageOrientationPatient[1],
                        segmentFrameData.ImageOrientationPatient[2],
                        segmentFrameData.ImageOrientationPatient[3],
                        segmentFrameData.ImageOrientationPatient[4],
                        segmentFrameData.ImageOrientationPatient[5]);
                    IDisplaySet displaySet = presentationImage.ParentDisplaySet;

                    if (segPresentationImage == null)
                    {
                        segPresentationImage = PresentationImageFromPositionOrientation(
                            imagePositionPatient,
                            imageOrientationPatient,
                            displaySet,
                            segImageData.FrameOfReferenceUid);
                    }

                    var imageSop = segPresentationImage as IImageSopProvider;
                    if (imageSop != null)
                    {
                        Vector3D segImageLocation =
                            imageSop.Frame.ImagePlaneHelper.ConvertToImagePlane(new Vector3D(
                                                                                    (float)imagePositionPatient.X,
                                                                                    (float)imagePositionPatient.Y,
                                                                                    (float)imagePositionPatient.Z));
                        PointF?segPixelLocation =
                            imageSop.Frame.ImagePlaneHelper.ConvertToImage(
                                new PointF(segImageLocation.X, segImageLocation.Y));
                        if (segPixelLocation.HasValue)
                        {
                            segLocation = segPixelLocation.Value;
                        }
                    }
                }

                if (segPresentationImage != null)
                {
                    SegFrameImageGraphic newGraphic = AddSegFrameImageGraphicToPresentationImage(
                        segPresentationImage,
                        segImageData.Rows,
                        segImageData.Columns,
                        segLocation.X,
                        segLocation.Y,
                        seg.Color,
                        referencedImageFrameNumber,
                        seg.Label,
                        seg.Description,
                        pixelDataGetter.Invoke(segmentFrameData.FrameNumber - 1),
                        new SegmentationDocumentReference(segmentationDocument, seg.SegmentationNumber));
                    if (newGraphic != null &&
                        segPresentationImage == segPresentationImage.ParentDisplaySet.ImageBox.TopLeftPresentationImage)
                    {
                        newGraphic.Draw();
                    }
                }
                else
                {
                    Platform.Log(LogLevel.Error, "Failed to find Presentation Image to display a segment on");
                }
            }
        }
Esempio n. 12
0
			private static Vector3D ImagePositionPatientToVector(ImagePositionPatient position)
			{
				return new Vector3D((float) position.X, (float) position.Y, (float) position.Z);
			}
        public static IPresentationImage PresentationImageFromPositionOrientation(
            double[] imagePositionPatientValues,
            double[] imageOrientationPatientValues,
            IDisplaySet displaySet,
            string frameOfReferenceUid)
        {
            Platform.CheckForNullReference(displaySet, "displaySet");

            if (imagePositionPatientValues != null && imagePositionPatientValues.Length > 2 &&
                imageOrientationPatientValues != null && imageOrientationPatientValues.Length > 5)
            {
                var imagePositionPatient = new ImagePositionPatient(
                    imagePositionPatientValues[0],
                    imagePositionPatientValues[1],
                    imagePositionPatientValues[2]);
                var imageOrientationPatient = new ImageOrientationPatient(
                    imageOrientationPatientValues[0],
                    imageOrientationPatientValues[1],
                    imageOrientationPatientValues[2],
                    imageOrientationPatientValues[3],
                    imageOrientationPatientValues[4],
                    imageOrientationPatientValues[5]);

                return PresentationImageFromPositionOrientation(
                    imagePositionPatient,
                    imageOrientationPatient,
                    displaySet,
                    frameOfReferenceUid);
            }

            return null;
        }
        public static IPresentationImage PresentationImageFromPositionOrientation(
            ImagePositionPatient imagePositionPatient,
            ImageOrientationPatient imageOrientationPatient,
            IDisplaySet displaySet,
            string frameOfReferenceUid)
        {
            var point = new Vector3D(
                (float) imagePositionPatient.X,
                (float) imagePositionPatient.Y,
                (float) imagePositionPatient.Z);

            if (displaySet != null)
            {
                var firstSop = displaySet.PresentationImages.OfType<IImageSopProvider>().FirstOrDefault();
                // Match Frame of Reference UID, if present
                if (firstSop == null || string.IsNullOrEmpty(frameOfReferenceUid) ||
                    string.IsNullOrEmpty(firstSop.Frame.FrameOfReferenceUid) || frameOfReferenceUid == firstSop.Frame.FrameOfReferenceUid)
                {
                    foreach (IPresentationImage image in displaySet.PresentationImages)
                    {
                        var sop = image as IImageSopProvider;
                        if (sop != null)
                        {
                            Vector3D planeRow = new Vector3D(
                                (float) sop.Frame.ImageOrientationPatient.RowX,
                                (float) sop.Frame.ImageOrientationPatient.RowY,
                                (float) sop.Frame.ImageOrientationPatient.RowZ).Normalize();
                            Vector3D planeColumn = new Vector3D(
                                (float) sop.Frame.ImageOrientationPatient.ColumnX,
                                (float) sop.Frame.ImageOrientationPatient.ColumnY,
                                (float) sop.Frame.ImageOrientationPatient.ColumnZ).Normalize();
                            Vector3D planeNormal = planeRow.Cross(planeColumn);

                            var planePosition = new Vector3D(
                                (float) sop.Frame.ImagePositionPatient.X,
                                (float) sop.Frame.ImagePositionPatient.Y,
                                (float) sop.Frame.ImagePositionPatient.Z);
                            if (PointIsInPlane(point, planeNormal, planePosition))
                                return image;
                        }
                    }
                }
            }

            return null;
        }
        public static void CreateSeriesGraphicsForSeg(IPresentationImage presentationImage, Seg seg,
                                                      SegmentationDocument segmentationDocument,
                                                      IDicomMessageSopDataSource dicomMessageSopDataSourceSop)
        {
            Platform.CheckForNullReference(presentationImage, "presentationImage");
            Platform.CheckForNullReference(seg, "seg");
            Platform.CheckForNullReference(segmentationDocument, "segmentationDocument");

            SegmentImageData segImageData = seg.SegmentImageData;
            if (segImageData == null)
            {
                Platform.Log(LogLevel.Error,
                             "Cannot create segmentation graphics when no segmentation imaging data is provided");
                return;
            }

            var imageSopProvider = presentationImage as IImageSopProvider;
            if (imageSopProvider == null)
            {
                Platform.Log(LogLevel.Error,
                             "Failed to populate SegFrameGraphics collection. Image is not an ImageSopProvider");
                return;
            }

            DicomPixelData segmentationPixelData = DicomPixelData.CreateFrom(dicomMessageSopDataSourceSop.SourceMessage);
            var rawPixelData =
                (byte[]) dicomMessageSopDataSourceSop.SourceMessage.DataSet.GetAttribute(DicomTags.PixelData).Values;
            var pixelDataGetter = new Func<int, byte[]>(frameIndex =>
                                                            {
                                                                if (segImageData.BitsStored == 1)
                                                                {
                                                                    // Do unpacking
                                                                    int frameLength = segImageData.Rows*
                                                                                      segImageData.Columns;
                                                                    var overlayData =
                                                                        new OverlayData(frameIndex*frameLength,
                                                                                        segImageData.Rows,
                                                                                        segImageData.Columns, false,
                                                                                        rawPixelData);
                                                                    return overlayData.Unpack();
                                                                }
                                                                if (segImageData.BitsStored == 8)
                                                                {
                                                                    return segmentationPixelData.GetFrame(frameIndex);
                                                                }
                                                                throw new InvalidDataException(
                                                                    "Segmentation objects need to have BitsStored as either 1 or 8");
                                                            });

            // NOTE: SegmentFrameData was already sorted
            foreach (SegmentFrameData segmentFrameData in segImageData.SegmentFrameData)
            {
                IPresentationImage segPresentationImage = null;

                // Get the presentation image if we have an image reference
                string referencedSopInstanceUid = segmentFrameData.ReferencedSopInstanceUid;
                int referencedImageFrameNumber = segmentFrameData.ReferencedFrameNumber ?? 1;
                if (!string.IsNullOrEmpty(referencedSopInstanceUid))
                {
                    segPresentationImage =
                        presentationImage.ParentDisplaySet.PresentationImages.OfType<IImageSopProvider>()
                            .FirstOrDefault(curImageSopProvider => curImageSopProvider != null &&
                                                                   curImageSopProvider.ImageSop.SopInstanceUid ==
                                                                   referencedSopInstanceUid &&
                                                                   curImageSopProvider.Frame.FrameNumber ==
                                                                   referencedImageFrameNumber) as IPresentationImage;
                }

                // Location defaults to 0, 0 unless determined otherwise from image position and image orientation
                var segLocation = new PointF();

                // Get the presentation image from the image position and orientation
                if (segmentFrameData.ImagePositionPatient != null &&
                    segmentFrameData.ImagePositionPatient.Count() > 2 &&
                    segmentFrameData.ImageOrientationPatient != null &&
                    segmentFrameData.ImageOrientationPatient.Count() > 5)
                {
                    var imagePositionPatient = new ImagePositionPatient(
                        segmentFrameData.ImagePositionPatient[0],
                        segmentFrameData.ImagePositionPatient[1],
                        segmentFrameData.ImagePositionPatient[2]);
                    var imageOrientationPatient = new ImageOrientationPatient(
                        segmentFrameData.ImageOrientationPatient[0],
                        segmentFrameData.ImageOrientationPatient[1],
                        segmentFrameData.ImageOrientationPatient[2],
                        segmentFrameData.ImageOrientationPatient[3],
                        segmentFrameData.ImageOrientationPatient[4],
                        segmentFrameData.ImageOrientationPatient[5]);
                    IDisplaySet displaySet = presentationImage.ParentDisplaySet;

                    if (segPresentationImage == null)
                    {
                        segPresentationImage = PresentationImageFromPositionOrientation(
                            imagePositionPatient,
                            imageOrientationPatient,
                            displaySet,
                            segImageData.FrameOfReferenceUid);
                    }

                    var imageSop = segPresentationImage as IImageSopProvider;
                    if (imageSop != null)
                    {
                        Vector3D segImageLocation =
                            imageSop.Frame.ImagePlaneHelper.ConvertToImagePlane(new Vector3D(
                                                                                    (float) imagePositionPatient.X,
                                                                                    (float) imagePositionPatient.Y,
                                                                                    (float) imagePositionPatient.Z));
                        PointF? segPixelLocation =
                            imageSop.Frame.ImagePlaneHelper.ConvertToImage(
                                new PointF(segImageLocation.X, segImageLocation.Y));
                        if (segPixelLocation.HasValue)
                            segLocation = segPixelLocation.Value;
                    }
                }

                if (segPresentationImage != null)
                {
                    SegFrameImageGraphic newGraphic = AddSegFrameImageGraphicToPresentationImage(
                        segPresentationImage,
                        segImageData.Rows,
                        segImageData.Columns,
                        segLocation.X,
                        segLocation.Y,
                        seg.Color,
                        referencedImageFrameNumber,
                        seg.Label,
                        seg.Description,
                        pixelDataGetter.Invoke(segmentFrameData.FrameNumber - 1),
                        new SegmentationDocumentReference(segmentationDocument, seg.SegmentationNumber));
                    if (newGraphic != null &&
                        segPresentationImage == segPresentationImage.ParentDisplaySet.ImageBox.TopLeftPresentationImage)
                    {
                        newGraphic.Draw();
                    }
                }
                else
                {
                    Platform.Log(LogLevel.Error, "Failed to find Presentation Image to display a segment on");
                }
            }
        }
Esempio n. 16
0
 private static Vector3D ImagePositionPatientToVector(ImagePositionPatient position)
 {
     return(new Vector3D((float)position.X, (float)position.Y, (float)position.Z));
 }