private static Image CreateNewImage(Image image, int fieldSize) { Stopwatch watch = new Stopwatch(); watch.Start(); var size = image.Size; Image newImage = new Image(size.Width, size.Height); var access = newImage.Planes[0].GetLinearAccess <byte>(); PixelHelper pixelHelper = new PixelHelper(image.Planes[0]); for (int y = 0; y < size.Height; y++) { for (int x = 0; x < size.Width; x++) { access[x, y] = (byte)PixelsInNeighborhod(pixelHelper, x, y, fieldSize); } } watch.Stop(); MessageBox.Show(watch.ElapsedMilliseconds.ToString()); return(newImage); }
private static Image CreateNewImage(Image image, int[,] kernel) { var size = image.Size; Image newImage = new Image(size.Width, size.Height); var access = newImage.Planes[0].GetLinearAccess <byte>(); PixelHelper pixelHelper = new PixelHelper(image.Planes[0]); for (int y = 0; y < size.Height; y++) { for (int x = 0; x < size.Width; x++) { access[x, y] = (byte)KernelOperation(pixelHelper, x, y, kernel); } } return(newImage); }
private static unsafe int PixelsInNeighborhod(PixelHelper pixelHelper, int x, int y, int fieldSize) { int pixelValueSum = 0; int numberOfPixels = 0; for (int kernelY = y - fieldSize; kernelY <= y + fieldSize; kernelY++) { var ypart = kernelY * pixelHelper.YInc + pixelHelper.PBase; for (int kernelX = x - fieldSize; kernelX <= x + fieldSize; kernelX++) { if (IsInsideImage(pixelHelper.Size, kernelX, kernelY)) { numberOfPixels++; pixelValueSum += *(kernelX * pixelHelper.XInc + ypart); } } } return(pixelValueSum / numberOfPixels); }
private static unsafe int KernelOperation(PixelHelper pixelHelper, int x, int y, int[,] kernel) { int valueSum = 0; int firstDimension = 0; int pixelsAround = (kernel.GetLength(0) - 1) / 2; for (int kernelY = y - pixelsAround; kernelY <= y + pixelsAround; kernelY++) { var ypart = kernelY * pixelHelper.YInc + pixelHelper.PBase; int secondDimension = 0; for (int kernelX = x - pixelsAround; kernelX <= x + pixelsAround; kernelX++) { if (IsInsideImage(pixelHelper.Size, kernelX, kernelY)) { var pixelValue = *(kernelX * pixelHelper.XInc + ypart); valueSum += pixelValue * kernel[firstDimension, secondDimension]; } secondDimension++; } firstDimension++; } return(valueSum / kernel.Length); }