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); }
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); }
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); }
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); }
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); }
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); }