c#实现识别图片上的验证码数字

2019-12-30 11:03:40刘景俊
  • }  } 
  • btp.Save("d:去除相关颜色.png");   
  • pictureBox2.Image = Image.FromFile("d:去除相关颜色.png");   
  •   //灰度 
  • Bitmap bmphd = btp;  for (int i = 0; i < bmphd.Width; i++) 
  • {  for (int j = 0; j < bmphd.Height; j++) 
  • {  //取图片当前的像素点 
  • var color = bmphd.GetPixel(i, j);   
  • var gray = (int)(color.R * 0.001 + color.G * 0.700 + color.B * 0.250);   
  • //重新设置当前的像素点  bmphd.SetPixel(i, j, Color.FromArgb(gray, gray, gray)); 
  • }  } 
  • bmphd.Save("d:灰度.png");  pictureBox27.Image = Image.FromFile("d:灰度.png"); 
  •    
  • //二值化  Bitmap erzhi = bmphd; 
  • Bitmap orcbmp;  int nn = 3; 
  • int w = erzhi.Width;  int h = erzhi.Height; 
  • BitmapData data = erzhi.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);  unsafe 
  • {  byte* p = (byte*)data.Scan0; 
  • byte[,] vSource = new byte[w, h];  int offset = data.Stride - w * nn; 
  •   for (int y = 0; y < h; y++) 
  • {  for (int x = 0; x < w; x++) 
  • {  vSource[x, y] = (byte)(((int)p[0] + (int)p[1] + (int)p[2]) / 3); 
  • p += nn;  } 
  • p += offset;  } 
  • erzhi.UnlockBits(data);   
  • Bitmap bmpDest = new Bitmap(w, h, PixelFormat.Format24bppRgb);  BitmapData dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); 
  • p = (byte*)dataDest.Scan0;  offset = dataDest.Stride - w * nn;