OSDN Git Service

[backup]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / detector / NyARDetectMarker.java
index 568d315..e2a1db2 100644 (file)
@@ -7,26 +7,25 @@
  *   HITLab, University of Washington, Seattle\r
  * http://www.hitl.washington.edu/artoolkit/\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.detector;\r
@@ -40,65 +39,158 @@ import jp.nyatla.nyartoolkit.core.raster.*;
 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
 import jp.nyatla.nyartoolkit.core.transmat.*;\r
 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;\r
+import jp.nyatla.nyartoolkit.core.squaredetect.NyARCoord2Linear;\r
+import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector;\r
+import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
+import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;\r
 import jp.nyatla.nyartoolkit.core.types.*;\r
+import jp.nyatla.nyartoolkit.core.types.stack.NyARObjectStack;\r
 \r
 class NyARDetectMarkerResult\r
 {\r
        public int arcode_id;\r
-\r
-       public int direction;\r
-\r
        public double confidence;\r
 \r
-       public NyARSquare ref_square;\r
+       public NyARSquare square=new NyARSquare();\r
 }\r
 \r
-class NyARDetectMarkerResultHolder\r
-{\r
-       public NyARDetectMarkerResult[] result_array = new NyARDetectMarkerResult[1];\r
 \r
-       /**\r
-        * result_holderを最大i_reserve_size個の要素を格納できるように予約します。\r
-        * \r
-        * @param i_reserve_size\r
-        */\r
-       public void reservHolder(int i_reserve_size)\r
+class NyARDetectMarkerResultStack extends NyARObjectStack<NyARDetectMarkerResult>\r
+{\r
+       public NyARDetectMarkerResultStack(int i_length) throws NyARException\r
        {\r
-               if (i_reserve_size >= result_array.length) {\r
-                       int new_size = i_reserve_size + 5;\r
-                       result_array = new NyARDetectMarkerResult[new_size];\r
-                       for (int i = 0; i < new_size; i++) {\r
-                               result_array[i] = new NyARDetectMarkerResult();\r
-                       }\r
-               }\r
+               super(i_length,NyARDetectMarkerResult.class);\r
+               return;\r
        }\r
+       protected NyARDetectMarkerResult createElement()\r
+       {\r
+               return new NyARDetectMarkerResult();\r
+       }       \r
 }\r
 \r
+\r
 /**\r
  * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。\r
  * \r
  */\r
 public class NyARDetectMarker\r
 {\r
-       private static final int AR_SQUARE_MAX = 300;\r
+       /**\r
+        * detectMarkerのコールバック関数\r
+        */\r
+       private class DetectSquareCB implements NyARSquareContourDetector.IDetectMarkerCallback\r
+       {\r
+               //公開プロパティ\r
+               public NyARDetectMarkerResultStack result_stack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX);\r
+               //参照インスタンス\r
+               public INyARRgbRaster _ref_raster;\r
+               //所有インスタンス\r
+               private INyARColorPatt _inst_patt;\r
+               private NyARMatchPattDeviationColorData _deviation_data;\r
+               private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;\r
+               private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();\r
+               private NyARCoord2Linear _coordline;\r
+               \r
+               public DetectSquareCB(INyARColorPatt i_inst_patt,NyARCode[] i_ref_code,int i_num_of_code,NyARParam i_param) throws NyARException\r
+               {\r
+                       final int cw = i_ref_code[0].getWidth();\r
+                       final int ch = i_ref_code[0].getHeight();\r
+\r
+                       this._inst_patt=i_inst_patt;\r
+                       this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());\r
+                       this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);\r
+\r
+                       //NyARMatchPatt_Color_WITHOUT_PCA[]の作成\r
+                       this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_num_of_code];\r
+                       this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]);\r
+                       for (int i = 1; i < i_num_of_code; i++){\r
+                               //解像度チェック\r
+                               if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) {\r
+                                       throw new NyARException();\r
+                               }\r
+                               this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]);\r
+                       }\r
+                       return;\r
+               }\r
+               private NyARIntPoint2d[] __tmp_vertex=NyARIntPoint2d.createArray(4);\r
+               /**\r
+                * 矩形が見付かるたびに呼び出されます。\r
+                * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。\r
+                */\r
+               public void onSquareDetect(NyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException\r
+               {\r
+                       NyARMatchPattResult mr=this.__detectMarkerLite_mr;\r
+                       //輪郭座標から頂点リストに変換\r
+                       NyARIntPoint2d[] vertex=this.__tmp_vertex;\r
+                       vertex[0].x=i_coordx[i_vertex_index[0]];\r
+                       vertex[0].y=i_coordy[i_vertex_index[0]];\r
+                       vertex[1].x=i_coordx[i_vertex_index[1]];\r
+                       vertex[1].y=i_coordy[i_vertex_index[1]];\r
+                       vertex[2].x=i_coordx[i_vertex_index[2]];\r
+                       vertex[2].y=i_coordy[i_vertex_index[2]];\r
+                       vertex[3].x=i_coordx[i_vertex_index[3]];\r
+                       vertex[3].y=i_coordy[i_vertex_index[3]];\r
+               \r
+                       //画像を取得\r
+                       if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){\r
+                               return;\r
+                       }\r
+                       //取得パターンをカラー差分データに変換して評価する。\r
+                       this._deviation_data.setRaster(this._inst_patt);\r
+\r
+                       //最も一致するパターンを割り当てる。\r
+                       int square_index,direction;\r
+                       double confidence;\r
+                       this._match_patt[0].evaluate(this._deviation_data,mr);\r
+                       square_index=0;\r
+                       direction=mr.direction;\r
+                       confidence=mr.confidence;\r
+                       //2番目以降\r
+                       for(int i=1;i<this._match_patt.length;i++){\r
+                               this._match_patt[i].evaluate(this._deviation_data,mr);\r
+                               if (confidence > mr.confidence) {\r
+                                       continue;\r
+                               }\r
+                               // もっと一致するマーカーがあったぽい\r
+                               square_index = i;\r
+                               direction = mr.direction;\r
+                               confidence = mr.confidence;\r
+                       }\r
+                       //最も一致したマーカ情報を、この矩形の情報として記録する。\r
+                       final NyARDetectMarkerResult result = this.result_stack.prePush();\r
+                       result.arcode_id = square_index;\r
+                       result.confidence = confidence;\r
 \r
+                       final NyARSquare sq=result.square;\r
+                       //directionを考慮して、squareを更新する。\r
+                       for(int i=0;i<4;i++){\r
+                               int idx=(i+4 - direction) % 4;\r
+                               this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]);\r
+                       }\r
+                       for (int i = 0; i < 4; i++) {\r
+                               //直線同士の交点計算\r
+                               if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){\r
+                                       throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK\r
+                               }\r
+                       }\r
+               }\r
+               public void init(INyARRgbRaster i_raster)\r
+               {\r
+                       this._ref_raster=i_raster;\r
+                       this.result_stack.clear();\r
+                       \r
+               }\r
+       }       \r
+       private DetectSquareCB _detect_cb;\r
+       \r
+       \r
+       private static final int AR_SQUARE_MAX = 300;\r
        private boolean _is_continue = false;\r
