Exemple #1
0
        /// <summary>
        /// Construct integral image from source grayscale image.
        /// </summary>
        ///
        /// <param name="image">Source unmanaged image.</param>
        ///
        /// <returns>Returns integral image.</returns>
        ///
        /// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
        ///
        public static IntegralImage FromBitmap(UnmanagedImage image)
        {
            // check image format
            if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new ArgumentException("Source image can be graysclae (8 bpp indexed) image only.");
            }

            // get source image size
            int width  = image.Width;
            int height = image.Height;
            int offset = image.Stride - width;

            // create integral image
            IntegralImage im = new IntegralImage(width, height);

            uint[,] integralImage = im.integralImage;

            // do the job
            unsafe
            {
                byte *src = (byte *)image.ImageData.ToPointer( );

                // for each line
                for (int y = 1; y <= height; y++)
                {
                    uint rowSum = 0;

                    // for each pixel
                    for (int x = 1; x <= width; x++, src++)
                    {
                        rowSum += *src;

                        integralImage[y, x] = rowSum + integralImage[y - 1, x];
                    }
                    src += offset;
                }
            }

            return(im);
        }
Exemple #2
0
        /// <summary>
        /// Construct integral image from source grayscale image.
        /// </summary>
        ///
        /// <param name="image">Source grayscale image.</param>
        ///
        /// <returns>Returns integral image.</returns>
        ///
        /// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
        ///
        public static IntegralImage FromBitmap(Bitmap image)
        {
            // check image format
            if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new UnsupportedImageFormatException("Source image can be graysclae (8 bpp indexed) image only.");
            }

            // lock source image
            BitmapData imageData = image.LockBits(
                new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // process the image
            IntegralImage im = FromBitmap(imageData);

            // unlock image
            image.UnlockBits(imageData);

            return(im);
        }