Exemplo n.º 1
0
        public Bitmap Apply(BitmapData imageData)
        {
            if (!FormatTranslations.ContainsKey(imageData.PixelFormat))
            {
                throw new UnsupportedImageFormatException("Source pixel format is not supported by the filter.");
            }
            BlobCounter blobCounter = new BlobCounter(imageData);

            Blob[] objectsInformation = blobCounter.GetObjectsInformation();
            int    num  = 0;
            Blob   blob = null;
            int    i    = 0;

            for (int num2 = objectsInformation.Length; i < num2; i++)
            {
                int num3 = objectsInformation[i].Rectangle.Width * objectsInformation[i].Rectangle.Height;
                if (num3 > num)
                {
                    num  = num3;
                    blob = objectsInformation[i];
                }
            }
            if (blob == null)
            {
                throw new ArgumentException("The source image does not contain any blobs.");
            }
            blobPosition = new IntPoint(blob.Rectangle.Left, blob.Rectangle.Top);
            if (originalImage == null)
            {
                blobCounter.ExtractBlobsImage(new UnmanagedImage(imageData), blob, extractInOriginalSize: false);
            }
            else
            {
                if (originalImage.PixelFormat != PixelFormat.Format24bppRgb && originalImage.PixelFormat != PixelFormat.Format32bppArgb && originalImage.PixelFormat != PixelFormat.Format32bppRgb && originalImage.PixelFormat != PixelFormat.Format32bppPArgb && originalImage.PixelFormat != PixelFormat.Format8bppIndexed)
                {
                    throw new UnsupportedImageFormatException("Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only.");
                }
                if (originalImage.Width != imageData.Width || originalImage.Height != imageData.Height)
                {
                    throw new InvalidImagePropertiesException("Original image must have the same size as passed source image.");
                }
                blobCounter.ExtractBlobsImage(originalImage, blob, extractInOriginalSize: false);
            }
            Bitmap result = blob.Image.ToManagedImage();

            blob.Image.Dispose();
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Apply filter to an image.
        /// </summary>
        ///
        /// <param name="imageData">Source image to get biggest blob from.</param>
        ///
        /// <returns>Returns image of the biggest blob.</returns>
        ///
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception>
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the original image.</exception>
        /// <exception cref="InvalidImagePropertiesException">Source and original images must have the same size.</exception>
        /// <exception cref="ArgumentException">The source image does not contain any blobs.</exception>
        ///
        public Bitmap Apply(BitmapData imageData)
        {
            // check pixel format of the source image
            if (!FormatTranslations.ContainsKey(imageData.PixelFormat))
            {
                throw new UnsupportedImageFormatException("Source pixel format is not supported by the filter.");
            }

            // locate blobs in the source image
            var blobCounter = new BlobCounter(imageData);
            // get information about blobs
            var blobs = blobCounter.GetObjectsInformation();
            // find the biggest blob
            var  maxSize     = 0;
            Blob biggestBlob = null;

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                var size = blobs[i].Rectangle.Width * blobs[i].Rectangle.Height;

                if (size > maxSize)
                {
                    maxSize     = size;
                    biggestBlob = blobs[i];
                }
            }

            // check if any blob was found
            if (biggestBlob == null)
            {
                throw new ArgumentException("The source image does not contain any blobs.");
            }

            blobPosition = new IntPoint(biggestBlob.Rectangle.Left, biggestBlob.Rectangle.Top);

            // extract biggest blob's image
            if (originalImage == null)
            {
                blobCounter.ExtractBlobsImage(new UnmanagedImage(imageData), biggestBlob, false);
            }
            else
            {
                // check original image's format
                if (
                    (originalImage.PixelFormat != PixelFormat.Format24bppRgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format32bppArgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format32bppRgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format32bppPArgb) &&
                    (originalImage.PixelFormat != PixelFormat.Format8bppIndexed)
                    )
                {
                    throw new UnsupportedImageFormatException("Original image may be grayscale (8bpp indexed) or color (24/32bpp) image only.");
                }

                // check its size
                if ((originalImage.Width != imageData.Width) || (originalImage.Height != imageData.Height))
                {
                    throw new InvalidImagePropertiesException("Original image must have the same size as passed source image.");
                }

                blobCounter.ExtractBlobsImage(originalImage, biggestBlob, false);
            }

            var managedImage = biggestBlob.Image.ToManagedImage();

            // dispose unmanaged image of the biggest blob
            biggestBlob.Image.Dispose();

            return(managedImage);
        }