VB6 切割圖形中的文字
2015-03-06 分類:web 技術 閱讀次數:3066 評論(1)繼上篇大津閥值法分離圖形中的文字後,今天把切割圖形中的文字也寫出來。
用一個不規則矩陣 (jagged array) 儲存切割後的文字排列。
原始驗證碼圖形為, 來自 yunfile.com:
經由OTSU大津法,找出閥值,轉成矩陣 Pix(x,y),如以下所示的數字,準備切割:
準備切割成為:
副程序如下
Public Sub CutNum(ByRef vPix() As Boolean, ByRef vNums() As Variant) 'begin cutting every numers Dim iX As Integer: Dim iY As Integer Dim iB1 As Integer: Dim iB2 As Integer Dim i As Integer: Dim iNums As Integer: Dim iPrevious As Integer Dim j As Integer 'Dim aNums(3) As Variant 'array for cutted number, it will be a jagged array Dim aPix1() As Boolean ReDim aPix1(0, 0) As Boolean Dim bStartStage As Boolean Dim iArrayStart As Integer Dim arTEMPV() As Integer Dim arTEMPH() As Integer ReDim arTEMPV(UBound(vPix, 1)) As Integer 'Y Axie histogram ReDim arTEMPH(UBound(vPix, 2)) As Integer 'X Axie histogram For i = LBound(arTEMPV) To UBound(arTEMPV) arTEMPV(i) = 0 Next For i = LBound(arTEMPH) To UBound(arTEMPH) arTEMPH(i) = 0 Next For iX = LBound(arTEMPV) To UBound(arTEMPV) For iY = LBound(arTEMPH) To UBound(arTEMPH) If vPix(iX, iY) Then arTEMPV(iX) = arTEMPV(iX) + 1 arTEMPH(iY) = arTEMPH(iY) + 1 End If Next Next iNums = 0 iPrevious = 0 bStartStage = False iB1 = LBound(arTEMPV) For i = 0 To UBound(vNums) For iX = iB1 To UBound(arTEMPV) If iX > LBound(arTEMPV) Then iPrevious = arTEMPV(iX - 1) If (arTEMPV(iX) = 0) Then If Not bStartStage Then Erase aPix1 ReDim aPix1(iX - iArrayStart + 1, UBound(vPix, 2)) For j = LBound(aPix1, 1) + 1 To UBound(aPix1, 1) For iY = LBound(aPix1, 2) + 1 To UBound(aPix1, 2) aPix1(j, iY) = vPix(j + iArrayStart - 1, iY - 1) Next Next 'Text1.Text = "" Call ToTextBox(aPix1) vNums(i) = aPix1 bStartStage = True Exit For End If Else 'arTEMPV(iX) <> 0 If bStartStage And (iPrevious = 0) Then bStartStage = False iArrayStart = iX End If End If Next iB1 = iX + 1 Next End Sub
在主程式內,呼叫
Dim aNums(3) As Variant
Call CutNum(pixNEW, aNums)
即可取回 aNums 這個不規則矩陣,包含4個數字的矩陣資料。
?anum(0)(1,1) '取出第一個數字第2行,第2列的boolean值
?ubound(anum(1),2) '返回第二個數字的第二維陣列個數 (y值)
Call ToTextBox(aPix1)
這只是輸出畫面用途。
轉載請註明出處為「本文轉載於『油拉林』原地址: http://blog.hiastro.com.tw/webtechs/vb6-cut-Numbers-jagged-array」
評論
發表評論