提取验证码,灰度处理
一开始想做一个验证码识别器,Google了大半天,得出的结论是,没有万能的(至少我没看见),涉及到了一堆的杂七杂八的东西,着实泼了一盆凉水啊,但也借鉴了网上的一些方法,为真正做一个验证码提取器做了下铺垫,革命尚未成功,仍需努力。。。
总的来说,要做这类东西步骤有如下几个:
1-当然是获取要识别的验证码
2-灰度处理(说的比较笼统)
3-分割成N块
4-把图片转换成一堆0,1
5-根据一些自附表进行匹配(包括误差的比较等)
呃,God Bless Me.......
下面是第一步与第二步的code:(对于代码中的那个验证码,灰度处理后基本能100%抓取,是因为验证码比较 ‘单纯’ 呐,遇到复杂的就挂B了。。。)
public void grayChange()
{
WebRequest request = WebRequest.Create("http://sbc.jit.edu.cn/admininc/checkcode.asp");
WebResponse response = request.GetResponse();
Stream reader = response.GetResponseStream();
Bitmap bmpold = new Bitmap(reader);
this.pictureBox1.Image = bmpold;
Bitmap bmp = GetNewBitmap(bmpold);
//图片灰度处理
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
Color c = bmp.GetPixel(i, j);
int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);//转换灰度的算法
//黑白处理
//当为100,则蓝绿色100%抓取,红色基本没有
//当为210上时,几乎100%能抓取
if (c.R >= 220)
{
bmp.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
else
{
bmp.SetPixel(i, j, Color.FromArgb(0, 0, 0));
}
}
}
this.pictureBox2.Image = bmp;
}
public Bitmap GetNewBitmap(Bitmap mapnew)
{
int posx1 = mapnew.Width;
int posy1 = mapnew.Height;
int posx2 = 0;
int posy2 = 0;
//复制新图
Rectangle cloneRect = new Rectangle(posx2, posy2, posx1, posy1);
return mapnew.Clone(cloneRect, mapnew.PixelFormat);
}
private void button2_Click(object sender, EventArgs e)
{
grayChange();
}