public int getThreshold(NyARHistogram i_histogram)
        {
            //総ピクセル数を計算
            int n            = i_histogram.length;
            int sum_of_pixel = i_histogram.total_of_data;

            int[] hist = i_histogram.data;
            // 閾値ピクセル数確定
            int th_pixcels = sum_of_pixel * this._persentage / 100;
            int th_wk;
            int th_w, th_b;

            // 黒点基準
            th_wk = th_pixcels;
            for (th_b = 0; th_b < n - 2; th_b++)
            {
                th_wk -= hist[th_b];
                if (th_wk <= 0)
                {
                    break;
                }
            }
            // 白点基準
            th_wk = th_pixcels;
            for (th_w = n - 1; th_w > 1; th_w--)
            {
                th_wk -= hist[th_w];
                if (th_wk <= 0)
                {
                    break;
                }
            }
            // 閾値の保存
            return((th_w + th_b) / 2);
        }
        /**
         * この関数は、SlidePTileを用いて敷居値を1個求めます。敷居値の範囲は、i_histogram引数の範囲と同じです。
         */
        public int getThreshold(NyARHistogram i_histogram)
        {
            //総ピクセル数を計算
            int n = i_histogram.length;
            int sum_of_pixel = i_histogram.total_of_data;
            int[] hist = i_histogram.data;
            // 閾値ピクセル数確定
            int th_pixcels = sum_of_pixel * this._persentage / 100;
            int th_wk;
            int th_w, th_b;

            // 黒点基準
            th_wk = th_pixcels;
            for (th_b = 0; th_b < n - 2; th_b++)
            {
                th_wk -= hist[th_b];
                if (th_wk <= 0)
                {
                    break;
                }
            }
            // 白点基準
            th_wk = th_pixcels;
            for (th_w = n - 1; th_w > 1; th_w--)
            {
                th_wk -= hist[th_w];
                if (th_wk <= 0)
                {
                    break;
                }
            }
            // 閾値の保存
            return (th_w + th_b) / 2;
        }
Example #3
0
 /**
  * 共通初期化関数。
  * @param i_param
  * @param i_drv_factory
  * ラスタドライバのファクトリ。
  * @param i_gs_type
  * @param i_rgb_type
  * @return
  * @
  */
 private void initInstance(NyARIntSize i_size)
 {
     //リソースの生成
     this.initResource(i_size);
     this._gs_hist = new NyARHistogram(256);
     this._src_ts = 0;
     this._gs_id_ts = 0;
     this._gs_hist_ts = 0;
 }
	    public NyARRasterThresholdAnalyzer_SlidePTile(int i_persentage,int i_raster_format,int i_vertical_interval)
	    {
		    Debug.Assert (0 <= i_persentage && i_persentage <= 50);
		    //初期化
		    if(!initInstance(i_raster_format,i_vertical_interval)){
			    throw new NyARException();
		    }
		    this._sptile=new NyARHistogramAnalyzer_SlidePTile(i_persentage);
		    this._histogram=new NyARHistogram(256);
	    }
        public int getThreshold(NyARHistogram i_histogram)
        {
            int[] hist = i_histogram.data;
            int   n = i_histogram.length;
            int   da, sa, db, sb, dt, pt, st;
            int   i;
            int   th = 0;

            //後で使う
            dt = pt = 0;
            for (i = 0; i < n; i++)
            {
                int h = hist[i];
                dt += h * i;
                pt += h * i * i;      //正規化の時に使う定数
            }
            st = i_histogram.total_of_data;
            //Low側(0<=i<=n-2)
            da = dt;
            sa = st;
            //High側(i=n-1)
            db = sb = 0;

            double max    = -1;
            double max_mt = 0;

            //各ヒストグラムの分離度を計算する(1<=i<=n-1の範囲で評価)
            for (i = n - 1; i > 0; i--)
            {
                //次のヒストグラムを計算
                int hist_count = hist[i];
                int hist_val   = hist_count * i;
                da -= hist_val;
                sa -= hist_count;
                db += hist_val;
                sb += hist_count;

                //クラス間分散を計算
                double dv  = (sa + sb);
                double mt  = (double)(da + db) / dv;
                double ma  = (sa != 0?((double)da / (double)sa):0) - mt;
                double mb  = (sb != 0?((double)db / (double)sb):0) - mt;
                double kai = ((double)(sa * (ma * ma) + sb * (mb * mb))) / dv;
                if (max < kai)
                {
                    max_mt = mt;
                    max    = kai;
                    th     = i;
                }
                //System.out.println(kai);
            }
            //max_mtを元に正規化
            this._score = max / ((double)(pt + max_mt * max_mt * st - 2 * max_mt * dt) / st);//129,0.8888888888888887
            return(th);
        }
