private void ReturnCTImage(int frame, TifFileInfo fi, UnmanagedImage image) { byte[] imageArr = image.ToByteArray(); int position = 0; if (fi.bitsPerPixel == 8) { byte[][] oldImage = fi.image8bitFilter[frame]; for (int y = 0; y < fi.sizeY; y++, position += fi.sizeX) { Array.Copy(imageArr, position, oldImage[y], 0, fi.sizeX); } } else if (fi.bitsPerPixel == 16) { ushort[][] oldImage = fi.image16bitFilter[frame]; for (int y = 0; y < fi.sizeY; y++) { for (int x = 0; x < fi.sizeX; x++, position++) { if (imageArr[position] == 0) { oldImage[y][x] = 0; } else { oldImage[y][x] = 16000; } } } } }
public void ToByteArray_test8pp(PixelFormat pixelFormat, int w, int h, int expected) { int[,] values = Vector.Range(0, 255).Get(0, h * w).Reshape(h, w); UnmanagedImage image = values.ToBitmap().ToUnmanagedImage(); int formatBytes = pixelFormat.GetPixelFormatSizeInBytes(); byte[] b = image.ToByteArray(); Assert.AreEqual(w * h * formatBytes, b.Length); Assert.AreEqual(expected, b.Length); // Reconstruct the original matrix UnmanagedImage r = UnmanagedImage.FromByteArray(b, w, h, pixelFormat); byte[,] actual = r.ToManagedImage().ToMatrix(0, 0, 255); Assert.AreEqual(values, actual); }
public void ToByteArray_test_general(PixelFormat pixelFormat, int w, int h, int expected) { int c = pixelFormat.GetNumberOfChannels(); byte[,,] values = (byte[, , ])Vector.Range((byte)0, (byte)255).Get(0, h * w).Reshape(new[] { h, w, c }); UnmanagedImage image = values.ToBitmap().ToUnmanagedImage(); int formatBytes = pixelFormat.GetPixelFormatSizeInBytes(); byte[] b = image.ToByteArray(); Assert.AreEqual(w * h * formatBytes, b.Length); Assert.AreEqual(expected, b.Length); // Reconstruct the original matrix UnmanagedImage r = UnmanagedImage.FromByteArray(b, w, h, pixelFormat); byte[,,] actual = r.ToManagedImage().ToMatrix((byte)0, (byte)255); Assert.AreEqual(values, actual); }
public void ToByteArray_test_general(PixelFormat pixelFormat, int w, int h, int expected) { int c = pixelFormat.GetNumberOfChannels(); byte[,,] values = (byte[, , ])Vector.Range((byte)0, (byte)255).Get(0, c * h * w).Reshape(new[] { h, w, c }); UnmanagedImage image = values.ToBitmap().ToUnmanagedImage(); int formatBytes = pixelFormat.GetPixelFormatSizeInBytes(); byte[] b = image.ToByteArray(); Assert.AreEqual(w * h * formatBytes, b.Length, "{0} * {1} * {2}, {3}", w, h, formatBytes, b.Length); Assert.AreEqual(expected, b.Length, "{0}, {1}", expected, b.Length); // Reconstruct the original matrix UnmanagedImage r = UnmanagedImage.FromByteArray(b, w, h, pixelFormat); byte[,,] actual = r.ToManagedImage().ToMatrix((byte)0, (byte)255); string a = String.Join(" ", (string[])Accord.Math.Matrix.ToString(actual).DeepFlatten()); string e = String.Join(" ", (string[])Accord.Math.Matrix.ToString(values).DeepFlatten()); Assert.AreEqual(e, a, "{0} == {1}", e, a); }
/// <summary> /// Process the filter on the specified image. /// </summary> /// /// <param name="image">Source image data.</param> /// protected unsafe override void ProcessFilter(UnmanagedImage image) { int width = image.Width; int height = image.Height; PixelFormat format = image.PixelFormat; int pixelSize = System.Drawing.Bitmap.GetPixelFormatSize(format) / 8; Debug.Assert(pixelSize == 1); // Debug.Assert(image.Stride == width); byte[] bPixels = image.ToByteArray(); this.fPixels = new float[bPixels.Length]; for (int i = 0; i < fPixels.Length; i++) { if (bPixels[i] != 0) { fPixels[i] = float.MaxValue; } } int[][] pointBufs = Jagged.Zeros <int>(2, width); // pass 1 & 2: increasing y for (int x = 0; x < width; x++) { pointBufs[0][x] = -1; pointBufs[1][x] = -1; } for (int y = 0; y < height; y++) { edmLine(bPixels, fPixels, pointBufs, width, y * width, y); } // pass 3 & 4: decreasing y for (int x = 0; x < width; x++) { pointBufs[0][x] = -1; pointBufs[1][x] = -1; } for (int y = height - 1; y >= 0; y--) { edmLine(bPixels, fPixels, pointBufs, width, y * width, y); } // Finish applying the distance function if (NeedSquareRoot(distance)) { for (int i = 0, p = 0; i < height; i++) { for (int j = 0; j < width; j++, p++) { float f = fPixels[p] = (fPixels[p] < 0f) ? 0 : (float)Math.Sqrt(fPixels[p]); if (f > max) { max = f; ued = new IntPoint(i, j); } } } } else { for (int i = 0, p = 0; i < height; i++) { for (int j = 0; j < width; j++, p++) { float f = fPixels[p] = (fPixels[p] < 0f) ? 0 : fPixels[p]; if (f > max) { max = f; ued = new IntPoint(i, j); } } } } // Normalize and store fixed(float *srcPtr = fPixels) { int offset = image.Offset; byte * dst = (byte *)image.ImageData.ToPointer(); float *src = srcPtr; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++, src++, dst++) { (*dst) = (byte)Vector.Scale(*src, 0, max, 0, 255); } dst += offset; } } }