public float[,] Itegralize()
        {
            var img = new LockBitmap(_image);
            img.LockBits();

            var result = new float[img.Width, img.Height];

            for (var i = 0; i < img.Width; i++)
            {
                for (var j = 0; j < img.Height; j++)
                {
                    result[i, j] = img.GetPixel(i, j).GetBrightness();
                    if (j > 0)
                    {
                        result[i, j] += result[i, j - 1];
                    }
                    if (i > 0)
                    {
                        result[i, j] += result[i - 1, j];
                    }

                    if (i > 0 && j > 0)
                    {
                        result[i, j] -= result[i - 1, j - 1];
                    }
                }
            }

            return result;
        }
Esempio n. 2
0
        public Bitmap ApplyFilter(Bitmap image, IFilterParameters filterParameters)
        {
            matrixFilterParameters = filterParameters as MatrixFilterParameters;
            if (matrixFilterParameters == null)
            {
                throw new ArgumentException();
            }

            var destImage = new Bitmap(image);

            resultImage = new LockBitmap(destImage);
            resultImage.LockBits();

            sourceImage = new LockBitmap(image);
            sourceImage.LockBits();

            matrixLength = Convert.ToInt32(Math.Sqrt(matrixFilterParameters.Matrix.Length));
            Parallel.For(0, sourceImage.Width, x =>
            {
                for (int y = 0; y < sourceImage.Height; y++)
                {
                    Wrap(x, y);
                }
            });
            resultImage.UnlockBits();
            sourceImage.UnlockBits();
            return destImage;
        }
Esempio n. 3
0
        public Bitmap ApplyFilter(Bitmap image, IFilterParameters filterParameters)
        {
            var param = filterParameters as GausianFilterParameters;
            if (param == null)
            {
                throw new ArgumentException("Parameters object must implement IFilterParameters");
            }
            var windowSize = (int)Math.Ceiling(param.Sigma * 3);
            var window = InitializeWindow(param.Sigma);

            var img = new LockBitmap(image);
            var dest = new Bitmap(image);
            img.LockBits();
            var destImage = new LockBitmap(dest);
            destImage.LockBits();

            Parallel.For(0, img.Width, x =>
            {
                Color[] tmp = new Color[img.Height];
                for (var y = 0; y < img.Height; y++)
                {
                    var sum = 0.0;
                    var r = 0.0;
                    var g = 0.0;
                    var b = 0.0;
                    for (var i = 0; i < window.Length; i++)
                    {
                        var l = x + i - windowSize;
                        if (l >= 0 && l < img.Width)
                        {
                            var sourceColor = img.GetPixel(l, y);
                            r += sourceColor.R * window[i];
                            g += sourceColor.G * window[i];
                            b += sourceColor.B * window[i];
                            sum += window[i];
                        }
                    }
                    tmp[y] = Color.FromArgb((r / sum).GetValidColorComponent(), (g / sum).GetValidColorComponent(),
                        (b / sum).GetValidColorComponent());
                }
                for (var i = 0; i < img.Height; i++)
                {
                    destImage.SetPixel(x, i, tmp[i]);
                }
            });

            Parallel.For(0, img.Height, y =>
            {
                Color[] tmp = new Color[img.Width];
                for (var x = 0; x < img.Width; x++)
                {
                    var sum = 0.0;
                    var r = 0.0;
                    var g = 0.0;
                    var b = 0.0;
                    for (var i = 0; i < window.Length; i++)
                    {
                        var l = y + i - windowSize;
                        if (l >= 0 && l < img.Height)
                        {
                            var sourceColor = destImage.GetPixel(x, l);
                            r += sourceColor.R * window[i];
                            g += sourceColor.G * window[i];
                            b += sourceColor.B * window[i];
                            sum += window[i];
                        }
                    }
                    tmp[x] = Color.FromArgb((r / sum).GetValidColorComponent(), (g / sum).GetValidColorComponent(),
                        (b / sum).GetValidColorComponent());
                }
                for (var i = 0; i < img.Width; i++)
                {
                    img.SetPixel(i, y, tmp[i]);
                }
            });
            img.UnlockBits();
            destImage.UnlockBits();
            return image;
        }