Exemple #1
0
        //internal for unit testing only.
        internal static void CalculateVisibleRectangles(
            ImageGraphic imageGraphic,
            Rectangle clientRectangle,
            out Rectangle dstVisibleRectangle,
            out RectangleF srcVisibleRectangle)
        {
            Rectangle  srcRectangle = new Rectangle(0, 0, imageGraphic.Columns, imageGraphic.Rows);
            RectangleF dstRectangle = imageGraphic.SpatialTransform.ConvertToDestination(srcRectangle);

            // Find the intersection between the drawable client rectangle and
            // the transformed destination rectangle
            dstRectangle = RectangleUtilities.RoundInflate(dstRectangle);
            dstRectangle = RectangleUtilities.Intersect(clientRectangle, dstRectangle);

            if (dstRectangle.IsEmpty)
            {
                dstVisibleRectangle = Rectangle.Empty;
                srcVisibleRectangle = RectangleF.Empty;
                return;
            }

            // Figure out what portion of the image is visible in source coordinates
            srcVisibleRectangle = imageGraphic.SpatialTransform.ConvertToSource(dstRectangle);
            //dstRectangle is already rounded, this is just a conversion to Rectangle.
            dstVisibleRectangle = Rectangle.Round(dstRectangle);
        }
Exemple #2
0
        /// <summary>
        /// Returns a bounding box that has been rounded to the nearest whole pixel(s).
        /// </summary>
        /// <remarks>
        /// Uses <see cref="RectangleUtilities.RoundInflate"/> to ensure the bounding box
        /// returned will encompass every pixel that is inside the ROI.
        /// </remarks>
        /// <param name="constrainToImage">Whether or not the returned rectangle should also be constrained to the image bounds.</param>
	    public Rectangle GetBoundingBoxRounded(bool constrainToImage)
	    {
	        Rectangle bounds = RectangleUtilities.RoundInflate(BoundingBox);
	        if (constrainToImage)
	        {
                bounds.Intersect(new Rectangle(0, 0, this.ImageSize.Width - 1, this.ImageSize.Height - 1));
	        }
	        
            return bounds;
	    }
        protected void SerializeDisplayedArea(DisplayedAreaModuleIod displayedAreaModule, DicomPresentationImageCollection <T> images)
        {
            displayedAreaModule.InitializeAttributes();
            List <DisplayedAreaModuleIod.DisplayedAreaSelectionSequenceItem> displayedAreas = new List <DisplayedAreaModuleIod.DisplayedAreaSelectionSequenceItem>();

            foreach (T image in images)
            {
                DisplayedAreaModuleIod.DisplayedAreaSelectionSequenceItem displayedArea = new DisplayedAreaModuleIod.DisplayedAreaSelectionSequenceItem();
                displayedArea.InitializeAttributes();
                displayedArea.ReferencedImageSequence = new[] { CreateImageSopInstanceReference(image.Frame) };

                ImageGraphic imageGraphic = ((IImageGraphicProvider)image).ImageGraphic;
                Size         imageSize    = new Size(imageGraphic.Columns, imageGraphic.Rows);

                // compute the visible area of the image as a rectangle oriented positively in screen space
                RectangleF visibleImageArea = imageGraphic.SpatialTransform.ConvertToSource(image.ClientRectangle);
                visibleImageArea = RectangleUtilities.ConvertToPositiveRectangle(visibleImageArea);
                visibleImageArea = RectangleUtilities.RoundInflate(visibleImageArea);
                visibleImageArea = RectangleUtilities.Intersect(visibleImageArea, new Rectangle(new Point(0, 0), imageSize));

                // compute the pixel addresses of the visible area by intersecting area with actual pixel addresses available
                Rectangle visiblePixels = ConvertToPixelAddressRectangle(Rectangle.Truncate(visibleImageArea));
                displayedArea.DisplayedAreaTopLeftHandCorner     = visiblePixels.Location;
                displayedArea.DisplayedAreaBottomRightHandCorner = visiblePixels.Location + visiblePixels.Size;

                ISpatialTransform spatialTransform = image.SpatialTransform;
                switch (_displayAreaSerializationOption)
                {
                case DisplayAreaSerializationOption.SerializeAsMagnification:
                    displayedArea.PresentationSizeMode = DisplayedAreaModuleIod.PresentationSizeMode.Magnify;
                    displayedArea.PresentationPixelMagnificationRatio = spatialTransform.Scale;
                    break;

                case DisplayAreaSerializationOption.SerializeAsTrueSize:
                    displayedArea.PresentationSizeMode     = DisplayedAreaModuleIod.PresentationSizeMode.TrueSize;
                    displayedArea.PresentationPixelSpacing = image.Frame.NormalizedPixelSpacing;
                    break;

                case DisplayAreaSerializationOption.SerializeAsDisplayedArea:
                default:
                    displayedArea.PresentationSizeMode = DisplayedAreaModuleIod.PresentationSizeMode.ScaleToFit;
                    break;
                }

                displayedArea.PresentationPixelAspectRatio = PixelAspectRatio.FromString(image.Frame.NormalizedPixelSpacing.GetPixelAspectRatioString());

                displayedAreas.Add(displayedArea);
            }
            displayedAreaModule.DisplayedAreaSelectionSequence = displayedAreas.ToArray();
        }
        private static double CalculateMean
        (
            RectangleF roiBoundingBox,
            GrayscalePixelData pixelData,
            IModalityLut modalityLut,
            IsPointInRoiDelegate isPointInRoi
        )
        {
            double sum        = 0;
            int    pixelCount = 0;

            var boundingBox = RectangleUtilities.RoundInflate(RectangleUtilities.ConvertToPositiveRectangle(roiBoundingBox));

            pixelData.ForEachPixel(
                boundingBox.Left,
                boundingBox.Top,
                boundingBox.Right,
                boundingBox.Bottom,
                delegate(int i, int x, int y, int pixelIndex)
            {
                if (isPointInRoi(x, y))
                {
                    ++pixelCount;
                    // Make sure we run the raw pixel through the modality LUT
                    // when doing the calculation. Note that the modality LUT
                    // can be something other than a rescale intercept, so we can't
                    // just run the mean through the LUT.
                    int storedValue  = pixelData.GetPixel(pixelIndex);
                    double realValue = modalityLut != null ? modalityLut[storedValue] : storedValue;
                    sum += realValue;
                }
            });

            if (pixelCount == 0)
            {
                return(0);
            }

            return(sum / pixelCount);
        }
        private static double CalculateStandardDeviation
        (
            double mean,
            RectangleF roiBoundingBox,
            GrayscalePixelData pixelData,
            IModalityLut modalityLut,
            IsPointInRoiDelegate isPointInRoi
        )
        {
            double sum        = 0;
            int    pixelCount = 0;

            var boundingBox = RectangleUtilities.RoundInflate(RectangleUtilities.ConvertToPositiveRectangle(roiBoundingBox));

            pixelData.ForEachPixel(
                boundingBox.Left,
                boundingBox.Top,
                boundingBox.Right,
                boundingBox.Bottom,
                delegate(int i, int x, int y, int pixelIndex)
            {
                if (isPointInRoi(x, y))
                {
                    ++pixelCount;
                    int storedValue  = pixelData.GetPixel(pixelIndex);
                    double realValue = modalityLut != null ? modalityLut[storedValue] : storedValue;

                    double deviation = realValue - mean;
                    sum += deviation * deviation;
                }
            });

            if (pixelCount == 0)
            {
                return(0);
            }

            return(Math.Sqrt(sum / pixelCount));
        }
        internal static double CalculateStandardDeviationForStack(double mean,
                                                                  List <ImageCalculationInfo> imageCalculationInfoStack)
        {
            double sum        = 0;
            int    pixelCount = 0;

            for (int imageCount = 0; imageCount < imageCalculationInfoStack.Count; imageCount++)
            {
                PixelData            pixelData            = imageCalculationInfoStack[imageCount].PixelData;
                SegFrameImageGraphic segFrameImageGraphic = imageCalculationInfoStack[imageCount].SegFrameImageGraphic;
                RectangleF           roiBoundingBox       = imageCalculationInfoStack[imageCount].RoiBoundingBox;
                IModalityLut         modalityLut          = imageCalculationInfoStack[imageCount].ModalityLut;

                Rectangle boundingBox =
                    RectangleUtilities.RoundInflate(RectangleUtilities.ConvertToPositiveRectangle(roiBoundingBox));

                int left = boundingBox.Left;
                if (left < 0)
                {
                    left = 0;
                }
                if (left >= segFrameImageGraphic.Columns)
                {
                    left = segFrameImageGraphic.Columns - 1;
                }
                int right = boundingBox.Right;
                if (right < 0)
                {
                    right = 0;
                }
                if (right >= segFrameImageGraphic.Columns)
                {
                    right = segFrameImageGraphic.Columns - 1;
                }
                int top = boundingBox.Top;
                if (top < 0)
                {
                    top = 0;
                }
                if (top >= segFrameImageGraphic.Rows)
                {
                    top = segFrameImageGraphic.Rows - 1;
                }
                int bottom = boundingBox.Bottom;
                if (bottom < 0)
                {
                    bottom = 0;
                }
                if (bottom >= segFrameImageGraphic.Rows)
                {
                    bottom = segFrameImageGraphic.Rows - 1;
                }

                pixelData.ForEachPixel(
                    left,
                    top,
                    right,
                    bottom,
                    delegate(int i, int x, int y, int pixelIndex)
                {
                    if (segFrameImageGraphic[x, y])
                    {
                        ++pixelCount;
                        int storedValue  = pixelData.GetPixel(pixelIndex);
                        double realValue = modalityLut != null ? modalityLut[storedValue] : storedValue;

                        double deviation = realValue - mean;
                        sum += deviation * deviation;
                    }
                });
            }

            if (pixelCount == 0)
            {
                return(0);
            }

            return(Math.Sqrt(sum / pixelCount));
        }
        internal static double CalculateMeanForStack(List <ImageCalculationInfo> imageCalculationInfoStack)
        {
            double sum        = 0;
            int    pixelCount = 0;

            for (int imageCount = 0; imageCount < imageCalculationInfoStack.Count; imageCount++)
            {
                PixelData            pixelData            = imageCalculationInfoStack[imageCount].PixelData;
                SegFrameImageGraphic segFrameImageGraphic = imageCalculationInfoStack[imageCount].SegFrameImageGraphic;
                RectangleF           roiBoundingBox       = imageCalculationInfoStack[imageCount].RoiBoundingBox;
                IModalityLut         modalityLut          = imageCalculationInfoStack[imageCount].ModalityLut;

                Rectangle boundingBox =
                    RectangleUtilities.RoundInflate(RectangleUtilities.ConvertToPositiveRectangle(roiBoundingBox));

                int left = boundingBox.Left;
                if (left < 0)
                {
                    left = 0;
                }
                if (left >= segFrameImageGraphic.Columns)
                {
                    left = segFrameImageGraphic.Columns - 1;
                }
                int right = boundingBox.Right;
                if (right < 0)
                {
                    right = 0;
                }
                if (right >= segFrameImageGraphic.Columns)
                {
                    right = segFrameImageGraphic.Columns - 1;
                }
                int top = boundingBox.Top;
                if (top < 0)
                {
                    top = 0;
                }
                if (top >= segFrameImageGraphic.Rows)
                {
                    top = segFrameImageGraphic.Rows - 1;
                }
                int bottom = boundingBox.Bottom;
                if (bottom < 0)
                {
                    bottom = 0;
                }
                if (bottom >= segFrameImageGraphic.Rows)
                {
                    bottom = segFrameImageGraphic.Rows - 1;
                }

                pixelData.ForEachPixel(
                    left,
                    top,
                    right,
                    bottom,
                    delegate(int i, int x, int y, int pixelIndex)
                {
                    //if (x >= 0 && x < segFrameImageGraphic.Columns && y >= 0 && y < segFrameImageGraphic.Rows)
                    if (segFrameImageGraphic[x, y])
                    {
                        ++pixelCount;
                        // Make sure we run the raw pixel through the modality LUT
                        // when doing the calculation. Note that the modality LUT
                        // can be something other than a rescale intercept, so we can't
                        // just run the mean through the LUT.
                        int storedValue  = pixelData.GetPixel(pixelIndex);
                        double realValue = modalityLut != null ? modalityLut[storedValue] : storedValue;
                        sum += realValue;
                    }
                });
            }

            if (pixelCount == 0)
            {
                return(0);
            }

            return(sum / pixelCount);
        }
