關於Otsu二值化的程式(從C轉成DELPHI) (問題兼分享) |
尚未結案
|
pusong
一般會員 發表:2 回覆:7 積分:1 註冊:2003-02-27 發送簡訊給我 |
本程式由arisaka_matsuri發表的BCB程式改寫成DELPHI 詳文參照 http://delphi.ktop.com.tw/topic.php?topic_id=40261 小弟新學DELPHI 不曉得如此"轉譯"是否有錯誤發生?
可否為小弟研究一下呢? 感恩~~~ PS: 第一次發言,不知是否有錯,請多多包涵~~
procedure TfmColorspace.Button1Click(Sender: TObject); var i,T,N,T_optimal:integer; x,y,Gr:integer; ni: array[0..256] of double; Pi: array[0..256] of double; Sum_Pi: array[0..256] of double; Sum_pi_i: array[0..256] of double; W1,W2,U1,U2:double; Sigma_1_square, Sigma_2_square, Sigma_w_square:double; MinVariance:double; P: PRGBTripleList; begin // Otsu法求最佳門檻值(256灰階) FUNCTION // **** 變數說明 **** // N 影像的尺寸(總像素數) // n[i] 影像中灰階值為i的像素數 // P[i] 影像中灰階值為i的像素所出現的機率 // // 當門檻值為i時 // Sum_Pi[i] 影像中第一群像素(C1)所佔的比率 =W1 // W2 影像中第二群像素(C2)所佔的比率 =1-W1 // Sum_Pi_i[i] SUM(T=0 to i,P[i]*i) // U1 C1的期望值 =Sum_Pi_i[i]/W1 // U2 C2的期望值 =(Sum_Pi_i[255]-Sum_Pi_i[i])/W2 // Sigma_1_square C1的變異數 // Sigma_2_square C2的變異數 // Sigma_w_square C1和C2的變異數和 T_optimal := 0; MinVariance:= 1.7e 8; // 計算影像的總像素數 N := BitMap.Width*BitMap.Height; // 統計影像中灰階值為i的像素數 for i:=0 to 256 do begin ni[i]:=0; Sum_pi_i[i]:=0; Sum_pi[i]:=0; end; with BitMap do begin begin for y := 0 to BitMap.Height - 1 do begin P := PRGBTripleList(ScanLine[y]); for x := 0 to BitMap.Width - 1 do begin Gr := Round(P^[x].rgbtRed); ni[Gr]:=ni[Gr] 1; end; end; end; end; // 計算影像中灰階值為i的像素所出現的機率 for i:=0 to 256 do begin Pi[i]:= (ni[i]/N); end; // 分別計算門檻值為T時,Sum_Pi和Sum_Pi_i Sum_pi[0]:=Pi[0]; Sum_pi_i[0]:=0; for i:= 1 to 256 do begin Sum_Pi[i] := Sum_Pi[i-1] Pi[i]; Sum_Pi_i[i] := Sum_Pi_i[i-1] Pi[i] * i; end; // 檢查所有的灰階值,使C1和C2的變異數和為最小時 // 即為所求的最佳門檻值 for T:=1 to 254 do begin W1:=0.0; W2:=0.0; W1 := Sum_Pi[T]; W2 := 1 - W1; if w1 < 1e-10 then w1 := 1e-10; if w2 < 1e-10 then w2 := 1e-10; U1 := Sum_Pi_i[T] / (W1); U2 := (Sum_Pi_i[255] - Sum_Pi_i[T]) / (W2); Sigma_1_square := 0; for i := 0 to T do Sigma_1_square := (((i-U1))*(i-U1)*Pi[i]) (((i-1)-U1)*((i-1)-U1)*Pi[i-1]); Sigma_1_square := Sigma_1_square / W1; Sigma_2_square := 0; for i:=T 1 to 255 do Sigma_2_square := ((i-U2)*(i-U2)*Pi[i]) (((i-1)-U2)*((i-1)-U2)*Pi[i-1]); Sigma_2_square := Sigma_2_square / W2; Sigma_w_square := (W1 * Sigma_1_square) (W2 * Sigma_2_square); if Sigma_w_square < MinVariance then begin MinVariance := Sigma_w_square; T_optimal := T; end; end; // 回傳最佳門檻值 showmessage(intTostr(T_optimal)); end;發表人 - pusong 於 2003/12/06 22:52:56 |
azurecloud
中階會員 發表:52 回覆:108 積分:92 註冊:2003-09-04 發送簡訊給我 |
Hi, pusong 您好:
感謝您熱心的分享,請問您自己 Compile 有過嗎?我測試有幾個問題: 1、PRGBTripleList 找不到這個物件(是否要改成 TImageList ?)
2、ScanLine 這個變數沒有宣告
3、with BitMap do 這裡也會出問題,BitMap 該是附屬在 ImageList 下的
物件或方法
以上提供您參考,抱歉對於演算法不熟,只能幫您測測 bug 而已 ========================
我要努力向上~~[/h5]======================== |
pusong
一般會員 發表:2 回覆:7 積分:1 註冊:2003-02-27 發送簡訊給我 |
Hi, azurecloud 您好: 我已經可以RUN了 只是對於C轉DELPHI不是很瞭解
不知道是否有缺失(該宣告沒宣告) 1、PRGBTripleList 找不到這個物件(是否要改成 TImageList ?) 這要宣告....但內容不是很清楚 2、ScanLine 這個變數沒有宣告 這不是內設指令嗎? 3、with BitMap do 這裡也會出問題,BitMap 該是附屬在 ImageList 下的
物件或方法 恩 應該是 以上提供您參考,抱歉對於演算法不熟,只能幫您測測 bug 而已 謝謝 就互相學習囉!
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |