Example #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="pixelData">The pixel data the algorithm will be run on.</param>
        /// <param name="modalityLut">The modality lut to use for calculating <see cref="WindowWidth"/> and <see cref="WindowCenter"/>, if applicable.</param>
        protected AlgorithmCalculatedVoiLutLinear(GrayscalePixelData pixelData, IModalityLut modalityLut)
        {
            Platform.CheckForNullReference(pixelData, "pixelData");

            _pixelData   = pixelData;
            _modalityLut = modalityLut;

            _windowWidth  = double.NaN;
            _windowCenter = double.NaN;
        }
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="pixelData">The pixel data the algorithm will be run on.</param>
		/// <param name="modalityLut">The modality lut to use for calculating <see cref="WindowWidth"/> and <see cref="WindowCenter"/>, if applicable.</param>
		protected AlgorithmCalculatedVoiLutLinear(GrayscalePixelData pixelData, IModalityLut modalityLut)
		{
			Platform.CheckForNullReference(pixelData, "pixelData");

			_pixelData = pixelData;
			_modalityLut = modalityLut;

			_windowWidth = double.NaN;
			_windowCenter = double.NaN;
		}
Example #3
0
		public void SetPixelUnsigned8()
		{
			int columns = 7;
			int rows = 19;
			int bitsAllocated = 8;
			int bitsStored = 8;
			int highBit = 7;
			bool isSigned = false;
			int samplesPerPixel = 1;
			int imageSize = columns * rows * bitsAllocated / 8 * samplesPerPixel;
			byte[] pixelData = new byte[imageSize];

			PixelData pixelDataWrapper = new GrayscalePixelData(
				rows,
				columns,
				bitsAllocated,
				bitsStored,
				highBit,
				isSigned,
				pixelData);

			int x = 3;
			int y = 4;

			int testValue = 0xab;
			pixelDataWrapper.SetPixel(x, y, testValue);
			int actualValue = pixelDataWrapper.GetPixel(x, y);
			Assert.AreEqual(testValue, actualValue);

			// Make sure it works with unsafe code too
			fixed (byte* pPixelData = pixelDataWrapper.Raw)
			{
				int i = y * columns + x;
				actualValue = pPixelData[i];
			}

			Assert.AreEqual(testValue, actualValue);
		}
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="pixelData">The pixel data the algorithm will be run on.</param>
		protected AlgorithmCalculatedVoiLutLinear(GrayscalePixelData pixelData)
			: this(pixelData, null)
		{
		}
Example #5
0
		public void Signed9Bit()
		{
			int columns = 7;
			int rows = 19;
			int bitsAllocated = 16;
			int bitsStored = 9;
			int highBit = 8;
			bool isSigned = true;
			int samplesPerPixel = 1;
			int imageSize = columns * rows * bitsAllocated / 8 * samplesPerPixel;
			byte[] pixelData = new byte[imageSize];

			PixelData pixelDataWrapper = new GrayscalePixelData(
				rows,
				columns,
				bitsAllocated,
				bitsStored,
				highBit,
				isSigned,
				pixelData);

			pixelData[0] = 255;
			pixelData[1] = 1;

			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 = -256;
			pixelDataWrapper.SetPixel(0, 0, expectedValue);
			actualValue = pixelDataWrapper.GetPixel(0, 0);
			Assert.AreEqual(expectedValue, actualValue);

			expectedValue = 255;
			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);
		}
Example #6
0
		public void Signed5Bit16BitsAllocated()
		{
			int columns = 7;
			int rows = 19;
			int bitsAllocated = 16;
			int bitsStored = 5;
			int highBit = 4;
			bool isSigned = true;
			int samplesPerPixel = 1;
			int imageSize = columns * rows * bitsAllocated / 8 * samplesPerPixel;
			byte[] pixelData = new byte[imageSize];

			PixelData pixelDataWrapper = new GrayscalePixelData(
				rows,
				columns,
				bitsAllocated,
				bitsStored,
				highBit,
				isSigned,
				pixelData);

			Signed5Bit(pixelData, pixelDataWrapper);
		}
Example #7
0
		public void SetPixelSigned16()
		{
			int columns = 7;
			int rows = 19;
			int bitsAllocated = 16;
			int bitsStored = 16;
			int highBit = 15;
			bool isSigned = true;
			int samplesPerPixel = 1;
			int imageSize = columns * rows * bitsAllocated / 8 * samplesPerPixel;
			byte[] pixelData = new byte[imageSize];

			PixelData pixelDataWrapper = new GrayscalePixelData(
				rows,
				columns,
				bitsAllocated,
				bitsStored,
				highBit,
				isSigned,
				pixelData);

			int x = 3;
			int y = 4;

			// Test the API
			int testValue = -1234;
			pixelDataWrapper.SetPixel(x, y, testValue);
			int actualValue = pixelDataWrapper.GetPixel(x, y);
			Assert.AreEqual(testValue, actualValue);

			// Make sure it works with unsafe code too
			fixed (byte* pPixelData = pixelDataWrapper.Raw)
			{
				short* pShortPixelData = (short*)pPixelData;
				int i = y * columns + x;
				actualValue = pShortPixelData[i];
			}

			Assert.AreEqual(testValue, actualValue);
		}
