public static ImageBuffers GetBuffersFromImage(Bitmap bmp) { try { UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp); ImageBuffers buffers = new ImageBuffers(); buffers.R = new int[bmp.Width * bmp.Height]; buffers.G = new int[bmp.Width * bmp.Height]; buffers.B = new int[bmp.Width * bmp.Height]; buffers.Width = bmp.Width; buffers.Height = bmp.Height; int count = 0; for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { buffers.R[count] = unsafeBitmap.GetPixel(i, j).R; buffers.G[count] = unsafeBitmap.GetPixel(i, j).G; buffers.B[count++] = unsafeBitmap.GetPixel(i, j).B; } } unsafeBitmap.UnlockBits(); return(buffers); } catch (Exception ex) { return(null); } }
public static UnsafeBitmap GetImageFromBuffers(ImageBuffers buffers) { try { UnsafeBitmap bmp = new UnsafeBitmap(buffers.Width, buffers.Height); int count = 0; for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { int R = buffers.R[count]; int G = buffers.G[count]; int B = buffers.B[count++]; bmp.SetPixel(i, j, Color.FromArgb(R, G, B)); } } bmp.UnlockBits(); return(bmp); } catch (Exception ex) { return(null); } }
void fn(ImageBuffers x) { var buffersOfFirstImage = BuffersFirstImage; int width = buffersOfFirstImage.Width; int height = buffersOfFirstImage.Height; int imageSize = width * height; sobel(buffersOfFirstImage.RedChannel, buffersOfFirstImage.GreenChannel, height, width, imageSize, 'x'); sobel(buffersOfFirstImage.RedChannel, buffersOfFirstImage.BlueChannel, height, width, imageSize, 'y'); var outputbuffer = ImageHelper.CreateNewImageBuffersObject(buffersOfFirstImage.GreenChannel, buffersOfFirstImage.GreenChannel, buffersOfFirstImage.GreenChannel, width, height); var outputBuffersObject = ImageHelper.CreateNewImageBuffersObject(buffersOfFirstImage.BlueChannel, buffersOfFirstImage.BlueChannel, buffersOfFirstImage.BlueChannel, width, height); for (int i = 0; i < outputBuffersObject.GreenChannel.Length; i++) { outputBuffersObject.GreenChannel[i] = (int)Math.Sqrt((Math.Pow(outputBuffersObject.GreenChannel[i], 2) + (Math.Pow(outputbuffer.BlueChannel[i], 2)))); outputBuffersObject.GreenChannel[i] = outputBuffersObject.GreenChannel[i] % 255; } this.outputImage_pictureBox.Image = (Bitmap)ImageHelper.GetImageFromBuffers(outputBuffersObject).BitmapObject; }