Example #6
0
 public NyARRasterThresholdAnalyzer_SlidePTile(int i_persentage, int i_raster_format, int i_vertical_interval)
 {
     Debug.Assert(0 <= i_persentage && i_persentage <= 50);
     //初期化
     if (!initInstance(i_raster_format, i_vertical_interval))
     {
         throw new NyARException();
     }
     this._sptile    = new NyARHistogramAnalyzer_SlidePTile(i_persentage);
     this._histogram = new NyARHistogram(256);
 }
 /**
  * デバック用関数
  * @param args
  * main関数引数
  */
 public static void main(string[] args)
 {
     NyARHistogram data = new NyARHistogram(256);
     for (int i = 0; i < 256; i++)
     {
         data.data[i] = 128 - i > 0 ? 128 - i : i - 128;
     }
     data.total_of_data = data.getTotal(0, 255);
     NyARHistogramAnalyzer_KittlerThreshold an = new NyARHistogramAnalyzer_KittlerThreshold();
     int th = an.getThreshold(data);
     //System.out.print(th);
     return;
 }
Example #8
0
        /**
         * デバック用関数
         * @param args
         * main関数引数
         */
        public static void main(string[] args)
        {
            NyARHistogram data = new NyARHistogram(256);

            for (int i = 0; i < 256; i++)
            {
                data.data[i] = 128 - i > 0 ? 128 - i : i - 128;
            }
            data.total_of_data = data.getTotal(0, 255);
            //NyARHistogramAnalyzer_KittlerThreshold an = new NyARHistogramAnalyzer_KittlerThreshold();
            //int th = an.getThreshold(data);
            //System.out.print(th);
            return;
        }
	    public int getThreshold(NyARHistogram i_histogram)
	    {
		    int[] hist=i_histogram.data;
		    int n=i_histogram.length;
		    int da,sa,db,sb,dt,pt,st;
		    int i;		
		    int th=0;
		    //後で使う
		    dt=pt=0;
		    for(i=0;i<n;i++){
			    int h=hist[i];
			    dt+=h*i;
			    pt+=h*i*i;//正規化の時に使う定数
		    }
		    st=i_histogram.total_of_data;
		    //Low側(0<=i<=n-2)
		    da=dt;
		    sa=st;
		    //High側(i=n-1)
		    db=sb=0;		
    		
		    double max=-1;
		    double max_mt=0;
		    //各ヒストグラムの分離度を計算する(1<=i<=n-1の範囲で評価)
		    for(i=n-1;i>0;i--){
			    //次のヒストグラムを計算
			    int hist_count=hist[i];
			    int hist_val=hist_count*i;
			    da-=hist_val;
			    sa-=hist_count;
			    db+=hist_val;
			    sb+=hist_count;
    			
			    //クラス間分散を計算
			    double dv=(sa+sb);
			    double mt=(double)(da+db)/dv;
			    double ma=(sa!=0?((double)da/(double)sa):0)-mt;
			    double mb=(sb!=0?((double)db/(double)sb):0)-mt;
			    double kai=((double)(sa*(ma*ma)+sb*(mb*mb)))/dv;
			    if(max<kai){
				    max_mt=mt;
				    max=kai;
				    th=i;
			    }
			    //System.out.println(kai);
		    }
		    //max_mtを元に正規化
		    this._score=max/((double)(pt+max_mt*max_mt*st-2*max_mt*dt)/st);//129,0.8888888888888887
		    return th;
	    }
        /**
         * o_histogramにヒストグラムを出力します。
         * @param i_input
         * @param o_histogram
         * @return
         * @throws NyARException
         */
        public int analyzeRaster(INyARRaster i_input, NyARHistogram o_histogram)
        {
            NyARIntSize size = i_input.getSize();

            //最大画像サイズの制限
            Debug.Assert(size.w * size.h < 0x40000000);
            Debug.Assert(o_histogram.length == 256);      //現在は固定

            int[] h = o_histogram.data;
            //ヒストグラム初期化
            for (int i = o_histogram.length - 1; i >= 0; i--)
            {
                h[i] = 0;
            }
            o_histogram.total_of_data = size.w * size.h / this._vertical_skip;
            return(this._histImpl.createHistogram(i_input, size, h, this._vertical_skip));
        }
        public void analyzeRaster(INyARRaster i_input, NyARIntRect i_area, NyARHistogram o_histogram)
        {
            NyARIntSize size = i_input.getSize();

            //最大画像サイズの制限
            Debug.Assert(size.w * size.h < 0x40000000);
            Debug.Assert(o_histogram.length == 256);//現在は固定

            int[] h = o_histogram.data;
            //ヒストグラム初期化
            for (int i = o_histogram.length - 1; i >= 0; i--)
            {
                h[i] = 0;
            }
            o_histogram.total_of_data = i_area.w * i_area.h / this._vertical_skip;
            this._histImpl.createHistogram(i_input, i_area.x, i_area.y, i_area.w, i_area.h, o_histogram.data, this._vertical_skip);
            return;
        }
        public override void doFilter(INyARRaster i_input, INyARRaster i_output)
        {
            //ヒストグラムを得る
            NyARHistogram hist = this._histogram;

            this._hist_analyzer.analyzeRaster(i_input, hist);
            //変換テーブルを作成
            int hist_total = this._histogram.total_of_data;
            int min        = hist.getMinData();
            int hist_size  = this._histogram.length;
            int sum        = 0;

            for (int i = 0; i < hist_size; i++)
            {
                sum          += hist.data[i];
                this.table[i] = (int)((sum - min) * (hist_size - 1) / ((hist_total - min)));
            }
            //変換
            base.doFilter(i_input, i_output);
        }