-\r
-       private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt;\r
-\r
-       private INyARSquareDetector _square_detect;\r
-\r
-       private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX);\r
-\r
+       private NyARSquareContourDetector _square_detect;\r
        protected INyARTransMat _transmat;\r
+       private NyARRectOffset[] _offset;       \r
 \r
-       private double[] _marker_width; \r
-\r
-       // 検出結果の保存用\r
-       private INyARColorPatt _patt;\r
 \r
-       private NyARDetectMarkerResultHolder _result_holder = new NyARDetectMarkerResultHolder();\r
-       private NyARMatchPattDeviationColorData _deviation_data;\r
        /**\r
         * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。\r
         * \r
@@ -112,42 +204,50 @@ public class NyARDetectMarker
         * i_codeのマーカーサイズをミリメートルで指定した配列を指定します。 先頭からi_number_of_code個の要素には、有効な値を指定する必要があります。\r
         * @param i_number_of_code\r
         * i_codeに含まれる、ARCodeの数を指定します。\r
+        * @param i_input_raster_type\r
+        * 入力ラスタのピクセルタイプを指定します。この値は、INyARBufferReaderインタフェイスのgetBufferTypeの戻り値を指定します。\r
         * @throws NyARException\r
         */\r
-       public NyARDetectMarker(NyARParam i_param,NyARCode[] i_code,double[] i_marker_width, int i_number_of_code) throws NyARException\r
+       public NyARDetectMarker(NyARParam i_param,NyARCode[] i_code,double[] i_marker_width, int i_number_of_code,int i_input_raster_type) throws NyARException\r
        {\r
-               final NyARIntSize scr_size=i_param.getScreenSize();\r
+               initInstance(i_param,i_code,i_marker_width,i_number_of_code,i_input_raster_type);\r
+               return;\r
+       }\r
+       protected void initInstance(\r
+               NyARParam       i_ref_param,\r
+               NyARCode[]      i_ref_code,\r
+               double[]        i_marker_width,\r
+               int                     i_number_of_code,\r
+               int i_input_raster_type) throws NyARException\r
+       {\r
+\r
+               final NyARIntSize scr_size=i_ref_param.getScreenSize();\r
                // 解析オブジェクトを作る\r
-               this._square_detect = new NyARSquareDetector(i_param.getDistortionFactor(),scr_size);\r
-               this._transmat = new NyARTransMat(i_param);\r
+               final int cw = i_ref_code[0].getWidth();\r
+               final int ch = i_ref_code[0].getHeight();\r
+\r
+               //detectMarkerのコールバック関数\r
+               this._detect_cb=new DetectSquareCB(\r
+                       new NyARColorPatt_Perspective_O2(cw, ch,4,25),\r
+                       i_ref_code,i_number_of_code,i_ref_param);\r
+               this._transmat = new NyARTransMat(i_ref_param);\r
+               //NyARToolkitプロファイル\r
+               this._square_detect =new NyARSquareContourDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize());\r
+               this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type);\r
 \r
