验证码识别常用算法图像处理(验证码识别)程序中常用算法:灰度,二值化,去噪(1*1像素或者3*3像素等)代码:view plaincopy to clipboardprint?//灰度private void btnGray_Click(object sender, EventArgs e){try{int Height = this.picBase.Image.Height;int Width = this.picBase.Image.Width;Bitmap newbitmap = new Bitmap(Width, Height);Bitmap oldbitmap = (Bitmap)this.picBase.Image;Color pixel;for (int x = 0; x < Width; x++){for (int y = 0; y < Height; y++){pixel = oldbitmap.GetPixel(x, y);newbitmap.SetPixel(x, y, Gray(pixel));}}this.picBase.Image = newbitmap;}catch (Exception err){MessageBox.Show("灰度化失败原因:" + err.Message);}}//灰度化算法protected static Color Gray(Color c){int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));return Color.FromArgb(rgb, rgb, rgb);}//灰度private void btnGray_Click(object sender, EventArgs e){try{int Height = this.picBase.Image.Height;int Width = this.picBase.Image.Width;Bitmap newbitmap = new Bitmap(Width, Height);Bitmap oldbitmap = (Bitmap)this.picBase.Image;Color pixel;for (int x = 0; x < Width; x++){for (int y = 0; y < Height; y++){pixel = oldbitmap.GetPixel(x, y);newbitmap.SetPixel(x, y, Gray(pixel));}}this.picBase.Image = newbitmap;}catch (Exception err){MessageBox.Show("灰度化失败原因:" + err.Message);}}//灰度化算法protected static Color Gray(Color c){int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));return Color.FromArgb(rgb, rgb, rgb);}图像二值化效果图:代码:view plaincopy to clipboardprint?//二值化private void btnDobleValue_Click(object sender, EventArgs e){this.picBase.Image = Binarizate((Bitmap)this.picBase.Image);}public static Bitmap Binarizate(Bitmap map){int tv = ComputeThresholdValue(map);int x = map.Width;int y = map.Height;for (int i = 0; i < x; i++){for (int j = 0; j < y; j++){if (map.GetPixel(i, j).R >= tv){map.SetPixel(i, j, Color.FromArgb(0xff, 0xff, 0xff));}else{map.SetPixel(i, j, Color.FromArgb(0, 0, 0));}}}return map;}//二值化private void btnDobleValue_Click(object sender, EventArgs e){this.picBase.Image = Binarizate((Bitmap)this.picBase.Image);}public static Bitmap Binarizate(Bitmap map){int tv = ComputeThresholdValue(map);int x = map.Width;int y = map.Height;for (int i = 0; i < x; i++){for (int j = 0; j < y; j++){if (map.GetPixel(i, j).R >= tv){map.SetPixel(i, j, Color.FromArgb(0xff, 0xff, 0xff));}else{map.SetPixel(i, j, Color.FromArgb(0, 0, 0));}}}return map;}图像去噪(1*1像素)效果图:<原始><去噪后>代码:view plaincopy to clipboardprint?private void btnClean11_Click(object sender, EventArgs e){this.picBase.Image = OperateBitmap.ClearNoise((Bitmap)this.picBase.Image,4); }//1*1除噪public static Bitmap ClearNoise(Bitmap bitmap, int MaxNearPoints){Color piexl;int nearDots = 0;//int XSpan, YSpan, tmpX, tmpY;//逐点判断for (int i = 0; i < bitmap.Width; i++)for (int j = 0; j < bitmap.Height; j++){piexl = bitmap.GetPixel(i, j);if (piexl.R != 255){nearDots = 0;//判断周围8个点是否全为空if (i == 0 || i == bitmap.Width - 1 || j == 0 || j == bitmap.Height - 1) //边框全去掉{bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));}else{if (bitmap.GetPixel(i - 1, j - 1).R != 255 ) nearDots++;if (bitmap.GetPixel(i, j - 1).R != 255) nearDots++;if (bitmap.GetPixel(i + 1, j - 1).R != 255) nearDots++;if (bitmap.GetPixel(i - 1, j).R != 255) nearDots++;if (bitmap.GetPixel(i + 1, j).R != 255) nearDots++;if (bitmap.GetPixel(i - 1, j + 1).R != 255) nearDots++;if (bitmap.GetPixel(i, j + 1).R != 255) nearDots++;if (bitmap.GetPixel(i + 1, j + 1).R != 255) nearDots++;if (nearDots < 4)bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点}}else//背景bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));}return bitmap;}private void btnClean11_Click(object sender, EventArgs e){this.picBase.Image = OperateBitmap.ClearNoise((Bitmap)this.picBase.Image,4);}//1*1除噪public static Bitmap ClearNoise(Bitmap bitmap, int MaxNearPoints){Color piexl;int nearDots = 0;//int XSpan, YSpan, tmpX, tmpY;//逐点判断for (int i = 0; i < bitmap.Width; i++)for (int j = 0; j < bitmap.Height; j++){piexl = bitmap.GetPixel(i, j);if (piexl.R != 255){nearDots = 0;//判断周围8个点是否全为空if (i == 0 || i == bitmap.Width - 1 || j == 0 || j == bitmap.Height - 1) //边框全去掉{bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));}else{if (bitmap.GetPixel(i - 1, j - 1).R != 255 ) nearDots++;if (bitmap.GetPixel(i, j - 1).R != 255) nearDots++;if (bitmap.GetPixel(i + 1, j - 1).R != 255) nearDots++;if (bitmap.GetPixel(i - 1, j).R != 255) nearDots++;if (bitmap.GetPixel(i + 1, j).R != 255) nearDots++;if (bitmap.GetPixel(i - 1, j + 1).R != 255) nearDots++;if (bitmap.GetPixel(i, j + 1).R != 255) nearDots++;if (bitmap.GetPixel(i + 1, j + 1).R != 255) nearDots++;if (nearDots < 4)bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点}}else//背景bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));}return bitmap;}另外本人有百度知道的自动问答机源代码c#源码可出售。