Accord.Imaging.GrayLevelDifferenceMethod.Compute C# (CSharp) Method

Compute() public method

Computes the Gray-level Difference Method (GLDM) Histogram for the given source image.
public Compute ( UnmanagedImage source ) : int[]
source UnmanagedImage The source image.
return int[]
        public int[] Compute(UnmanagedImage source)
        {
            int width = source.Width;
            int height = source.Height;
            int stride = source.Stride;
            int offset = stride - width;
            int maxGray = 255;

            int[] hist;

            unsafe
            {
                byte* src = (byte*)source.ImageData.ToPointer();

                if (autoGray)
                    maxGray = max(width, height, offset, src);


                hist = new int[maxGray + 1];

                switch (degree)
                {
                    case CooccurrenceDegree.Degree0:
                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 1; x < width; x++)
                            {
                                byte a = src[stride * y + (x - 1)];
                                byte b = src[stride * y + x];
                                int bin = Math.Abs(a - b);
                                hist[bin]++;
                            }
                        }
                        break;

                    case CooccurrenceDegree.Degree45:
                        for (int y = 1; y < height; y++)
                        {
                            for (int x = 0; x < width - 1; x++)
                            {
                                byte a = src[stride * y + x];
                                byte b = src[stride * (y - 1) + (x + 1)];
                                int bin = Math.Abs(a - b);
                                hist[bin]++;
                            }
                        }
                        break;

                    case CooccurrenceDegree.Degree90:
                        for (int y = 1; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                byte a = src[stride * (y - 1) + x];
                                byte b = src[stride * y + x];
                                int bin = Math.Abs(a - b);
                                hist[bin]++;
                            }
                        }
                        break;

                    case CooccurrenceDegree.Degree135:
                        for (int y = 1; y < height; y++)
                        {
                            int steps = width - 1;
                            for (int x = 0; x < width - 1; x++)
                            {
                                byte a = src[stride * y + (steps - x)];
                                byte b = src[stride * (y - 1) + (steps - 1 - x)];
                                int bin = Math.Abs(a - b);
                                hist[bin]++;
                            }
                        }
                        break;
                }
            }

            return hist;
        }

Usage Example

        /// <summary>
        ///  Gray-Level Difference Method (GLDM).
        ///  <para>Computes an gray-level histogram of difference values between adjacent pixels in an image.</para>
        ///  <para>Accord.NET internal call. Please see: <see cref="Accord.Imaging.GrayLevelDifferenceMethod">Gray-Level Difference Method</see> for details.</para>
        /// </summary>
        /// <param name="image">The source image.</param>
        /// <param name="autoGray">Whether the maximum value of gray should be automatically computed from the image. </param>
        /// <param name="degree">The direction at which the co-occurrence should be found.</param>
        /// <returns>An histogram containing co-occurrences for every gray level in <paramref name="image"/>.</returns>
        public static int[] GrayLevelDifferenceMethod(this Image<Gray, byte> image, CooccurrenceDegree degree, bool autoGray = true)
        {
            GrayLevelDifferenceMethod gldm = new GrayLevelDifferenceMethod(degree, autoGray);
            var hist = gldm.Compute(image.ToAForgeImage(copyAlways: false, failIfCannotCast: true));

            return hist;
        }
All Usage Examples Of Accord.Imaging.GrayLevelDifferenceMethod::Compute