-               //各コード用の比較器を作る。\r
-               this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_number_of_code];\r
-               final int cw = i_code[0].getWidth();\r
-               final int ch = i_code[0].getHeight();\r
-               this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_code[0]);\r
-               for (int i = 1; i < i_number_of_code; i++){\r
-                       //解像度チェック\r
-                       if (cw != i_code[i].getWidth() || ch != i_code[i].getHeight()) {\r
-                               throw new NyARException();\r
-                       }\r
-                       this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_code[i]);\r
-               }               \r
-               // 評価パターンのホルダを作る\r
-               this._patt = new NyARColorPatt_O3(cw, ch);\r
                //実サイズ保存\r
-               this._marker_width = i_marker_width;\r
-               //差分データインスタンスの作成\r
-               this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch);\r
+               this._offset = NyARRectOffset.createArray(i_number_of_code);\r
+               for(int i=0;i<i_number_of_code;i++){\r
+                       this._offset[i].setSquare(i_marker_width[i]);\r
+               }\r
                //2値画像バッファを作る\r
                this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h);\r
-               return;\r
+               return;         \r
        }\r
-\r
+       \r
        private NyARBinRaster _bin_raster;\r
 \r
-       private NyARRasterFilter_ARToolkitThreshold _tobin_filter = new NyARRasterFilter_ARToolkitThreshold(100);\r
-       private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult();\r
+       private INyARRasterFilter_Rgb2Bin _tobin_filter;\r
 \r
        /**\r
         * i_imageにマーカー検出処理を実行し、結果を記録します。\r
@@ -167,57 +267,15 @@ public class NyARDetectMarker
                }\r
 \r
                // ラスタを2値イメージに変換する.\r
-               this._tobin_filter.setThreshold(i_threshold);\r
+               ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold);\r
                this._tobin_filter.doFilter(i_raster, this._bin_raster);\r
 \r
-               NyARSquareStack l_square_list = this._square_list;\r
-               // スクエアコードを探す\r
-               this._square_detect.detectMarker(this._bin_raster, l_square_list);\r
-\r
-               final int number_of_square = l_square_list.getLength();\r
-               // コードは見つかった?\r
-               if (number_of_square < 1) {\r
-                       // ないや。おしまい。\r
-                       return 0;\r
-               }\r
-               // 保持リストのサイズを調整\r
-               this._result_holder.reservHolder(number_of_square);\r
-               NyARMatchPattResult mr=this.__detectMarkerLite_mr;\r
-\r
-               // 1スクエア毎に、一致するコードを決定していく\r
-               for (int i = 0; i < number_of_square; i++) {\r
-                       NyARSquare square = (NyARSquare)l_square_list.getItem(i);\r
-\r
-                       // 評価基準になるパターンをイメージから切り出す\r
-                       if (!this._patt.pickFromRaster(i_raster, square)) {\r
-                               // イメージの切り出しは失敗することもある。\r
-                               continue;\r
-                       }\r
-                       //取得パターンをカラー差分データに変換する。\r
-                       this._deviation_data.setRaster(this._patt);\r
-                       int square_index = 0;\r
-                       int direction = NyARSquare.DIRECTION_UNKNOWN;\r
-                       double confidence = 0;\r
-                       for(int i2=0;i2<this._match_patt.length;i2++){\r
-                               this._match_patt[i2].evaluate(this._deviation_data,mr);\r
+               //detect\r
+               this._detect_cb.init(i_raster);\r
+               this._square_detect.detectMarkerCB(this._bin_raster,this._detect_cb);\r
 \r
-                               final double c2 = mr.confidence;\r
-                               if (confidence > c2) {\r
-                                       continue;\r
-                               }\r
-                               // もっと一致するマーカーがあったぽい\r
-                               square_index = i2;\r
-                               direction = mr.direction;\r
-                               confidence = c2;\r
-                       }\r
-                       // i番目のパターン情報を記録する。\r
-                       final NyARDetectMarkerResult result = this._result_holder.result_array[i];\r
-                       result.arcode_id = square_index;\r
-                       result.confidence = confidence;\r
-                       result.direction = direction;\r
-                       result.ref_square = square;\r
-               }\r
-               return number_of_square;\r
+               //見付かった数を返す。\r
+               return this._detect_cb.result_stack.getLength();\r
        }\r
 \r
        /**\r
@@ -231,12 +289,12 @@ public class NyARDetectMarker
         */\r
        public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException\r
        {\r
-               final NyARDetectMarkerResult result = this._result_holder.result_array[i_index];\r
+               final NyARDetectMarkerResult result = this._detect_cb.result_stack.getItem(i_index);\r
                // 一番一致したマーカーの位置とかその辺を計算\r
                if (_is_continue) {\r
-                       _transmat.transMatContinue(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);\r
+                       _transmat.transMatContinue(result.square, this._offset[result.arcode_id], o_result);\r
                } else {\r
-                       _transmat.transMat(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result);\r
+                       _transmat.transMat(result.square, this._offset[result.arcode_id], o_result);\r
                }\r
                return;\r
        }\r
@@ -251,21 +309,8 @@ public class NyARDetectMarker
         */\r
        public double getConfidence(int i_index)\r
        {\r
-               return this._result_holder.result_array[i_index].confidence;\r
+               return this._detect_cb.result_stack.getItem(i_index).confidence;\r
        }\r
-\r
-       /**\r
-        * i_indexのマーカーの方位を返します。\r
-        * \r
-        * @param i_index\r
-        * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。\r
-        * @return 0,1,2,3の何れかを返します。\r
-        */\r
-       public int getDirection(int i_index)\r
-       {\r
-               return this._result_holder.result_array[i_index].direction;\r
-       }\r
-\r
        /**\r
         * i_indexのマーカーのARCodeインデックスを返します。\r
         * \r
@@ -275,7 +320,7 @@ public class NyARDetectMarker
         */\r
        public int getARCodeIndex(int i_index)\r
        {\r
-               return this._result_holder.result_array[i_index].arcode_id;\r
+               return this._detect_cb.result_stack.getItem(i_index).arcode_id;\r
        }\r
 \r
        /**\r