Exemple #8
0
        public void RoundInflated()
        {
            //positive width and height
            RectangleF testRectangle = new RectangleF(1.5F, 2.5F, 5F, 7F);

            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 1);
            Assert.AreEqual(testRectangle.Top, 2);
            Assert.AreEqual(testRectangle.Right, 7);
            Assert.AreEqual(testRectangle.Bottom, 10);

            testRectangle = new RectangleF(1.0F, 2.0F, 5F, 7F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 1);
            Assert.AreEqual(testRectangle.Top, 2);
            Assert.AreEqual(testRectangle.Right, 6);
            Assert.AreEqual(testRectangle.Bottom, 9);

            testRectangle = new RectangleF(1.0F, 2.0F, 5.5F, 7.5F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 1);
            Assert.AreEqual(testRectangle.Top, 2);
            Assert.AreEqual(testRectangle.Right, 7);
            Assert.AreEqual(testRectangle.Bottom, 10);

            testRectangle = new RectangleF(-1.5F, -2.5F, 5F, 7F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -2);
            Assert.AreEqual(testRectangle.Top, -3);
            Assert.AreEqual(testRectangle.Right, 4);
            Assert.AreEqual(testRectangle.Bottom, 5);

            testRectangle = new RectangleF(-1.5F, -2.5F, 5.49F, 7.49F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -2);
            Assert.AreEqual(testRectangle.Top, -3);
            Assert.AreEqual(testRectangle.Right, 4);
            Assert.AreEqual(testRectangle.Bottom, 5);

            testRectangle = new RectangleF(-1.5F, -2.5F, 5.5F, 7.5F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -2);
            Assert.AreEqual(testRectangle.Top, -3);
            Assert.AreEqual(testRectangle.Right, 4);
            Assert.AreEqual(testRectangle.Bottom, 5);

            testRectangle = new RectangleF(-1.5F, -2.5F, 5.51F, 7.51F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -2);
            Assert.AreEqual(testRectangle.Top, -3);
            Assert.AreEqual(testRectangle.Right, 5);
            Assert.AreEqual(testRectangle.Bottom, 6);

            testRectangle = new RectangleF(-11.5F, -12.5F, 5F, 7F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -12);
            Assert.AreEqual(testRectangle.Top, -13);
            Assert.AreEqual(testRectangle.Right, -6);
            Assert.AreEqual(testRectangle.Bottom, -5);

            //negative width and height
            testRectangle = new RectangleF(10.5F, 11.5F, -5F, -7F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 11);
            Assert.AreEqual(testRectangle.Top, 12);
            Assert.AreEqual(testRectangle.Right, 5);
            Assert.AreEqual(testRectangle.Bottom, 4);

            testRectangle = new RectangleF(10.5F, 11.5F, -5.49F, -7.49F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 11);
            Assert.AreEqual(testRectangle.Top, 12);
            Assert.AreEqual(testRectangle.Right, 5);
            Assert.AreEqual(testRectangle.Bottom, 4);

            testRectangle = new RectangleF(10.5F, 11.5F, -5.5F, -7.5F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 11);
            Assert.AreEqual(testRectangle.Top, 12);
            Assert.AreEqual(testRectangle.Right, 5);
            Assert.AreEqual(testRectangle.Bottom, 4);

            testRectangle = new RectangleF(10.5F, 11.5F, -5.51F, -7.51F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 11);
            Assert.AreEqual(testRectangle.Top, 12);
            Assert.AreEqual(testRectangle.Right, 4);
            Assert.AreEqual(testRectangle.Bottom, 3);

            testRectangle = new RectangleF(-10.5F, -11.5F, -5F, -7F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -10);
            Assert.AreEqual(testRectangle.Top, -11);
            Assert.AreEqual(testRectangle.Right, -16);
            Assert.AreEqual(testRectangle.Bottom, -19);

            testRectangle = new RectangleF(-10.5F, -11.5F, -5.49F, -7.49F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -10);
            Assert.AreEqual(testRectangle.Top, -11);
            Assert.AreEqual(testRectangle.Right, -16);
            Assert.AreEqual(testRectangle.Bottom, -19);

            testRectangle = new RectangleF(-10.5F, -11.5F, -5.5F, -7.5F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -10);
            Assert.AreEqual(testRectangle.Top, -11);
            Assert.AreEqual(testRectangle.Right, -16);
            Assert.AreEqual(testRectangle.Bottom, -19);

            testRectangle = new RectangleF(-10.5F, -11.5F, -5.51F, -7.51F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, -10);
            Assert.AreEqual(testRectangle.Top, -11);
            Assert.AreEqual(testRectangle.Right, -17);
            Assert.AreEqual(testRectangle.Bottom, -20);

            testRectangle = new RectangleF(3.5F, 5.5F, -5F, -7F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 4);
            Assert.AreEqual(testRectangle.Top, 6);
            Assert.AreEqual(testRectangle.Right, -2);
            Assert.AreEqual(testRectangle.Bottom, -2);

            testRectangle = new RectangleF(3.5F, 5.5F, -5.49F, -7.49F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 4);
            Assert.AreEqual(testRectangle.Top, 6);
            Assert.AreEqual(testRectangle.Right, -2);
            Assert.AreEqual(testRectangle.Bottom, -2);

            testRectangle = new RectangleF(3.5F, 5.5F, -5.5F, -7.5F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 4);
            Assert.AreEqual(testRectangle.Top, 6);
            Assert.AreEqual(testRectangle.Right, -2);
            Assert.AreEqual(testRectangle.Bottom, -2);

            testRectangle = new RectangleF(3.5F, 5.5F, -5.51F, -7.51F);
            testRectangle = RectangleUtilities.RoundInflate(testRectangle);
            Assert.AreEqual(testRectangle.Left, 4);
            Assert.AreEqual(testRectangle.Top, 6);
            Assert.AreEqual(testRectangle.Right, -3);
            Assert.AreEqual(testRectangle.Bottom, -3);
        }