OSDN Git Service

[backup]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / labeling / rlelabeling / NyARLabeling_Rle.java
index e2639e7..00f00cf 100644 (file)
@@ -1,35 +1,34 @@
 /* \r
  * PROJECT: NyARToolkit(Extension)\r
  * --------------------------------------------------------------------------------\r
- * The NyARToolkit is Java version ARToolkit class library.\r
- * Copyright (C)2008 R.Iizuka\r
+ * The NyARToolkit is Java edition ARToolKit class library.\r
+ * Copyright (C)2008-2009 Ryo Iizuka\r
  *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
  * \r
  * This program is distributed in the hope that it will be useful,\r
  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
  * GNU General Public License for more details.\r
- * \r
+ *\r
  * You should have received a copy of the GNU General Public License\r
- * along with this framework; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
  * \r
  * For further information please contact.\r
  *     http://nyatla.jp/nyatoolkit/\r
- *     <airmail(at)ebony.plala.or.jp>\r
+ *     <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
  * \r
  */\r
 package jp.nyatla.nyartoolkit.core.labeling.rlelabeling;\r
 \r
 import jp.nyatla.nyartoolkit.NyARException;\r
 import jp.nyatla.nyartoolkit.core.raster.*;\r
-import jp.nyatla.utils.*;\r
+import jp.nyatla.nyartoolkit.core.types.stack.NyARObjectStack;\r
 \r
-class RleInfoStack extends NyObjectStack<RleInfoStack.RleInfo>\r
+class RleInfoStack extends NyARObjectStack<RleInfoStack.RleInfo>\r
 {\r
        public class RleInfo\r
        {\r
@@ -43,7 +42,7 @@ class RleInfoStack extends NyObjectStack<RleInfoStack.RleInfo>
                public long pos_x;\r
                public long pos_y;              \r
        }       \r
-       public RleInfoStack(int i_length)\r
+       public RleInfoStack(int i_length) throws NyARException\r
        {\r
                super(i_length, RleInfoStack.RleInfo.class);\r
                return;\r
@@ -74,6 +73,7 @@ class RleElement
        }\r
 }\r
 \r
