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[]
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));
        }
        /// <summary>
        /// Calculate average brightness difference between pixels outside and inside of the object
        /// bounded by specified left and right edge 
        /// </summary>
        /// <param name="leftEdgePoints">Left edge point</param>
        /// <param name="rightEdgePoints">Right edge point</param>
        /// <param name="image">Image</param>
        /// <returns>Average brightness.</returns>
        private float CalculateAverageEdgesBrightnessDifference( List<IntPoint> leftEdgePoints, List<IntPoint> rightEdgePoints, UnmanagedImage image )
        {
            // create list of points, which are a bit on the left/right from edges
            List<IntPoint> leftEdgePoints1  = new List<IntPoint>( );
            List<IntPoint> leftEdgePoints2  = new List<IntPoint>( );
            List<IntPoint> rightEdgePoints1 = new List<IntPoint>( );
            List<IntPoint> rightEdgePoints2 = new List<IntPoint>( );

            int tx1, tx2, ty;
            int widthM1 = image.Width - 1;

            for ( int k = 0; k < leftEdgePoints.Count; k++ )
            {
                tx1 = leftEdgePoints[k].X - STEP_SIZE;
                tx2 = leftEdgePoints[k].X + STEP_SIZE;
                ty = leftEdgePoints[k].Y;

                leftEdgePoints1.Add( new IntPoint( ( tx1 < 0 ) ? 0 : tx1, ty ) );
                leftEdgePoints2.Add( new IntPoint( ( tx2 > widthM1 ) ? widthM1 : tx2, ty ) );

                tx1 = rightEdgePoints[k].X - STEP_SIZE;
                tx2 = rightEdgePoints[k].X + STEP_SIZE;
                ty = rightEdgePoints[k].Y;

                rightEdgePoints1.Add( new IntPoint( ( tx1 < 0 ) ? 0 : tx1, ty ) );
                rightEdgePoints2.Add( new IntPoint( ( tx2 > widthM1 ) ? widthM1 : tx2, ty ) );
            }

            // collect pixel values from specified points
            byte[] leftValues1  = image.Collect8bppPixelValues( leftEdgePoints1 );
            byte[] leftValues2  = image.Collect8bppPixelValues( leftEdgePoints2 );
            byte[] rightValues1 = image.Collect8bppPixelValues( rightEdgePoints1 );
            byte[] rightValues2 = image.Collect8bppPixelValues( rightEdgePoints2 );

            // calculate average difference between pixel values from outside of the shape and from inside
            float diff = 0;
            int pixelCount = 0;

            for ( int k = 0; k < leftEdgePoints.Count; k++ )
            {
                if ( rightEdgePoints[k].X - leftEdgePoints[k].X > STEP_SIZE * 2 )
                {
                    diff += ( leftValues1[k]  - leftValues2[k] );
                    diff += ( rightValues2[k] - rightValues1[k] );
                    pixelCount += 2;
                }
            }

            return diff / pixelCount;
        }
        private float calculateAverageEdgesBrightnessDifference(List<IntPoint> leftEdgePoints, List<IntPoint> rightEdgePoints, UnmanagedImage image)
        {
            List<IntPoint> leftEdgePoints1 = new List<IntPoint>();
            List<IntPoint> leftEdgePoints2 = new List<IntPoint>();
            List<IntPoint> rightEdgePoints1 = new List<IntPoint>();
            List<IntPoint> rightEdgePoints2 = new List<IntPoint>();

            int tx1, tx2, ty;
            int widthM1 = image.Width - 1;

            for (int k = 0; k < leftEdgePoints.Count; k++)
            {
                tx1 = leftEdgePoints[k].X - stepSize;
                tx2 = leftEdgePoints[k].X + stepSize;
                ty = leftEdgePoints[k].Y;

                leftEdgePoints1.Add(new IntPoint((tx1 < 0) ? 0 : tx1, ty));
                leftEdgePoints2.Add(new IntPoint((tx2 > widthM1) ? widthM1 : tx2, ty));

                tx1 = rightEdgePoints[k].X - stepSize;
                tx2 = rightEdgePoints[k].X + stepSize;
                ty = rightEdgePoints[k].Y;

                rightEdgePoints1.Add(new IntPoint((tx1 < 0) ? 0 : tx1, ty));
                rightEdgePoints2.Add(new IntPoint((tx2 > widthM1) ? widthM1 : tx2, ty));
            }

            byte[] leftValues1 = image.Collect8bppPixelValues(leftEdgePoints1);
            byte[] leftValues2 = image.Collect8bppPixelValues(leftEdgePoints2);
            byte[] rightValues1 = image.Collect8bppPixelValues(rightEdgePoints1);
            byte[] rightValues2 = image.Collect8bppPixelValues(rightEdgePoints2);

            float diff = 0;
            int pixelCount = 0;

            for (int k = 0; k < leftEdgePoints.Count; k++)
            {
                if (rightEdgePoints[k].X - leftEdgePoints[k].X > stepSize * 2)
                {
                    diff += (leftValues1[k] - leftValues2[k]);
                    diff += (rightValues2[k] - rightValues1[k]);
                    pixelCount += 2;
                }
            }

            return diff / pixelCount;
        }