亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#驗(yàn)證碼識(shí)別基礎(chǔ)方法實(shí)例分析

 更新時(shí)間:2014年09月29日 11:22:05   投稿:shichen2014  
這篇文章主要介紹了C#驗(yàn)證碼識(shí)別基礎(chǔ)方法實(shí)例分析,較為詳細(xì)的總結(jié)了C#驗(yàn)證碼的實(shí)現(xiàn)思路及具體步驟,并對(duì)實(shí)現(xiàn)思路進(jìn)行了總結(jié)歸納,具有很好的實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#驗(yàn)證碼識(shí)別基礎(chǔ)方法,是非常實(shí)用的技巧。分享給大家供大家參考。具體方法分析如下:

背景

最近有朋友在搞一個(gè)東西,已經(jīng)做的挺不錯(cuò)了,最后想再完美一點(diǎn),于是乎就提議把這種驗(yàn)證碼給K.O.了,于是乎就K.O.了這個(gè)驗(yàn)證碼。達(dá)到單個(gè)圖片識(shí)別時(shí)間小于200ms,500個(gè)樣本人工統(tǒng)計(jì)正確率為95%。由于本人沒(méi)有相關(guān)經(jīng)驗(yàn),是摸著石頭過(guò)河。本著經(jīng)驗(yàn)分享的精神,分享一下整個(gè)分析的思路。在各位大神面前獻(xiàn)丑了。

再來(lái)看看部分識(shí)別結(jié)果如下圖所示:

 

這里是不是看著很眼熟?下面再來(lái)具體分析一下。

處理第一步 去背景噪音和二值化

對(duì)于這一塊,考慮了幾種方法。

方法一:統(tǒng)計(jì)圖片顏色分布,顏色占有率低的判定為背景噪音。由于背景噪音和前景色區(qū)分并不明顯,嘗試了很多種取景方法都不能很好去除背景噪音,最終放棄了這種方法。

方法二:事后在網(wǎng)上稍微查了下,最近比較流行計(jì)算灰度后設(shè)定一個(gè)閾值進(jìn)行二值化。其實(shí)所謂的灰度圖片原理是根據(jù)人眼對(duì)色彩敏感度取了權(quán)值,這個(gè)權(quán)值對(duì)計(jì)算機(jī)來(lái)說(shuō)沒(méi)有什么意義。稍微想一下就可以發(fā)現(xiàn),這兩個(gè)過(guò)程完全可以合并。于是乎我一步完成了去背景噪音和二值化。閾值設(shè)置為RGB三分量之和到500。結(jié)果非常令人滿意。

處理第二步 制作字符樣本

樣本對(duì)于計(jì)算機(jī)來(lái)說(shuō)是非常重要的,因?yàn)橛?jì)算機(jī)很難有邏輯思維,就算有邏輯思維也要經(jīng)過(guò)長(zhǎng)期訓(xùn)練才能讓你滿意。所以要用事先制作好的樣本進(jìn)行比較。如果你仔細(xì)觀察過(guò)這些驗(yàn)證碼會(huì)發(fā)現(xiàn)一個(gè)bug,幾乎大部分的驗(yàn)證碼都是使用同樣的字體,于是乎就人工制作了一套字體的樣本。由于上一步已經(jīng)有去除背景噪音的結(jié)果,可以直接利用。制作樣本這一步有點(diǎn)簡(jiǎn)單枯燥,還需要細(xì)心。可能因?yàn)槟愕囊粋€(gè)不細(xì)心會(huì)導(dǎo)致某個(gè)符號(hào)的識(shí)別率偏低。在這500個(gè)樣本中,只發(fā)現(xiàn)了31個(gè)字符。幸虧是某部門的某人員還考慮到了易錯(cuò)的字符,例如,1和I,0和O等。要不然這個(gè)某部門要背負(fù)更多的罵名。

處理第三步 匹配

