promotion image of download ymail app
Promoted

求此 C#程式碼(OTSU) 解釋註解 PART2

// find otsu threshold

public int getOtsuThreshold(Bitmap bmp)

{

byte t=0;

float[] vet=new float[256];

int[] hist=new int[256];

vet.Initialize();

float p1,p2,p12;

int k;

BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),

ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

unsafe

{

byte* p = (byte*)(void*)bmData.Scan0.ToPointer();

getHistogram(p,bmp.Width,bmp.Height,bmData.Stride, hist);

// loop through all possible t values and maximize between class variance

for (k = 1; k != 255; k++)

{

p1 = Px(0, k, hist);

p2 = Px(k + 1, 255, hist);

p12 = p1 * p2;

if (p12 == 0)

p12 = 1;

float diff=(Mx(0, k, hist) * p2) - (Mx(k + 1, 255, hist) * p1);

vet[k] = (float)diff * diff / p12;

//vet[k] = (float)Math.Pow((Mx(0, k, hist) * p2) - (Mx(k + 1, 255, hist) * p1), 2) / p12;

}

}

bmp.UnlockBits(bmData);

t = (byte)findMax(vet, 256);

return t;

}

2 個解答

評分
  • Tai
    Lv 5
    7 年前
    最佳解答

    T = getOtsuThreshold(bmp)

    float m1, m2

    // 找遍 T, 將值放入 vet

    for (k = 1; k != 255; k++) {

     p1 = Px(0, k, hist); // <= k 的數量

     p2 = Px(k + 1, 255, hist); // > k 的數量

    p12 = p1 * p2; // ???

    float diff=(Mx(0, k, hist) * p2) - (Mx(k + 1, 255, hist) * p1); <= ???

    // 改寫成

    m1 = Mx(0, k, hist)/ p1;

    m2 = Mx(k+1,255,hist)/p2;

    diff = (m1-m2) * (m1-m2);

     vet[k] = p1*p2*diff/(p12*p12);

    }

    2013-11-12 06:53:31 補充:

    我沒有詳加推導源碼公式, 先依照自己想法寫了

    • Commenter avatar登入以對解答發表意見
  • 匿名使用者
    7 年前

    全台唯一合法博弈網站隆重登場

    中獎彩金領取僅需5分鐘立刻到手

    快來免費試試手氣吧!

    官方網站 aa777.net

    • Commenter avatar登入以對解答發表意見
還有問題?馬上發問,尋求解答。