Ejemplo n.º 1
0
        private static void Signed5Bit(byte[] pixelData, PixelData pixelDataWrapper)
        {
            pixelData[0] = 31;

            int actualValue = pixelDataWrapper.GetPixel(0, 0);

            Assert.AreEqual(-1, actualValue);

            int expectedValue = -1;

            pixelDataWrapper.SetPixel(0, 0, expectedValue);
            actualValue = pixelDataWrapper.GetPixel(0, 0);
            Assert.AreEqual(expectedValue, actualValue);

            expectedValue = -16;
            pixelDataWrapper.SetPixel(0, 0, expectedValue);
            actualValue = pixelDataWrapper.GetPixel(0, 0);
            Assert.AreEqual(expectedValue, actualValue);

            expectedValue = 15;
            pixelDataWrapper.SetPixel(0, 0, expectedValue);
            actualValue = pixelDataWrapper.GetPixel(0, 0);
            Assert.AreEqual(expectedValue, actualValue);

            expectedValue = 0;
            pixelDataWrapper.SetPixel(0, 0, expectedValue);
            actualValue = pixelDataWrapper.GetPixel(0, 0);
            Assert.AreEqual(expectedValue, actualValue);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets an enumeration of the modality LUT transformed pixel values contained within the region of interest.
        /// </summary>
        /// <remarks>
        /// If the <see cref="ModalityLut"/> is null, then this method enumerates the same values as <see cref="GetRawPixelValues"/>.
        /// </remarks>
        /// <returns>An enumeration of modality LUT transformed pixel values.</returns>
        public IEnumerable <double> GetPixelValues()
        {
            if (PixelData == null)
            {
                yield break;
            }

            LutFunction lut = v => v;

            if (ModalityLut != null)
            {
                lut = v => ModalityLut[v];
            }

            Rectangle bounds = GetBoundingBoxRounded(true);

            for (int x = bounds.Left; x <= bounds.Right; x++)
            {
                for (int y = bounds.Top; y <= bounds.Bottom; y++)
                {
                    PointF p = new PointF(x, y);
                    if (Contains(p))
                    {
                        yield return(lut(PixelData.GetPixel(x, y)));
                    }
                }
            }
        }
        private static float ImageKernelFunction3X3(PixelData pd, int x, int y)
        {
            float weightedMean = 0;

            weightedMean += 0.0625f * pd.GetPixel(x - 1, y - 1);
            weightedMean += 0.0625f * pd.GetPixel(x - 1, y + 0);
            weightedMean += 0.0625f * pd.GetPixel(x - 1, y + 1);
            weightedMean += 0.0625f * pd.GetPixel(x + 0, y - 1);
            weightedMean += 0.5000f * pd.GetPixel(x + 0, y + 0);
            weightedMean += 0.0625f * pd.GetPixel(x + 0, y + 1);
            weightedMean += 0.0625f * pd.GetPixel(x + 1, y - 1);
            weightedMean += 0.0625f * pd.GetPixel(x + 1, y + 0);
            weightedMean += 0.0625f * pd.GetPixel(x + 1, y + 1);
            return(weightedMean);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets an enumeration of the raw pixel values contained within the region of interest.
        /// </summary>
        /// <returns>An enumeration of raw pixel values.</returns>
        public IEnumerable <int> GetRawPixelValues()
        {
            if (PixelData == null)
            {
                yield break;
            }

            Rectangle bounds = GetBoundingBoxRounded(true);

            for (int x = bounds.Left; x <= bounds.Right; x++)
            {
                for (int y = bounds.Top; y <= bounds.Bottom; y++)
                {
                    PointF p = new PointF(x, y);
                    if (Contains(p))
                    {
                        yield return(PixelData.GetPixel(x, y));
                    }
                }
            }
        }
        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);
        }