区域对比验证码识别,超级简单的Region对比识别验证码
2012-03-19 15:27
281 查看
在验证识别常用技巧之外,我们可以采用区域对比的方法进行验证码识别,对相对简单的验证码有效,如果由噪点的话可以现去噪点,然后再执行如下函数
在验证识别常用技巧之外,我们可以采用区域对比的方法进行验证码识别,对相对简单的验证码有效,如果由噪点的话可以现去噪点,然后再执行如下函数
在一些验证码相对简单的站点中,就是对于特定的字符,在生成时,其字符的Region应该是一样的,如图片
![](http://hi.csdn.net/attachment/201203/19/0_13321422048ewm.gif)
中的6总是这个字体这个字号......我们只须把验证码图片从左向右一列一列扫描,分隔出每个字符的RGN,然后和chars.bmp中的每个字符的RGN对比,就知道是哪个数字了。
关键代码如下:
识别效率提高的,但是局限性太高了。现在这类我网站也少得多了,现在放出来。希望抛砖引玉引出图形的高级算法来。
在验证识别常用技巧之外,我们可以采用区域对比的方法进行验证码识别,对相对简单的验证码有效,如果由噪点的话可以现去噪点,然后再执行如下函数
在一些验证码相对简单的站点中,就是对于特定的字符,在生成时,其字符的Region应该是一样的,如图片
![](http://hi.csdn.net/attachment/201203/19/0_13321422048ewm.gif)
中的6总是这个字体这个字号......我们只须把验证码图片从左向右一列一列扫描,分隔出每个字符的RGN,然后和chars.bmp中的每个字符的RGN对比,就知道是哪个数字了。
关键代码如下:
namespace WindowsApplication1 { public partial class Form1 : Form { IniFile config = null; //查看两个颜色是不是一样,注意这里有一定误差也算相同 public bool IsSameColor(Color c1, Color c2) { if (Math.Abs(c1.R - c2.R) < 10 && Math.Abs(c1.G - c2.G) < 10 && Math.Abs(c1.B - c2.B) < 10) { return true; } else { return false; } } //计算一个Region中,像素的个数 public int RegionPointCount(Region r,int width,int height) { int count = 0; for(int h = 0; h<width; ++h) { for(int v = 0; v< height; ++v) { if(r.IsVisible(h,v)) { ++count; } } } return count; } //初始化每个字符的Region public void InitPictureCharInfo() { Bitmap bmp = (Bitmap)Bitmap.FromFile("chars.bmp"); List<BitmapCharInfo> bcil = charRgnList; Region rgn = new Region(); rgn.MakeEmpty(); if (bmp.Height > 0 && bmp.Width > 0) { Color bkColor = bmp.GetPixel(0, 0); bool bInWorking = false; int nNextStartPos = 0; for (int h = 0; h < bmp.Width; ++h) { bool bFindColor = false; for (int v = 0; v < bmp.Height; ++v) { if (!IsSameColor(bkColor, bmp.GetPixel(h, v))) { rgn.Union(new Rectangle(h, v, 1, 1)); bFindColor = true; } } if (bInWorking) { if (!bFindColor) { bInWorking = false; rgn.Translate(-nNextStartPos, 0); BitmapCharInfo bci = new BitmapCharInfo(rgn, h - nNextStartPos, bmp.Height); bci.orgPos = nNextStartPos; bcil.Add(bci); rgn = new Region(); rgn.MakeEmpty(); } } else { if (bFindColor) { bInWorking = true; nNextStartPos = h; } } } chars.AddRange("0123456789".ToCharArray()); } } //扫描并识别验证码 public void ScanValidCode() { Bitmap bmp = this.bmpValidCode; List<BitmapCharInfo> bcil = new List<BitmapCharInfo>(); Region rgn = new Region(); rgn.MakeEmpty(); if (bmp.Height > 0 && bmp.Width > 0) { Color bkColor = bmp.GetPixel(0, 0); bool bInWorking = false; int nNextStartPos = 0; for (int h = 0; h < bmp.Width; ++h) { bool bFindColor = false; for (int v = 0; v < bmp.Height; ++v) { if (!IsSameColor(bkColor, bmp.GetPixel(h, v))) { rgn.Union(new Rectangle(h, v, 1, 1)); bFindColor = true; } } if (bInWorking) { if (!bFindColor) { bInWorking = false; rgn.Translate(-nNextStartPos, 0); BitmapCharInfo bci = new BitmapCharInfo(rgn, h - nNextStartPos, bmp.Height); bci.orgPos = nNextStartPos; bcil.Add(bci); rgn = new Region(); rgn.MakeEmpty(); } } else { if (bFindColor) { bInWorking = true; nNextStartPos = h; } } } List<char> chs = new List<char>(); Graphics gh = Graphics.FromImage(bmp); foreach (BitmapCharInfo bci in bcil) { int minPos = -1; int minLng = -1; for (int i = 0; i < charRgnList.Count; ++i) { Region r = bci.rgn.Clone(); r.Union(charRgnList[i].rgn); r.Exclude(bci.rgn); int lng = RegionPointCount(r, bci.width, bci.height); if (minLng == -1) { minLng = lng; minPos = i; } else { if (lng < minLng) { minLng = lng; minPos = i; } } } if (minPos != -1) { chs.Add(chars[minPos]); } } string str = new string(chs.ToArray(), 0, chs.Count); //MessageBox.Show(str); this.currScanValidCode = str; } } public void GetValidCodePicture(CookieContainer cc, WebProxy wp ) { //this.pbValidCode.ImageLocation = this.pbValidCode.ImageLocation; Exception exp = null; for (int i = 0; i < 3; ++i) { try { Stream s = GetDataNonProxy(this.tbValidPicUrl.Text, cc,wp); Bitmap bmp = (Bitmap)Bitmap.FromStream(s); s.Close(); bmpValidCode = bmp; return; } catch (Exception ex) { exp = ex; } } MessageBox.Show("猎取图片失败:" + exp == null?"unkown":exp.Message); } Bitmap bmpValidCode = null; private void btnLoadPic_Click(object sender, EventArgs e) { this.GetValidCodePicture(null,null); currValidCode = this.currScanValidCode; this.UpdateDate(false); } List<BitmapCharInfo> charRgnList = new List<BitmapCharInfo>(); List<char> chars = new List<char>(); private void btnInit_Click(object sender, EventArgs e) { InitPictureCharInfo(); } private void btnScanValidCode_Click(object sender, EventArgs e) { ScanValidCode(); this.currValidCode = this.currScanValidCode; this.UpdateDate(false); } string currScanValidCode; private void Form1_Load(object sender, EventArgs e) { this.UpdateDate(true); InitPictureCharInfo(); config = new IniFile(Application.ExecutablePath + ".ini"); lastPwdPosInDict = config.GetInt("Process", "PwdPostion", 0); InitPwdDict(); UpdateProcessText(); } } public class BitmapCharInfo { public BitmapCharInfo() { this.rgn = new Region(); this.rgn.MakeEmpty(); } public BitmapCharInfo(Region r, int w, int h) { this.rgn = r; this.width = w; this.height = h; } public Region rgn; public int width = 0; public int height = 0; public int orgPos = 0; } }
识别效率提高的,但是局限性太高了。现在这类我网站也少得多了,现在放出来。希望抛砖引玉引出图形的高级算法来。
相关文章推荐
- 超级简单JS网页倒计时代码
- Nearest Neighbor Search 简单几何(求空间一点到区域的距离)
- 简单对比discuz phpwind phpBB
- Reactor与Proactor的简单对比
- 超级简单的 Socket
- (hdu step 3.1.1)超级楼梯(简单递推:从第1级到第m级有多少种走法,每次只能走一步或两步)
- 超级实用及简单易懂的操作XML
- 超级简单的方法开启Android4.4上的Google Now,不需要ROOT、拔卡、清空数据!
- 图片热点区域的简单应用
- 简单工厂和工厂模式的合并对比代码
- 超级简单的底部导航按钮
- HDU 2041 超级楼梯 简单DP动态规划 递归
- PHP静态延迟绑定和普通静态效率简单对比
- HNUSTOJ-1696 简单验证码识别(模拟)
- 超级新手必须知道超级简单的四大Cisco路由器常见命令
- 超级简单的一个AJAX.JS 类
- [软件甜点] 一款Win下超级简单的网络嗅探工具MiniSniffer
- 肤色检测算法 - 基于不同颜色空间简单区域划分的皮肤检测算法
- 超级简单的数据库连接池(支持多数据源)
- 一个超级简单的IP地址搜索引擎(Asp.Net)事件代码12行