Accord.Imaging.Filters.Kuwahara.ProcessFilter C# (CSharp) Method

ProcessFilter() protected method

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage image ) : void
image Accord.Imaging.UnmanagedImage Source image data.
return void
        protected unsafe override void ProcessFilter(UnmanagedImage image)
        {
            int width = image.Width;
            int height = image.Height;

            int stride = image.Stride;
            int offset = stride - width;


            int b = blockSize;
            int blocksX = width - b;
            int blocksY = height - b;
            double count = b * b;

            double[,] mean = new double[blocksY, blocksX];
            double[,] var = new double[blocksY, blocksX];

            byte* src = (byte*)image.ImageData.ToPointer();
            for (int y = 0; y < height - b; y++)
            {
                for (int x = 0; x < width - b; x++, src++)
                {
                    mean[y, x] = UnsafeTools.Sum(src, b, b, stride) / count;
                    var[y, x] = UnsafeTools.Scatter(src, b, b, stride, mean[y, x]);
                }
                src += offset + b;
            }


            src = (byte*)image.ImageData.ToPointer() + b * stride + b;
            for (int y = b; y < height - b - 1; y++)
            {
                for (int x = b; x < width - b - 1; x++, src++)
                {
                    // variances
                    double va = var[y - b, x - b], vb = var[y - b, x + 1];
                    double vc = var[y + 1, x - b], vd = var[y + 1, x + 1];

                    // means
                    double ma = mean[y - b, x - b], mb = mean[y - b, x + 1];
                    double mc = mean[y + 1, x - b], md = mean[y + 1, x + 1];

                    double value = min(va, vb, vc, vd,
                                       ma, mb, mc, md);

                    *src = (byte)(value > 255 ? 255 : (value < 0 ? 0 : value));
                }

                src += offset + 2 * b + 1;
            }
        }