Example #8
0
		public void SetPixelSigned16Grayscale()
		{
			int columns = 7;
			int rows = 19;
			int bitsAllocated = 16;
			int bitsStored = 16;
			int highBit = 15;
			bool isSigned = true;
			int samplesPerPixel = 1;
			int imageSize = columns * rows * bitsAllocated / 8 * samplesPerPixel;
			byte[] pixelData = new byte[imageSize];

			PixelData pixelDataWrapper = new GrayscalePixelData(
				rows,
				columns,
				bitsAllocated,
				bitsStored,
				highBit,
				isSigned,
				pixelData);

			int x = 3;
			int y = 4;

			// Test the API
			int testValue = -1234;
			pixelDataWrapper.SetPixel(x, y, testValue);
			int actualValue = pixelDataWrapper.GetPixel(x, y);
			Assert.AreEqual(testValue, actualValue);

			int index = y * columns + x;
			actualValue = pixelDataWrapper.GetPixel(index);
			Assert.AreEqual(testValue, actualValue);
		}
 /// <summary>
 /// Calculates and returns the minimum and maximum pixel values in the input <paramref name="pixelData"/>.
 /// </summary>
 /// <param name="pixelData">The input pixel data.</param>
 /// <param name="windowStart">Returns the minimum pixel value.</param>
 /// <param name="windowEnd">Returns the maximum pixel value.</param>
 protected override void CalculateWindowRange(GrayscalePixelData pixelData, out int windowStart, out int windowEnd)
 {
     pixelData.CalculateMinMaxPixelValue(out windowStart, out windowEnd);
 }