單個(gè)匹配用了最簡(jiǎn)單最原始的二值比較,不過(guò)匹配的是匹配率而不是匹配數(shù)。我定義了相關(guān)的計(jì)分原則。大原則是“該有的有了加分,該有的沒(méi)了減分,不該有的有了適度減分,可達(dá)區(qū)域外的不算分”。

由于一些符號(hào)的部分區(qū)域匹配結(jié)果跟另一些符號(hào)的完整匹配結(jié)果相似,需要把單個(gè)匹配在一個(gè)擴(kuò)大的區(qū)域內(nèi)擇優(yōu)。在一定的范圍內(nèi),找到一個(gè)最佳匹配,這個(gè)最佳匹配就是當(dāng)前位置對(duì)應(yīng)的符號(hào)。

完成了一次最佳匹配,可以把匹配位置向右推進(jìn)一大步,若找不到合適的最佳匹配就向右推進(jìn)一小步。

處理第四步 優(yōu)化和調(diào)整

任何一個(gè)算法都是需要優(yōu)化和調(diào)整的。現(xiàn)在要找到最佳參數(shù)配置和最佳代碼組織。這一步往往是需要花費(fèi)最多時(shí)間和精力的。

處理第五步 驗(yàn)證結(jié)果

這一步呢,純?nèi)肆︱?yàn)證結(jié)果,統(tǒng)計(jì)出正確率。

思考:

結(jié)果是出來(lái)了,代碼也不多,效果也很理想。搞這一行的,很多時(shí)候都想要通用的。能否通用,很大程度上在于抽象層次。本方法只是單純的匹配,自然不能通用,但是方法和思想?yún)s是通用的。具體案例具體分析。至于扭曲文字、空心文字等,處理要復(fù)雜的多。網(wǎng)上也有一些使用第三方圖像庫(kù)的方法,也許那些方法會(huì)比較通用。等有空了有興趣了繼續(xù)搞一下這個(gè)主題。

實(shí)例源碼:

至于這個(gè)源碼要不要發(fā)布,糾結(jié)了一段時(shí)間。網(wǎng)上已經(jīng)有類似的商業(yè)活動(dòng)了,而且這個(gè)識(shí)別本身沒(méi)有太大難度,再加上某系統(tǒng)天生的bug,此驗(yàn)證碼本身就相當(dāng)于沒(méi)有設(shè)置,因此發(fā)布此代碼,僅作于學(xué)習(xí)交流。

