private void addFragment(RleElement i_rel_img, int i_nof, int i_row_index, RleInfoStack o_stack) { int l = i_rel_img.l; int len = i_rel_img.r - l; i_rel_img.fid = i_nof;// REL毎の固有ID RleInfoStack.RleInfo v = o_stack.prePush(); v.entry_x = l; v.area = len; v.clip_l = l; v.clip_r = i_rel_img.r - 1; v.clip_t = i_row_index; v.clip_b = i_row_index; v.pos_x = (len * (2 * l + (len - 1))) / 2; v.pos_y = i_row_index * len; return; }
private int imple_labeling(INyARRaster i_raster, int i_th, int i_top, int i_bottom, RleLabelFragmentInfoStack o_stack) { // リセット処理 RleInfoStack rlestack = this._rlestack; rlestack.clear(); // RleElement[] rle_prev = this._rle1; RleElement[] rle_current = this._rle2; int len_prev = 0; int len_current = 0; int width = i_raster.getWidth(); int[] in_buf = (int[])i_raster.getBuffer(); int id_max = 0; int label_count = 0; // 初段登録 len_prev = toRel(in_buf, i_top, width, rle_prev, i_th); for (int i = 0; i < len_prev; i++) { // フラグメントID=フラグメント初期値、POS=Y値、RELインデクス=行 addFragment(rle_prev[i], id_max, i_top, rlestack); id_max++; // nofの最大値チェック label_count++; } RleInfoStack.RleInfo[] f_array = rlestack.getArray(); // 次段結合 for (int y = i_top + 1; y < i_bottom; y++) { // カレント行の読込 len_current = toRel(in_buf, y * width, width, rle_current, i_th); int index_prev = 0; for (int i = 0; i < len_current; i++) { // index_prev,len_prevの位置を調整する int id = -1; // チェックすべきprevがあれば確認 while (index_prev < len_prev) { if (rle_current[i].l - rle_prev[index_prev].r > 0) {// 0なら8方位ラベリング // prevがcurの左方にある→次のフラグメントを探索 index_prev++; continue; } else if (rle_prev[index_prev].l - rle_current[i].r > 0) {// 0なら8方位ラベリングになる // prevがcur右方にある→独立フラグメント addFragment(rle_current[i], id_max, y, rlestack); id_max++; label_count++; // 次のindexをしらべる goto SCAN_CUR; } id = rle_prev[index_prev].fid;//ルートフラグメントid RleInfoStack.RleInfo id_ptr = f_array[id]; //結合対象(初回)->prevのIDをコピーして、ルートフラグメントの情報を更新 rle_current[i].fid = id;//フラグメントIDを保存 // int l = rle_current[i].l; int r = rle_current[i].r; int len = r - l; //結合先フラグメントの情報を更新する。 id_ptr.area += len; //tとentry_xは、結合先のを使うので更新しない。 id_ptr.clip_l = l < id_ptr.clip_l ? l : id_ptr.clip_l; id_ptr.clip_r = r > id_ptr.clip_r ? r - 1 : id_ptr.clip_r; id_ptr.clip_b = y; id_ptr.pos_x += (len * (2 * l + (len - 1))) / 2; id_ptr.pos_y += y * len; //多重結合の確認(2個目以降) index_prev++; while (index_prev < len_prev) { if (rle_current[i].l - rle_prev[index_prev].r > 0) {// 0なら8方位ラベリング // prevがcurの左方にある→prevはcurに連結していない。 goto SCAN_PREV; } else if (rle_prev[index_prev].l - rle_current[i].r > 0) {// 0なら8方位ラベリングになる // prevがcurの右方にある→prevはcurに連結していない。 index_prev--; goto SCAN_CUR; } // prevとcurは連結している→ルートフラグメントの統合 //結合するルートフラグメントを取得 int prev_id = rle_prev[index_prev].fid; RleInfoStack.RleInfo prev_ptr = f_array[prev_id]; if (id != prev_id) { label_count--; //prevとcurrentのフラグメントidを書き換える。 for (int i2 = index_prev; i2 < len_prev; i2++) { //prevは現在のidから最後まで if (rle_prev[i2].fid == prev_id) { rle_prev[i2].fid = id; } } for (int i2 = 0; i2 < i; i2++) { //currentは0から現在-1まで if (rle_current[i2].fid == prev_id) { rle_current[i2].fid = id; } } //現在のルートフラグメントに情報を集約 id_ptr.area += prev_ptr.area; id_ptr.pos_x += prev_ptr.pos_x; id_ptr.pos_y += prev_ptr.pos_y; //tとentry_xの決定 if (id_ptr.clip_t > prev_ptr.clip_t) { // 現在の方が下にある。 id_ptr.clip_t = prev_ptr.clip_t; id_ptr.entry_x = prev_ptr.entry_x; } else if (id_ptr.clip_t < prev_ptr.clip_t) { // 現在の方が上にある。prevにフィードバック } else { // 水平方向で小さい方がエントリポイント。 if (id_ptr.entry_x > prev_ptr.entry_x) { id_ptr.entry_x = prev_ptr.entry_x; } else { } } //lの決定 if (id_ptr.clip_l > prev_ptr.clip_l) { id_ptr.clip_l = prev_ptr.clip_l; } else { } //rの決定 if (id_ptr.clip_r < prev_ptr.clip_r) { id_ptr.clip_r = prev_ptr.clip_r; } else { } //bの決定 //結合済のルートフラグメントを無効化する。 prev_ptr.area = 0; } index_prev++; } index_prev--; break; SCAN_PREV :; } // curにidが割り当てられたかを確認 // 右端独立フラグメントを追加 if (id < 0) { addFragment(rle_current[i], id_max, y, rlestack); id_max++; label_count++; } SCAN_CUR :; } // prevとrelの交換 RleElement[] tmp = rle_prev; rle_prev = rle_current; len_prev = len_current; rle_current = tmp; } //対象のラベルだけ転写 o_stack.init(label_count); RleLabelFragmentInfoStack.RleLabelFragmentInfo[] o_dest_array = o_stack.getArray(); int max = this._max_area; int min = this._min_area; int active_labels = 0; for (int i = id_max - 1; i >= 0; i--) { int area = f_array[i].area; if (area < min || area > max) {//対象外のエリア0のもminではじく continue; } // RleInfoStack.RleInfo src_info = f_array[i]; RleLabelFragmentInfoStack.RleLabelFragmentInfo dest_info = o_dest_array[active_labels]; dest_info.area = area; dest_info.clip_b = src_info.clip_b; dest_info.clip_r = src_info.clip_r; dest_info.clip_t = src_info.clip_t; dest_info.clip_l = src_info.clip_l; dest_info.entry_x = src_info.entry_x; dest_info.pos_x = src_info.pos_x / src_info.area; dest_info.pos_y = src_info.pos_y / src_info.area; active_labels++; } //ラベル数を再設定 o_stack.pops(label_count - active_labels); //ラベル数を返却 return(active_labels); }