+\r
 // RleImageをラベリングする。\r
 public class NyARLabeling_Rle\r
 {\r
@@ -86,7 +86,7 @@ public class NyARLabeling_Rle
        private int _max_area;\r
        private int _min_area;\r
 \r
-       public NyARLabeling_Rle(int i_width,int i_height)\r
+       public NyARLabeling_Rle(int i_width,int i_height) throws NyARException\r
        {\r
                this._rlestack=new RleInfoStack(i_width*i_height*2048/(320*240)+32);\r
                this._rle1 = RleElement.createArray(i_width/2+1);\r
@@ -108,11 +108,18 @@ public class NyARLabeling_Rle
        }\r
 \r
        /**\r
-        * i_bin_bufのbinイメージをREL圧縮する。\r
-        * \r
-        * @param i_bin_raster\r
+        * i_bin_bufのgsイメージをREL圧縮する。\r
+        * @param i_bin_buf\r
+        * @param i_st\r
+        * @param i_len\r
+        * @param i_out\r
+        * @param i_th\r
+        * BINラスタのときは0,GSラスタの時は閾値を指定する。\r
+        * この関数は、閾値を暗点と認識します。\r
+        * 暗点<=th<明点\r
+        * @return\r
         */\r
-       private int toRel(int[] i_bin_buf, int i_st, int i_len, RleElement[] i_out)\r
+       private int toRel(int[] i_bin_buf, int i_st, int i_len, RleElement[] i_out,int i_th)\r
        {\r
                int current = 0;\r
                int r = -1;\r
@@ -121,8 +128,8 @@ public class NyARLabeling_Rle
                final int right_edge = i_st + i_len - 1;\r
                while (x < right_edge) {\r
                        // 暗点(0)スキャン\r
-                       if (i_bin_buf[x] != 0) {\r
-                               x++;\r
+                       if (i_bin_buf[x] > i_th) {\r
+                               x++;//明点\r
                                continue;\r
                        }\r
                        // 暗点発見→暗点長を調べる\r
@@ -131,7 +138,7 @@ public class NyARLabeling_Rle
                        r++;// 暗点+1\r
                        x++;\r
                        while (x < right_edge) {\r
-                               if (i_bin_buf[x] != 0) {\r
+                               if (i_bin_buf[x] > i_th) {\r
                                        // 明点(1)→暗点(0)配列終了>登録\r
                                        i_out[current].r = r;\r
                                        current++;\r
@@ -146,7 +153,7 @@ public class NyARLabeling_Rle
                        }\r
                }\r
                // 最後の1点だけ判定方法が少し違うの。\r
-               if (i_bin_buf[x] != 0) {\r
+               if (i_bin_buf[x] > i_th) {\r
                        // 明点→rカウント中なら暗点配列終了>登録\r
                        if (r >= 0) {\r
                                i_out[current].r = r;\r
@@ -184,9 +191,39 @@ public class NyARLabeling_Rle
 \r
                return;\r
        }\r
-\r
-       //\r
-       public int labeling(NyARBinRaster i_bin_raster, int i_top, int i_bottom,RleLabelFragmentInfoStack o_stack) throws NyARException\r
+       //所望のラスタからBIN-RLEに変換しながらの低速系も準備しようかな\r
+       \r
+       /**\r
+        * 単一閾値を使ってGSラスタをBINラスタに変換しながらラベリングします。\r
+        * @param i_gs_raster\r
+        * @param i_top\r
+        * @param i_bottom\r
+        * @param o_stack\r
+        * @return\r
+        * @throws NyARException\r
+        */\r
+       public int labeling(NyARBinRaster i_bin_raster, int i_top, int i_bottom,NyARRleLabelFragmentInfoStack o_stack) throws NyARException\r
+       {\r
+               assert(i_bin_raster.isEqualBufferType(NyARBufferType.INT1D_BIN_8));\r
+               return this.imple_labeling(i_bin_raster,0,i_top,i_bottom,o_stack);\r
+       }\r
+       /**\r
+        * BINラスタをラベリングします。\r
+        * @param i_gs_raster\r
+        * @param i_th\r
+        * 画像を2値化するための閾値。暗点<=th<明点となります。\r
+        * @param i_top\r
+        * @param i_bottom\r
+        * @param o_stack\r
+        * @return\r
+        * @throws NyARException\r
+        */\r
+       public int labeling(NyARGrayscaleRaster i_gs_raster,int i_th, int i_top, int i_bottom,NyARRleLabelFragmentInfoStack o_stack) throws NyARException\r
+       {\r
+               assert(i_gs_raster.isEqualBufferType(NyARBufferType.INT1D_GRAY_8));\r
+               return this.imple_labeling(i_gs_raster,i_th,i_top,i_bottom,o_stack);\r
+       }\r
+       private int imple_labeling(INyARRaster i_raster,int i_th,int i_top, int i_bottom,NyARRleLabelFragmentInfoStack o_stack) throws NyARException\r
        {\r
                // リセット処理\r
                final RleInfoStack rlestack=this._rlestack;\r
@@ -197,14 +234,14 @@ public class NyARLabeling_Rle
                RleElement[] rle_current = this._rle2;\r
                int len_prev = 0;\r
                int len_current = 0;\r
-               final int width = i_bin_raster.getWidth();\r
-               int[] in_buf = (int[]) i_bin_raster.getBufferReader().getBuffer();\r
+               final int width = i_raster.getWidth();\r
+               int[] in_buf = (int[]) i_raster.getBuffer();\r
 \r
                int id_max = 0;\r
                int label_count=0;\r
                // 初段登録\r
 \r
-               len_prev = toRel(in_buf, i_top, width, rle_prev);\r
+               len_prev = toRel(in_buf, i_top, width, rle_prev,i_th);\r
                for (int i = 0; i < len_prev; i++) {\r
                        // フラグメントID=フラグメント初期値、POS=Y値、RELインデクス=行\r
                        addFragment(rle_prev[i], id_max, i_top,rlestack);\r
@@ -216,7 +253,7 @@ public class NyARLabeling_Rle
                // 次段結合\r
                for (int y = i_top + 1; y < i_bottom; y++) {\r
                        // カレント行の読込\r
-                       len_current = toRel(in_buf, y * width, width, rle_current);\r
+                       len_current = toRel(in_buf, y * width, width, rle_current,i_th);\r
                        int index_prev = 0;\r
 \r
                        SCAN_CUR: for (int i = 0; i < len_current; i++) {\r
@@ -339,8 +376,8 @@ public class NyARLabeling_Rle
                        rle_current = tmp;\r
                }\r
                //対象のラベルだけ転写\r
-               o_stack.reserv(label_count);\r
-               RleLabelFragmentInfoStack.RleLabelFragmentInfo[] o_dest_array=o_stack.getArray();\r
+               o_stack.init(label_count);\r
+               NyARRleLabelFragmentInfoStack.RleLabelFragmentInfo[] o_dest_array=o_stack.getArray();\r
                final int max=this._max_area;\r
                final int min=this._min_area;\r
                int active_labels=0;\r
@@ -351,7 +388,7 @@ public class NyARLabeling_Rle
                        }\r
                        //\r
                        final RleInfoStack.RleInfo src_info=f_array[i];\r
-                       final RleLabelFragmentInfoStack.RleLabelFragmentInfo dest_info=o_dest_array[active_labels];\r
+                       final NyARRleLabelFragmentInfoStack.RleLabelFragmentInfo dest_info=o_dest_array[active_labels];\r
                        dest_info.area=area;\r
                        dest_info.clip_b=src_info.clip_b;\r
                        dest_info.clip_r=src_info.clip_r;\r