VB6 otsu 大津演算法 (最大類間方差法)
2015-03-04 分類:web 技術 閱讀次數:3016 評論(0)OTSU, 最大類間方差法是由日本學者大津展之於1979年提出的,是一種自適應的閾值確定的方法,又叫大津法,簡稱OTSU。
用VB6,實作成函數,可隨時叫用。
Private Function OtsuValue(ByRef vaPixels() As Byte, ByVal vWidth As Long, ByVal vHeight As Long, _ Optional ByVal vA As Long = 0) As Byte 'Re-write by Robert Chen 'vaPixels() 3-dimension array for a picture's column, row, and RGB values (0-R,1-G, 2-B) 'vWidth, vHeight the same as vaPixel(*, Width, Height) '最大類間方差法(Otsu, 大津閥值法) '這個函數是根據百度文庫一個文檔裡提供的C代碼翻譯過來的 '@http://wenku.baidu.com/link?url=wVl9A7eZiRddxpaCPPLcAIb-VDlyrV__-Zfw6j6o50FEUochgV9G_zRVsMHVDxN2ilOUXiRbSSM-as_ELJpjxnWEvERlABlvVoVK6-FDQpW Dim aHistogram(255) As Long Dim x As Long Dim Y As Long Dim i As Long 'Initial array For i = LBound(aHistogram) To UBound(aHistogram) aHistogram(i) = CLng(0) Next 'making Histogram statistics For Y = 0 To vHeight - 1 For x = 0 To vWidth - 1 aHistogram(vaPixels(vA, x, Y)) = aHistogram(vaPixels(vA, x, Y)) + 1 Next Next Dim p(255) As Double Dim ut As Double Dim uk As Double Dim sigma As Double Dim mk As Double Dim maxk As Long Dim maxs As Double Dim total As Long Dim EPSTLON As Double EPSILON = 0.000001 '10 ^ -6 total = vWidth * vHeight ut = 0 For i = 0 To 255 p(i) = aHistogram(i) / total ut = ut + i * aHistogram(i) Next ut = ut / total wk = 0 uk = 0 maxs = 0 For i = 0 To 255 uk = uk + i * p(i) wk = wk + p(i) If wk <= EPSTLON Or wk >= (1# - EPSTLON) Then Else sigma = (ut * wk - uk) sigma = (sigma * sigma) / (wk * (1# - wk)) If sigma > maxs Then maxs = sigma maxk = i End If End If Next OtsuValue = CByte(maxk) End Function
轉載請註明出處為「本文轉載於『油拉林』原地址: http://blog.hiastro.com.tw/webtechs/VB6-otsu」
評論
發表評論