public static Bgr DetectBackground(Bitmap document, int border = 10)
{
double border2 = (double)border / 100f;
System.Drawing.Point tl = new System.Drawing.Point((int)(border2 * document.Width), (int)(border2 * document.Height));
System.Drawing.Point br = new System.Drawing.Point((int)(document.Width - (border2 * document.Width)), (int)(document.Height - (border2 * document.Height)));
Rectangle rect = new Rectangle(tl.X, tl.Y, br.X - tl.X, br.Y - tl.Y);
UnmanagedImage blackened = UnmanagedImage.FromManagedImage(document);
AForge.Imaging.Drawing.FillRectangle(blackened, rect, Color.Black);
Bitmap blacknew = blackened.ToManagedImage();
AForge.Imaging.ImageStatistics stat = new ImageStatistics(blacknew);
Histogram red = stat.RedWithoutBlack;
Histogram green = stat.GreenWithoutBlack;
Histogram blue = stat.BlueWithoutBlack;
int indexR = (int)red.Median;
int indexB = (int)blue.Median;
int indexG = (int)green.Median;
Emgu.CV.Image<Bgra, Byte> blackcv = new Image<Bgra, byte>(blacknew);
return new Bgr((double)indexB, (double)indexG, (double)indexR);
}