protected abstract Rectangle GetBoundingBox(Image <TPixel> image, Rectangle rectangle, IBorderAnalysis borderAnalysis, int colorThreshold);
        protected override Rectangle GetBoundingBox(Image <Rgb24> image, Rectangle rectangle, IBorderAnalysis borderAnalysis, int colorThreshold)
        {
            var w = rectangle.Right;
            var h = rectangle.Bottom;

            var backgroundColor = borderAnalysis.Background.ToPixel <Rgb24>();

            // Calculated x-min
            var xn = w;

            // Calculated x-max
            var xm = rectangle.X;

            // Calculated y-min
            var yn = h;

            // Calculated y-max
            var ym = rectangle.Y;

            for (var y = rectangle.Y; y < h; y++)
            {
                var row = image.GetPixelRowSpan(y);

                for (var x = rectangle.X; x < w; x++)
                {
                    // Current pixel
                    var c = row[x];

                    // Delta color values
                    var bd = Math.Abs(c.B - backgroundColor.B);
                    var gd = Math.Abs(c.G - backgroundColor.G);
                    var rd = Math.Abs(c.R - backgroundColor.R);

                    // Grayscale operation on color delta
                    // This is done to properly evaluate if
                    // the perceptual differences are above threshold

                    if (0.299 * rd + 0.587 * gd + 0.114 * bd <= colorThreshold)
                    {
                        continue;
                    }

                    if (x < xn)
                    {
                        xn = x;
                    }
                    if (x > xm)
                    {
                        xm = x;
                    }
                    if (y < yn)
                    {
                        yn = y;
                    }
                    if (y > ym)
                    {
                        ym = y;
                    }
                }
            }

            return(new Rectangle(xn, yn, xm - xn + 1, ym - yn + 1));
        }
示例#3
0
        protected override Rectangle GetBoundingBox(Image <Rgba32> image, Rectangle rectangle, IBorderAnalysis borderAnalysis, int colorThreshold)
        {
            var h = rectangle.Bottom;
            var w = rectangle.Right;

            var backgroundColor = borderAnalysis.Background.ToPixel <Rgba32>();

            var xn = w;
            var xm = rectangle.X;
            var yn = h;
            var ym = rectangle.Y;

            for (var y = rectangle.Y; y < h; y++)
            {
                var row = image.GetPixelRowSpan(y);

                for (var x = rectangle.X; x < w; x++)
                {
                    var c  = row[x];
                    var ac = Math.Max(c.A * 0.003921568627451, 1);

                    var bd = Math.Abs(c.B - backgroundColor.B) * ac;
                    var gd = Math.Abs(c.G - backgroundColor.G) * ac;
                    var rd = Math.Abs(c.R - backgroundColor.R) * ac;

                    if (0.299 * rd + 0.587 * gd + 0.114 * bd <= colorThreshold)
                    {
                        var ad = Math.Abs(c.A - backgroundColor.A);
                        if (ad < colorThreshold)
                        {
                            continue;
                        }
                    }

                    if (x < xn)
                    {
                        xn = x;
                    }
                    if (x > xm)
                    {
                        xm = x;
                    }
                    if (y < yn)
                    {
                        yn = y;
                    }
                    if (y > ym)
                    {
                        ym = y;
                    }
                }
            }

            return(new Rectangle(xn, yn, xm - xn + 1, ym - yn + 1));
        }