Example #1
0
        /**
         *
         * ラベリングの実体。
         * @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);
        }