Example #13
0
        /**
         * この関数は、kittlerThresholdを用いて敷居値を1個求めます。敷居値の範囲は、i_histogram引数の範囲と同じです。
         */
        public int getThreshold(NyARHistogram i_histogram)
        {
            int    i;
            double min = Double.MaxValue;
            int    th = 0;
            int    da, sa, db, sb, pa, pb;
            double oa, ob;

            int[] hist = i_histogram.data;
            int   n    = i_histogram.length;

            //Low側
            da = pa = 0;
            int h;

            for (i = 0; i < n; i++)
            {
                h   = hist[i];
                da += h * i;     //i*h[i]
                pa += h * i * i; //i*i*h[i]
            }
            sa = i_histogram.total_of_data;
            //High側(i=n-1)
            db = 0;
            sb = 0;
            pb = 0;


            for (i = n - 1; i > 0; i--)
            {
                //次のヒストグラムを計算
                int hist_count = hist[i];        //h[i]
                int hist_val   = hist_count * i; //h[i]*i
                int hist_val2  = hist_val * i;   //h[i]*i*i
                da -= hist_val;
                sa -= hist_count;
                pa -= hist_val2;
                db += hist_val;
                sb += hist_count;
                pb += hist_val2;

                //初期化
                double wa = (double)sa / (sa + sb);
                double wb = (double)sb / (sa + sb);
                if (wa == 0 || wb == 0)
                {
                    continue;
                }

                oa = ob = 0;
                double ma = sa != 0 ? (double)da / sa : 0;
                //Σ(i-ma)^2*h[i]=Σ(i^2*h[i])+Σ(ma^2*h[i])-Σ(2*i*ma*h[i])
                oa = ((double)(pa + ma * ma * sa - 2 * ma * da)) / sa;

                double mb = sb != 0 ? (double)db / sb : 0;
                //Σ(i-mb)^2*h[i]=Σ(i^2*h[i])+Σ(mb^2*h[i])-Σ(2*i*mb*h[i])
                ob = ((double)(pb + mb * mb * sb - 2 * mb * db)) / sb;

                double kai = wa * Math.Log(oa / wa) + wb * Math.Log(ob / wb);
                if (kai > 0 && min > kai)
                {
                    min = kai;
                    th  = i;
                }
                //System.out.println(kai);
            }
            return(th);//129//7.506713872738873
        }
        /**
         * この関数は、kittlerThresholdを用いて敷居値を1個求めます。敷居値の範囲は、i_histogram引数の範囲と同じです。
         */
        public int getThreshold(NyARHistogram i_histogram)
        {
            int i;
            double min = Double.MaxValue;
            int th = 0;
            int da, sa, db, sb, pa, pb;
            double oa, ob;

            int[] hist = i_histogram.data;
            int n = i_histogram.length;
            //Low側
            da = pa = 0;
            int h;
            for (i = 0; i < n; i++)
            {
                h = hist[i];
                da += h * i;	//i*h[i]
                pa += h * i * i;	//i*i*h[i]
            }
            sa = i_histogram.total_of_data;
            //High側(i=n-1)
            db = 0;
            sb = 0;
            pb = 0;


            for (i = n - 1; i > 0; i--)
            {
                //次のヒストグラムを計算
                int hist_count = hist[i];//h[i]
                int hist_val = hist_count * i;  //h[i]*i
                int hist_val2 = hist_val * i;    //h[i]*i*i
                da -= hist_val;
                sa -= hist_count;
                pa -= hist_val2;
                db += hist_val;
                sb += hist_count;
                pb += hist_val2;

                //初期化
                double wa = (double)sa / (sa + sb);
                double wb = (double)sb / (sa + sb);
                if (wa == 0 || wb == 0)
                {
                    continue;
                }

                oa = ob = 0;
                double ma = sa != 0 ? (double)da / sa : 0;
                //Σ(i-ma)^2*h[i]=Σ(i^2*h[i])+Σ(ma^2*h[i])-Σ(2*i*ma*h[i])
                oa = ((double)(pa + ma * ma * sa - 2 * ma * da)) / sa;

                double mb = sb != 0 ? (double)db / sb : 0;
                //Σ(i-mb)^2*h[i]=Σ(i^2*h[i])+Σ(mb^2*h[i])-Σ(2*i*mb*h[i])
                ob = ((double)(pb + mb * mb * sb - 2 * mb * db)) / sb;

                double kai = wa * Math.Log(oa / wa) + wb * Math.Log(ob / wb);
                if (kai > 0 && min > kai)
                {
                    min = kai;
                    th = i;
                }
                //System.out.println(kai);

            }
            return th;//129//7.506713872738873
        }
	    /**
	     * o_histogramにヒストグラムを出力します。
	     * @param i_input
	     * @param o_histogram
	     * @return
	     * @throws NyARException
	     */
	    public int analyzeRaster(INyARRaster i_input,NyARHistogram o_histogram)
	    {
    		
		    NyARIntSize size=i_input.getSize();
		    //最大画像サイズの制限
		    Debug.Assert(size.w*size.h<0x40000000);
		    Debug.Assert(o_histogram.length==256);//現在は固定

		    int[] h=o_histogram.data;
		    //ヒストグラム初期化
		    for (int i = o_histogram.length-1; i >=0; i--){
			    h[i] = 0;
		    }
		    o_histogram.total_of_data=size.w*size.h/this._vertical_skip;
		    return this._histImpl.createHistogram(i_input, size,h,this._vertical_skip);		
	    }