/** * この関数は、ラスタの指定点を基点に、画像の特定の範囲内から輪郭線を抽出します。 * 開始点は、輪郭の一部である必要があります。 * 通常は、ラべリングの結果の上辺クリップとX軸エントリポイントを開始点として入力します。 * @param i_raster * 輪郭線を抽出するラスタを指定します。 * @param i_area * 輪郭線の抽出範囲を指定する矩形。i_rasterのサイズ内である必要があります。 * @param i_th * 輪郭とみなす暗点の敷居値を指定します。 * @param i_entry_x * 輪郭抽出の開始点です。 * @param i_entry_y * 輪郭抽出の開始点です。 * @param o_coord * 輪郭点を格納するオブジェクトを指定します。 * @return * 輪郭線がo_coordの長さを超えた場合、falseを返します。 * @ */ public bool getContour(INyARGrayscaleRaster i_raster, NyARIntRect i_area, int i_th, int i_entry_x, int i_entry_y, NyARIntCoordinates o_coord) { //ラスタドライバの切り替え if (i_raster != this._ref_last_input_raster) { this._imdriver = (IRasterDriver)i_raster.createInterface(typeof(IRasterDriver)); this._ref_last_input_raster = i_raster; } return(this._imdriver.getContour(i_area.x, i_area.y, i_area.x + i_area.w - 1, i_area.h + i_area.y - 1, i_entry_x, i_entry_y, i_th, o_coord)); }
/** * この関数は、ラスタから差分画像を生成して、インスタンスに格納します。 * @param i_raster * 差分画像の元画像。サイズは、このインスタンスと同じである必要があります。 * {@link NyARBufferType#INT1D_X8R8G8B8_32}形式のバッファを持つラスタの場合、他の形式よりも * 何倍か高速に動作します。 */ public void setRaster(INyARRgbRaster i_raster) { //ドライバの生成 if (this._last_input_raster != i_raster) { this._last_drv = (IRasterDriver)i_raster.createInterface(typeof(IRasterDriver)); this._last_input_raster = i_raster; } this._pow = this._last_drv.makeColorData(this._data); return; }
/** * この関数は、ラスタの指定点を基点に、輪郭線を抽出します。 * 開始点は、輪郭の一部である必要があります。 * 通常は、ラべリングの結果の上辺クリップとX軸エントリポイントを開始点として入力します。 * @param i_raster * 輪郭線を抽出するラスタを指定します。 * @param i_th * 輪郭とみなす暗点の敷居値を指定します。 * @param i_entry_x * 輪郭抽出の開始点です。 * @param i_entry_y * 輪郭抽出の開始点です。 * @param o_coord * 輪郭点を格納する配列を指定します。i_array_sizeよりも大きなサイズの配列が必要です。 * @return * 輪郭の抽出に成功するとtrueを返します。輪郭抽出に十分なバッファが無いと、falseになります。 * @ */ public bool getContour(INyARGrayscaleRaster i_raster, int i_th, int i_entry_x, int i_entry_y, NyARIntCoordinates o_coord) { NyARIntSize s = i_raster.getSize(); //ラスタドライバの切り替え if (i_raster != this._ref_last_input_raster) { this._imdriver = (IRasterDriver)i_raster.createInterface(typeof(IRasterDriver)); this._ref_last_input_raster = i_raster; } return(this._imdriver.getContour(0, 0, s.w - 1, s.h - 1, i_entry_x, i_entry_y, i_th, o_coord)); }
/** * この関数は、ラスタの指定点を基点に、輪郭線を抽出します。 * 開始点は、輪郭の一部である必要があります。 * 通常は、ラべリングの結果の上辺クリップとX軸エントリポイントを開始点として入力します。 * @param i_raster * 輪郭線を抽出するラスタを指定します。 * @param i_th * 輪郭とみなす暗点の敷居値を指定します。 * @param i_entry_x * 輪郭抽出の開始点です。 * @param i_entry_y * 輪郭抽出の開始点です。 * @param o_coord * 輪郭点を格納する配列を指定します。i_array_sizeよりも大きなサイズの配列が必要です。 * @return * 輪郭の抽出に成功するとtrueを返します。輪郭抽出に十分なバッファが無いと、falseになります。 * @ */ public bool getContour(INyARGrayscaleRaster i_raster, int i_th, int i_entry_x, int i_entry_y, NyARIntCoordinates o_coord) { NyARIntSize s = i_raster.getSize(); //ラスタドライバの切り替え if (i_raster != this._ref_last_input_raster) { this._imdriver = (IRasterDriver)i_raster.createInterface(typeof(IRasterDriver)); this._ref_last_input_raster = i_raster; } return this._imdriver.getContour(0, 0, s.w - 1, s.h - 1, i_entry_x, i_entry_y, i_th, o_coord); }
/** * この関数は、ラスタの指定点を基点に、画像の特定の範囲内から輪郭線を抽出します。 * 開始点は、輪郭の一部である必要があります。 * 通常は、ラべリングの結果の上辺クリップとX軸エントリポイントを開始点として入力します。 * @param i_raster * 輪郭線を抽出するラスタを指定します。 * @param i_area * 輪郭線の抽出範囲を指定する矩形。i_rasterのサイズ内である必要があります。 * @param i_th * 輪郭とみなす暗点の敷居値を指定します。 * @param i_entry_x * 輪郭抽出の開始点です。 * @param i_entry_y * 輪郭抽出の開始点です。 * @param o_coord * 輪郭点を格納するオブジェクトを指定します。 * @return * 輪郭線がo_coordの長さを超えた場合、falseを返します。 * @ */ public bool getContour(INyARGrayscaleRaster i_raster, NyARIntRect i_area, int i_th, int i_entry_x, int i_entry_y, NyARIntCoordinates o_coord) { //ラスタドライバの切り替え if (i_raster != this._ref_last_input_raster) { this._imdriver = (IRasterDriver)i_raster.createInterface(typeof(IRasterDriver)); this._ref_last_input_raster = i_raster; } return this._imdriver.getContour(i_area.x, i_area.y, i_area.x + i_area.w - 1, i_area.h + i_area.y - 1, i_entry_x, i_entry_y, i_th, o_coord); }
/** * * ラベリングの実体。 * @return * ラベル数が上限に達したときはfalse */ private bool imple_labeling(INyARRaster i_raster, int i_th, int i_left, int i_top, int i_width, int i_height) { //ラスタのサイズを確認 Debug.Assert(i_raster.getSize().isEqualSize(this._raster_size)); //ラスタドライバのチェック if (_last_input_raster != i_raster) { this._image_driver = (IRasterDriver)i_raster.createInterface(typeof(IRasterDriver)); } IRasterDriver pixdrv = this._image_driver; RleElement[] rle_prev = this._rle1; RleElement[] rle_current = this._rle2; // リセット処理 RleInfoStack rlestack = this._rlestack; rlestack.clear(); // int len_prev = 0; int len_current = 0; int bottom = i_top + i_height; int id_max = 0; int ypos = i_top; // 初段登録 len_prev = pixdrv.xLineToRle(i_left, ypos, i_width, i_th, rle_prev); for (int i = 0; i < len_prev; i++) { // フラグメントID=フラグメント初期値、POS=Y値、RELインデクス=行 if (addFragment(rle_prev[i], id_max, ypos, rlestack)) { id_max++; } else { return(false); } } NyARRleLabelFragmentInfo[] f_array = rlestack.getArray(); // 次段結合 for (int y = i_top + 1; y < bottom; y++) { // カレント行の読込 ypos++; len_current = pixdrv.xLineToRle(i_left, ypos, i_width, i_th, rle_current); 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右方にある→独立フラグメント { if (addFragment(rle_current[i], id_max, y, rlestack)) { id_max++; } else { return(false); } // 次のindexをしらべる goto SCAN_CUR; } id = rle_prev[index_prev].fid; //ルートフラグメントid NyARRleLabelFragmentInfo 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; NyARRleLabelFragmentInfo prev_ptr = f_array[prev_id]; if (id != prev_id) { //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) { if (addFragment(rle_current[i], id_max, y, rlestack)) { id_max++; } else { return(false); } } SCAN_CUR :; } // prevとrelの交換 RleElement[] tmp = rle_prev; rle_prev = rle_current; len_prev = len_current; rle_current = tmp; } //対象のラベルだけを追記 int max = this._max_area; int min = this._min_area; for (int i = id_max - 1; i >= 0; i--) { NyARRleLabelFragmentInfo src_info = f_array[i]; int area = src_info.area; if (area < min || area > max) //対象外のエリア0のもminではじく { continue; } //値を相対位置に補正 src_info.clip_l += i_left; src_info.clip_r += i_left; src_info.entry_x += i_left; src_info.pos_x /= area; src_info.pos_y /= area; //コールバック関数コール this.OnLabelFound(src_info); } return(true); }