using System.Collections.Generic;
using System.Drawing;
using System.IO; 
using System.IO.Compression;
namespace Crack12306Captcha
{
public class Cracker
{
List<CharInfo> words_ = new List<CharInfo>(); 
public Cracker() 
{
var bytes = new byte[] { 
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xc5, 0x58, 0xd9, 0x92, 0x13, 0x31, 
0x0c, 0x94, 0x9e, 0x93, 0x0c, 0x61, 0x97, 0x2f, 0xe1, 0x58, 0xe0, 0x91, 0x9b, 0x82, 0x62, 0x0b, 
0x58, 0xee, 0xff, 0xff, 0x10, 0xd8, 0xcc, 0xc8, 0xea, 0x96, 0x6c, 0x8f, 0x13, 0x48, 0xe1, 0xaa, 
0x4d, 0x46, 0x96, 0x6d, 0xb5, 0x8e, 0x96, 0x67, 0x73, 0x7f, 0x3b, 0x09, 0x0e, 0x25, 0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0x3b, 0x74, 0xd2, 
0x52, 0xc5, 0xf8, 0x7c, 0x12, 0xfb, 0xe4, 0x37, 0x5b, 0xfb, 0x57, 0x11, 0xa1, 0x18, 0x00, 0x00, 
}; 
using (var stream = new MemoryStream(bytes)) 
using (var gzip = new GZipStream(stream, CompressionMode.Decompress)) 
using (var reader = new BinaryReader(gzip)) 
{ 
while (true) 
{ 

char ch = reader.ReadChar(); 

if (ch == '\0') 
break; 
int width = reader.ReadByte(); 
int height = reader.ReadByte(); 
bool[,] map = new bool[width, height]; 
for (int i = 0; i < width; i++) 
for (int j = 0; j < height; j++) 
map[i, j] = reader.ReadBoolean(); 
words_.Add(new CharInfo(ch, map)); 
} 
} 
} 
public string Read(Bitmap bmp) 
{ 
var result = string.Empty; 
var width = bmp.Width; 
var height = bmp.Height; 
var table = ToTable(bmp); 
var next = SearchNext(table, -1); 

while (next < width - 7) 
{ 
var matched = Match(table, next); 
if (matched.Rate > 0.6) 
{ 
result += matched.Char; 
next = matched.X + 10; 
} 
else 
{ 
next += 1; 
} 
} 
return result; 
} 
private bool[,] ToTable(Bitmap bmp) 
{ 
var table = new bool[bmp.Width, bmp.Height]; 
for (int i = 0; i < bmp.Width; i++) 
for (int j = 0; j < bmp.Height; j++) 
{ 
var color = bmp.GetPixel(i, j); 
table[i, j] = (color.R + color.G + color.B < 500); 
} 
return table; 
} 
private int SearchNext(bool[,] table, int start) 
{ 
var width = table.GetLength(0); 
var height = table.GetLength(1); 
for (start++; start < width; start++) 
for (int j = 0; j < height; j++) 
if (table[start, j]) 
return start; 
return start; 
} 
private double FixedMatch(bool[,] source, bool[,] target, int x0, int y0) 
{ 
double total = 0; 
double count = 0; 
int targetWidth = target.GetLength(0); 
int targetHeight = target.GetLength(1); 
int sourceWidth = source.GetLength(0); 
int sourceHeight = source.GetLength(1); 
int x, y; 
for (int i = 0; i < targetWidth; i++) 
{ 
x = i + x0; 
if (x < 0 || x >= sourceWidth) 
continue; 
for (int j = 0; j < targetHeight; j++) 
{ 

y = j + y0; 
if (y < 0 || y >= sourceHeight) 
continue; 
if (target[i, j]) 
{ 
total++; 
if (source[x, y]) 
count++; 
else 
count--; 
} 
else if (source[x, y]) 
count -= 0.55; 
} 
} 
return count / total; 
} 
private MatchedChar ScopeMatch(bool[,] source, bool[,] target, int start) 
{ 
int targetWidth = target.GetLength(0); 
int targetHeight = target.GetLength(1); 
int sourceWidth = source.GetLength(0); 
int sourceHeight = source.GetLength(1); 
double max = 0; 
var matched = new MatchedChar(); 
for (int i = -2; i < 6; i++) 
for (int j = -3; j < sourceHeight - targetHeight + 5; j++) 
{ 

double rate = FixedMatch(source, target, i + start, j); 
if (rate > max) 
{ 
max = rate; 
matched.X = i + start; 
matched.Y = j; 
matched.Rate = rate; 
} 
} 
return matched; 
} 

private MatchedChar Match(bool[,] source, int start) 
{ 
MatchedChar best = null; 
foreach (var info in words_) 
{ 

var matched = ScopeMatch(source, info.Table, start); 
matched.Char = info.Char; 
if (best == null || best.Rate < matched.Rate) 
best = matched; 
} 
return best; 
} 
private class CharInfo 
{ 
public char Char { get; private set; } 
public bool[,] Table { get; private set; } 
public CharInfo(char ch, bool[,] table) 
{ 
Char = ch; 
Table = table; 
} 
} 
private class MatchedChar 
{ 
public int X { get; set; } 
public int Y { get; set; } 
public char Char { get; set; } 
public double Rate { get; set; } 
} 
} 
}

使用方法如下:

var cracker = new Cracker();
var result = cracker.Read(img);

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論