/** * 共通初期化関数。 * @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 NyARSensor(NyARIntSize i_size) { this._gs_raster = NyARGrayscaleRaster.createInstance(i_size.w, i_size.h, NyARBufferType.INT1D_GRAY_8, true); this._gs_hist = new NyARHistogram(256); this._src_ts = 0; this._gs_id_ts = 0; this._gs_hist_ts = 0; this._hist_drv = (INyARHistogramFromRaster)this._gs_raster.createInterface(typeof(INyARHistogramFromRaster)); }
/** * この関数は、判別法を用いて敷居値を1個求めます。敷居値の範囲は、i_histogram引数の範囲と同じです。 * 関数は、thisのプロパティを更新します。 */ 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); }
/** * この関数は、判別法を用いて敷居値を1個求めます。敷居値の範囲は、i_histogram引数の範囲と同じです。 * 関数は、thisのプロパティを更新します。 */ 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; }
/** * デバック用関数 * @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_DiscriminantThreshold an = new NyARHistogramAnalyzer_DiscriminantThreshold(); int th = an.getThreshold(data); //System.out.print(th); return; }
/** * デバック用関数 * @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_DiscriminantThreshold an = new NyARHistogramAnalyzer_DiscriminantThreshold(); //int th = an.getThreshold(data); //System.out.print(th); return; }
public void createHistogram(int i_l, int i_t, int i_w, int i_h, int i_skip, NyARHistogram o_histogram) { o_histogram.reset(); int[] data_ptr = o_histogram.data; INyARGsPixelDriver drv = this._gsr.getGsPixelDriver(); int pix_count = i_w; int pix_mod_part = pix_count - (pix_count % 8); //左上から1行づつ走査していく for (int y = i_h - 1; y >= 0; y -= i_skip) { for (int x = pix_count - 1; x >= pix_mod_part; x--) { data_ptr[drv.getPixel(x, y)]++; } } o_histogram.total_of_data = i_w * i_h / i_skip; return; }
public void createHistogram(int i_l, int i_t, int i_w, int i_h, int i_skip, NyARHistogram o_histogram) { o_histogram.reset(); int[] data_ptr = o_histogram.data; INyARRgbRaster drv = this._gsr; int pix_count = i_w; int pix_mod_part = pix_count - (pix_count % 8); //左上から1行づつ走査していく for (int y = i_h - 1; y >= 0; y -= i_skip) { for (int x = pix_count - 1; x >= pix_mod_part; x--) { drv.getPixel(x, y, tmp); data_ptr[(tmp[0] + tmp[1] + tmp[2]) / 3]++; } } o_histogram.total_of_data = i_w * i_h / i_skip; return; }
public void doFilter(int i_hist_interval, INyARGrayscaleRaster i_output) { //ヒストグラムを得る NyARHistogram hist = this._histogram; this._histdrv.createHistogram(i_hist_interval, 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._hist[i] = (int)((sum - min) * (hist_size - 1) / ((hist_total - min))); } //変換 this._tone_table.doFilter(this._hist, i_output); return; }
public void createHistogram(int i_l, int i_t, int i_w, int i_h, int i_skip, NyARHistogram o_histogram) { o_histogram.reset(); int[] input = (int[])this._gsr.getBuffer(); NyARIntSize s = this._gsr.getSize(); int skip = (i_skip * s.w - i_w); int pix_count = i_w; int pix_mod_part = pix_count - (pix_count % 8); //左上から1行づつ走査していく int pt = (i_t * s.w + i_l); int[] data = o_histogram.data; for (int y = i_h - 1; y >= 0; y -= i_skip) { int x; for (x = pix_count - 1; x >= pix_mod_part; x--) { data[input[pt++]]++; } for (; x >= 0; x -= 8) { data[input[pt++]]++; data[input[pt++]]++; data[input[pt++]]++; data[input[pt++]]++; data[input[pt++]]++; data[input[pt++]]++; data[input[pt++]]++; data[input[pt++]]++; } //スキップ pt += skip; } o_histogram.total_of_data = i_w * i_h / i_skip; return; }
public void createHistogram(int i_skip, NyARHistogram o_histogram) { NyARIntSize s = this._gsr.getSize(); this.createHistogram(0, 0, s.w, s.h, i_skip, o_histogram); }