AForge.Imaging.UnmanagedImage.Collect8bppPixelValues C# (CSharp) Method

Collect8bppPixelValues() public method

Collect pixel values from the specified list of coordinates.

The method goes through the specified list of points and for each point retrievs corresponding pixel's value from the unmanaged image.

For grayscale image the output array has the same length as number of points in the specified list of points. For color image the output array has triple length, containing pixels' values in RGB order.

The method does not make any checks for valid coordinates and leaves this up to user. If specified coordinates are out of image's bounds, the result is not predictable (crash in most cases).

This method is supposed for images with 8 bpp channels only (8 bpp grayscale image and 24/32 bpp color images).

Unsupported pixel format of the source image. Use Collect16bppPixelValues() method for /// images with 16 bpp channels.
public Collect8bppPixelValues ( List points ) : byte[]
points List List of coordinates to collect pixels' value from.
return byte[]
        public byte[] Collect8bppPixelValues( List<IntPoint> points )
        {
            int pixelSize = Bitmap.GetPixelFormatSize( pixelFormat ) / 8;

            if ( ( pixelFormat == PixelFormat.Format16bppGrayScale ) || ( pixelSize > 4 ) )
            {
                throw new UnsupportedImageFormatException( "Unsupported pixel format of the source image. Use Collect16bppPixelValues() method for it." );
            }

            byte[] pixelValues = new byte[points.Count * ( ( pixelFormat == PixelFormat.Format8bppIndexed ) ? 1 : 3 )];

            unsafe
            {
                byte* basePtr = (byte*) imageData.ToPointer( );
                byte* ptr;

                if ( pixelFormat == PixelFormat.Format8bppIndexed )
                {
                    int i = 0;

                    foreach ( IntPoint point in points )
                    {
                        ptr = basePtr + stride * point.Y + point.X;
                        pixelValues[i++] = *ptr;
                    }
                }
                else
                {
                    int i = 0;

                    foreach ( IntPoint point in points )
                    {
                        ptr = basePtr + stride * point.Y + point.X * pixelSize;
                        pixelValues[i++] = ptr[RGB.R];
                        pixelValues[i++] = ptr[RGB.G];
                        pixelValues[i++] = ptr[RGB.B];
                    }
                }
            }

            return pixelValues;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="red">A grayscale image filtered using a redness algorithm</param>
        /// <param name="startAt">The maximum brightness point, the place to start filling</param>
        /// <param name="origin">The origin of the search, usually the click</param>
        /// <param name="maxRadius">The maximum distance from 'startAt' to consider filling</param>
        public AdaptiveCircleFill(UnmanagedImage red, System.Drawing.Point startAt, PointF origin, float maxRadius)
        {
            this.red = red;
            this.StartAt = startAt;
            MaxValue = red.Collect8bppPixelValues(new List<AForge.IntPoint>(new AForge.IntPoint[] { new AForge.IntPoint(startAt.X, startAt.Y) }))[0];
            //Set the min threshold to 4/10ths the starting point's value
            MinValue = (byte)Math.Round(0.4 * (double)MaxValue);
            MinValue = Math.Max((byte)50, MinValue);
            //Apply some arbitrary values...

            this.Origin = origin;
            this.MaxRadius = maxRadius;

            OriginStartOffset = Math.Sqrt((origin.X - StartAt.X) * (origin.X - StartAt.X) + (origin.Y - StartAt.Y) * (origin.Y - StartAt.Y));
        }
All Usage Examples Of AForge.Imaging.UnmanagedImage::Collect8bppPixelValues