Accord.Imaging.Tools.Max C# (CSharp) Method

Max() public static method

Computes the maximum pixel value in the given image.
public static Max ( this image ) : int
image this
return int
        public static int Max(this UnmanagedImage image)
        {
            if ((image.PixelFormat != PixelFormat.Format8bppIndexed) &&
                (image.PixelFormat != PixelFormat.Format16bppGrayScale))
                throw new UnsupportedImageFormatException("Only grayscale images are supported");

            int width = image.Width;
            int height = image.Height;
            int stride = image.Stride;

            int max = 0;

            unsafe
            {
                if (image.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    byte* src = (byte*)image.ImageData.ToPointer();

                    for (int y = 0; y < height; y++)
                        for (int x = 0; x < width; x++, src++)
                            if (*src > max) max = *src;
                }
                else
                {
                    ushort* src = (ushort*)image.ImageData.ToPointer();

                    for (int y = 0; y < height; y++)
                        for (int x = 0; x < width; x++, src++)
                            if (*src > max) max = *src;
                }
            }

            return max;
        }

Same methods

Tools::Max ( this image, Rectangle rectangle ) : int
Tools::Max ( this image, int channel ) : int

Usage Example

コード例 #1
0
        public void MaxTest2()
        {
            Bitmap image = new byte[, ]
            {
                { 5, 2, 7 },
                { 5, 3, 5 },
                { 9, 1, 2 }
            }.ToBitmap();

            {
                Rectangle rectangle = new Rectangle(1, 0, 2, 2);
                int       expected  = 7;
                int       actual    = Tools.Max(image, rectangle);
                Assert.AreEqual(expected, actual);
            }
            {
                int expected = 9;
                int actual   = Tools.Max(image);
                Assert.AreEqual(expected, actual);
            }
        }