Example #10
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="pixelData">The pixel data the algorithm will be run on.</param>
 protected AlgorithmCalculatedVoiLutLinear(GrayscalePixelData pixelData)
     : this(pixelData, null)
 {
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="pixelData">The pixel data the algorithm will be run on.</param>
 public MinMaxPixelCalculatedLinearLut(GrayscalePixelData pixelData)
     : base(pixelData)
 {
 }
Example #12
0
 /// <summary>
 /// Called when either of <see cref="WindowWidth"/> or <see cref="WindowCenter"/> are first accessed.
 /// </summary>
 /// <remarks>
 /// Inheritors must implement this method and return the <paramref name="windowStart"/> and <paramref name="windowEnd"/>
 /// value range that will be used to calculate the <see cref="WindowWidth"/> and <see cref="WindowCenter"/>.
 /// </remarks>
 /// <param name="pixelData">The pixel data that is to be used to calculate <paramref name="windowStart"/> and <paramref name="windowEnd"/>.</param>
 /// <param name="windowStart">returns the start value in the window range.</param>
 /// <param name="windowEnd">returns the end value in the window range.</param>
 protected abstract void CalculateWindowRange(GrayscalePixelData pixelData, out int windowStart, out int windowEnd);
		/// <summary>
		/// Called when either of <see cref="WindowWidth"/> or <see cref="WindowCenter"/> are first accessed.
		/// </summary>
		/// <remarks>
		/// Inheritors must implement this method and return the <paramref name="windowStart"/> and <paramref name="windowEnd"/> 
		/// value range that will be used to calculate the <see cref="WindowWidth"/> and <see cref="WindowCenter"/>.
		/// </remarks>
		/// <param name="pixelData">The pixel data that is to be used to calculate <paramref name="windowStart"/> and <paramref name="windowEnd"/>.</param>
		/// <param name="windowStart">returns the start value in the window range.</param>
		/// <param name="windowEnd">returns the end value in the window range.</param>
		protected abstract void CalculateWindowRange(GrayscalePixelData pixelData, out int windowStart, out int windowEnd);
Example #14
0
		public void ForEachPixel()
		{
			int columns = 5;
			int rows = 4;
			int bitsAllocated = 16;
			int bitsStored = 16;
			int highBit = 15;
			bool isSigned = true;
			int samplesPerPixel = 1;
			int imageSize = columns * rows * bitsAllocated / 8 * samplesPerPixel;
			byte[] pixelData = new byte[imageSize];

			PixelData pixelDataWrapper = new GrayscalePixelData(
				rows,
				columns,
				bitsAllocated,
				bitsStored,
				highBit,
				isSigned,
				pixelData);

			int i = 0;

			for (int y = 0; y < rows; y++)
			{
				for (int x = 0; x < columns; x++)
				{
					pixelDataWrapper.SetPixel(x, y, i);
					i++;
				}
			}

			int left = 1, top = 1, right = 3, bottom = 3;

			int pixelCount = 0;

			pixelDataWrapper.ForEachPixel(left, top, right, bottom,
				delegate(int j, int x, int y, int pixelIndex)
				{
					if (j == 0)
						Assert.AreEqual(6, pixelDataWrapper.GetPixel(pixelIndex));
					else if (j == 1)
						Assert.AreEqual(7, pixelDataWrapper.GetPixel(pixelIndex));
					else if (j == 3)
						Assert.AreEqual(11, pixelDataWrapper.GetPixel(pixelIndex));

					pixelCount++;
				});

			Assert.AreEqual(9, pixelCount);
		}
		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);
		}
		/// <summary>
		/// Calculates and returns the minimum and maximum pixel values in the input <paramref name="pixelData"/>.
		/// </summary>
		/// <param name="pixelData">The input pixel data.</param>
		/// <param name="windowStart">Returns the minimum pixel value.</param>
		/// <param name="windowEnd">Returns the maximum pixel value.</param>
		protected override void CalculateWindowRange(GrayscalePixelData pixelData, out int windowStart, out int windowEnd)
		{
			pixelData.CalculateMinMaxPixelValue(out windowStart, out windowEnd);
		}
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="pixelData">The pixel data the algorithm will be run on.</param>
		public MinMaxPixelCalculatedLinearLut(GrayscalePixelData pixelData)
			: base(pixelData)
		{
		}
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <remarks>
		/// The input <see cref="IVoiLut"/> object can be null.
		/// </remarks>
		/// <param name="pixelData">The pixel data the algorithm will be run on.</param>
		/// <param name="modalityLut">The modality lut to use for calculating <see cref="AlgorithmCalculatedVoiLutLinear.WindowWidth"/> 
		/// and <see cref="AlgorithmCalculatedVoiLutLinear.WindowCenter"/>, if applicable.</param>
		public MinMaxPixelCalculatedLinearLut(GrayscalePixelData pixelData, IModalityLut modalityLut)
			: base(pixelData, modalityLut)
		{
		}
        // TODO - move to the Graphics helpers class?
        private static GrayscalePixelData CreateFramePixelData(IPresentationImage presentationImage, IEnumerable<IEnumerable<PointF>> polygons)
        {
            var imageGraphicProvider = presentationImage as IImageGraphicProvider;
            if (imageGraphicProvider == null)
                return null;

            var imageGraphic = imageGraphicProvider.ImageGraphic;
            // Unpacked
            var grayscalePixelData = new GrayscalePixelData(
                imageGraphic.Rows, imageGraphic.Columns, // image dimensions
                8, // bits allocated is 8
                8, // bits stored is 8
                7, // the high bit is 7
                false, // not signed
                MemoryManager.Allocate<byte>(imageGraphic.Rows * imageGraphic.Columns)); // new empty pixel buffer

            // Packing 1 bit at a time (Little Endean). See OverlayData for better example
            //var grayscalePixelData = new GrayscalePixelData(
            //    imageGraphic.Rows, imageGraphic.Columns, // image dimensions
            //    1, // bits allocated is 1
            //    1, // bits stored is 1
            //    1, // the high bit is 1
            //    false, // not signed
            //    MemoryManager.Allocate<byte>((int) Math.Ceiling(imageGraphic.Rows * imageGraphic.Columns/8.0))); // new empty pixel buffer

            var columns = imageGraphic.Columns;
            foreach (var polygon in polygons)
            {
                var verticesArray = polygon.ToArray();
                var boundingBox = RectangleUtilities.ComputeBoundingRectangle(verticesArray);

                // Convert vector polygon to raster on a per pixel basis
                imageGraphic.PixelData.ForEachPixel(
                    delegate(int i, int x, int y, int pixelIndex)
                    {
                            // Unpacked
                            var point = new PointF(x, y);
                            var isPixelSet = grayscalePixelData.Raw[y * columns + x] == 0xFF ||
                                (boundingBox.Contains(point) &&
                                SegmentationGraphicsHelpers.IsInPolygon(verticesArray, point));
                            grayscalePixelData.Raw[y * columns + x] = isPixelSet ? (byte)0xFF : (byte)0x00;

                            // Packed
                            //var mask = (byte) (1 << (pixelIndex%8));
                            //var outPos = pixelIndex/8;
                            //var curByte = grayscalePixelData.Raw[outPos];
                            //var point = new PointF(x, y);
                            //var isPixelSet = (boundingBox.Contains(point) && SegmentationGraphicsHelpers.IsInPolygon(verticesArray, point));
                            //grayscalePixelData.Raw[outPos] = (byte)(isPixelSet ? curByte | mask : curByte & ~mask);
                        }
                    );
            }

            return grayscalePixelData;
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <remarks>
 /// The input <see cref="IVoiLut"/> object can be null.
 /// </remarks>
 /// <param name="pixelData">The pixel data the algorithm will be run on.</param>
 /// <param name="modalityLut">The modality lut to use for calculating <see cref="AlgorithmCalculatedVoiLutLinear.WindowWidth"/>
 /// and <see cref="AlgorithmCalculatedVoiLutLinear.WindowCenter"/>, if applicable.</param>
 public MinMaxPixelCalculatedLinearLut(GrayscalePixelData pixelData, IModalityLut modalityLut)
     : base(pixelData, modalityLut)
 {
 }