public Bitmap Convert(Bitmap bitmap, int threshold, int range) { var gray = new Bitmap(bitmap.Width, bitmap.Height); var max = 3 * 255; for (int iY = 0; iY < bitmap.Height; iY++) { for (int iX = 0; iX < bitmap.Width; iX++) { var pixel = bitmap.GetPixel(iX, iY); //int sum = pixel.R + pixel.G + pixel.B; int sum = ImageToGraphHelper.GrayScale(pixel); //gray.SetPixel(iX, iY, Color.FromArgb(255, res, res, res)); //continue; if (sum > ImageToGraphHelper.Normalize(threshold - range, max) && sum < ImageToGraphHelper.Normalize(threshold + range, max)) { gray.SetPixel(iX, iY, Color.Black); continue; } if (sum < ImageToGraphHelper.Normalize(threshold - range, max) || sum > ImageToGraphHelper.Normalize(threshold + range, max)) { gray.SetPixel(iX, iY, Color.Gray); continue; } gray.SetPixel(iX, iY, Color.White); } } return(gray); }
public DrawPointItem ScanToNextPossiblePoint(Bitmap bitmap, int iY, int iX, int threshold, int range) { var rad = range / 2; if (rad == 0) { rad = 1; } var collect = new List <DrawPointItem>(); for (int iRangeY = iY - rad; iRangeY < iY + rad; iRangeY++) { for (int iRangeX = iX - rad; iRangeX < iX + rad; iRangeX++) { int targetY = iRangeY; int targetX = iRangeX; //bool isMinOutRangeY = false; //bool isMaxOutRangeY = false; //bool isMinOutRangeX = false; //bool isMaxOutRangeX = false; if (targetY < 0) { //isMinOutRangeY = true; targetY = 0; } if (targetY >= bitmap.Height) { //isMaxOutRangeY = true; targetY = bitmap.Height - 1; } if (targetX < 0) { //isMinOutRangeX = true; targetX = 0; } if (targetX >= bitmap.Width) { //isMaxOutRangeX = true; targetX = bitmap.Width - 1; } //if (isMinOutRangeY || isMaxOutRangeY) //{ //} //else //{ // targetY = iRangeY; //} //if(isMinOutRangeX || isMaxOutRangeX) //{ //} //else //{ // targetX = iRangeX; //} var t = ImageToGraphHelper.GrayScale(bitmap.GetPixel(targetX, targetY)); collect.Add(new DrawPointItem(targetX, targetY, t)); } } var max = collect.Min(m => m.GrayScaleValue); if (collect.All(a => a.GrayScaleValue == max)) { var t = ImageToGraphHelper.GrayScale(bitmap.GetPixel(iX, iY)); return(new DrawPointItem(iX, iY, t)); } var result = collect.First(f => f.GrayScaleValue <= max); return(result); }