Пример #1
0
        public static Bitmap FilterSharpen7(Bitmap image)
        {
            ConMatrix matr = new ConMatrix();

            matr.Size = 7;

            matr.Matrix = new int[7, 7]
            {
                { 0, 0, 0, -2, -2, 0, 0 },
                { 0, 0, 0, -2, -2, 0, 0 },
                { -2, -2, 0, -2, 0, 0, 0 },
                { -2, -2, -2, 11, -2, -2, -2 },
                { 0, 0, 0, -2, 0, -2, -2 },
                { 0, 0, -2, -2, 0, 0, 0 },
                { 0, 0, -2, -2, 0, 0, 0 }
            };
            return(Sharpen.ImgConvolution(image, matr));
        }
Пример #2
0
        private static Bitmap ImgConvolution(Bitmap image, ConMatrix fmat)
        {
            //Avoid division by 0
            if (fmat.Factor == 0)
            {
                return(null);
            }


            Bitmap srcImage = (Bitmap)image.Clone();

            int x, y, filterx, filtery, tempx, tempy;
            int s = fmat.Size / 2;

            int r, g, b, tr, tg, tb;

            int pixelSize = GetPixelSize(image);

            BitmapData imageData    = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, image.PixelFormat);
            BitmapData srcImageData = srcImage.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);

            // Scan0 is the memory address where pixel-array begins.
            // Stride is the width of each row of pixels.
            int    stride = srcImageData.Stride;
            IntPtr scan0  = srcImageData.Scan0;

            unsafe
            {
                byte *tempPixel;

                for (y = s; y < srcImageData.Height - s; y++)
                {
                    for (x = s; x < srcImageData.Width - s; x++)
                    {
                        r = g = b = 0;

                        //Convolution
                        for (filtery = 0; filtery < fmat.Size; filtery++)
                        {
                            for (filterx = 0; filterx < fmat.Size; filterx++)
                            {
                                // Get nearby pixel's position
                                tempx = x + filterx - s;
                                tempy = y + filtery - s;

                                // Go to that pixel in pixel-array
                                tempPixel = (byte *)scan0 + (tempy * stride) + (tempx * pixelSize);

                                // The format is BGRA (1 byte each). Get em
                                tb = (int)*tempPixel;
                                tg = (int)*(tempPixel + 1);
                                tr = (int)*(tempPixel + 2);

                                r += fmat.Matrix[filtery, filterx] * tr;
                                g += fmat.Matrix[filtery, filterx] * tg;
                                b += fmat.Matrix[filtery, filterx] * tb;
                            }
                        }

                        // Remove values out of [0,255]
                        r = Math.Min(Math.Max((r / fmat.Factor) + fmat.Offset, 0), 255);
                        g = Math.Min(Math.Max((g / fmat.Factor) + fmat.Offset, 0), 255);
                        b = Math.Min(Math.Max((b / fmat.Factor) + fmat.Offset, 0), 255);

                        // Compute new pixel position (in new image) and write the pixels (BGRA format)
                        byte *newpixel = (byte *)imageData.Scan0 + (y * imageData.Stride) + (x * pixelSize);
                        *     newpixel = (byte)b;
                        *(newpixel + 1) = (byte)g;
                        *(newpixel + 2) = (byte)r;
                        // If there is Alpha value
                        if (HasAlpha(image))
                        {
                            *(newpixel + 3) = 255;
                        }
                    }
                }
            }

            image.UnlockBits(imageData);
            srcImage.UnlockBits(srcImageData);

            return(image);
        }