Beispiel #1
0
        public static Bitmap Filter(byte[,] input, Kernel kernel, bool monochrome = false)
        {
            int width       = input.GetLength(0);
            int height      = input.GetLength(1);
            int kWidth      = kernel.Width;
            int kHeight     = kernel.Height;
            int kWidthHalf  = kWidth / 2;
            int kHeightHalf = kHeight / 2;

            byte[,] inputBytes  = AddOutline(input, kWidthHalf, kHeightHalf);
            byte[,] outputBytes = new byte[width, height];

            double sumPixel, sumKernel;
            double kernelValue;
            int    x, y, i, j;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    sumPixel = sumKernel = 0;
                    for (i = 0; i < kWidth; i++)
                    {
                        for (j = 0; j < kHeight; j++)
                        {
                            kernelValue = kernel[i, j];
                            if (kernelValue != 0)
                            {
                                sumPixel  += inputBytes[x + i, y + j] * kernelValue;
                                sumKernel += kernelValue;
                            }
                        }
                    }
                    if (sumKernel <= 0)
                    {
                        sumKernel = 1;
                    }

                    sumPixel /= sumKernel;
                    if (sumPixel < 0)
                    {
                        sumPixel = 0;
                    }
                    else if (sumPixel > 255)
                    {
                        sumPixel = 255;
                    }

                    outputBytes[x, y] = (byte)sumPixel;
                }
            }
            if (monochrome)
            {
                return(BitmapBytesConverter.Get1BppBitmap(outputBytes));
            }
            return(BitmapBytesConverter.GetGrayscale8BppBitmap(outputBytes));
        }
Beispiel #2
0
        public static Bitmap GetBorderMask(Point[] points, int width, int height)
        {
            byte[,] outputBytes = new byte[width, height];

            int length = points.Length;

            for (int line = 0; line < length; line++)
            {
                outputBytes[points[line].X, points[line].Y] = 255;
            }

            return(BitmapBytesConverter.GetGrayscale8BppBitmap(outputBytes));
        }