2 * PROJECT: NyARToolkit(Extension)
\r
3 * --------------------------------------------------------------------------------
\r
4 * The NyARToolkit is Java edition ARToolKit class library.
\r
5 * Copyright (C)2008-2009 Ryo Iizuka
\r
7 * This program is free software: you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation, either version 3 of the License, or
\r
10 * (at your option) any later version.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
20 * For further information please contact.
\r
21 * http://nyatla.jp/nyatoolkit/
\r
22 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
25 package jp.nyatla.nyartoolkit.core.analyzer.histogram;
\r
27 import jp.nyatla.nyartoolkit.core.types.NyARHistogram;
\r
31 * このクラスは、kittlerThresholdを用いて敷居値を求める機能を提供します。
\r
33 public class NyARHistogramAnalyzer_KittlerThreshold implements INyARHistogramAnalyzer_Threshold
\r
36 * この関数は、kittlerThresholdを用いて敷居値を1個求めます。敷居値の範囲は、i_histogram引数の範囲と同じです。
\r
38 public int getThreshold(NyARHistogram i_histogram)
\r
41 double min=Double.MAX_VALUE;
\r
43 int da,sa,db,sb,pa,pb;
\r
46 int[] hist=i_histogram.data;
\r
47 int n=i_histogram.length;
\r
54 pa+=h*i*i; //i*i*h[i]
\r
56 sa=i_histogram.total_of_data;
\r
65 int hist_count=hist[i];//h[i]
\r
66 int hist_val =hist_count*i; //h[i]*i
\r
67 int hist_val2=hist_val*i; //h[i]*i*i
\r
76 double wa=(double)sa/(sa+sb);
\r
77 double wb=(double)sb/(sa+sb);
\r
83 double ma=sa!=0?(double)da/sa:0;
\r
84 //Σ(i-ma)^2*h[i]=Σ(i^2*h[i])+Σ(ma^2*h[i])-Σ(2*i*ma*h[i])
\r
85 oa=((double)(pa+ma*ma*sa-2*ma*da))/sa;
\r
87 double mb=sb!=0?(double)db/sb:0;
\r
88 //Σ(i-mb)^2*h[i]=Σ(i^2*h[i])+Σ(mb^2*h[i])-Σ(2*i*mb*h[i])
\r
89 ob=((double)(pb+mb*mb*sb-2*mb*db))/sb;
\r
91 double kai=wa*Math.log(oa/wa)+wb*Math.log(ob/wb);
\r
92 if(kai>0 && min>kai){
\r
96 //System.out.println(kai);
\r
99 return th;//129//7.506713872738873
\r
106 public static void main(String[] args)
\r
108 NyARHistogram data=new NyARHistogram(256);
\r
109 for(int i=0;i<256;i++){
\r
110 data.data[i]=128-i>0?128-i:i-128;
\r
112 data.total_of_data=data.getTotal(0,255);
\r
113 NyARHistogramAnalyzer_KittlerThreshold an=new NyARHistogramAnalyzer_KittlerThreshold();
\r
114 int th=an.getThreshold(data);
\r
115 System.out.print(th);
\r