/* \r
- * Capture Test NyARToolkitCSサンプルプログラム\r
+ * Capture Test NyARToolkitサンプルプログラム\r
* --------------------------------------------------------------------------------\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.processor;\r
\r
import jp.nyatla.nyartoolkit.NyARException;\r
-import jp.nyatla.nyartoolkit.core.*;\r
+import jp.nyatla.nyartoolkit.core.analyzer.raster.threshold.*;\r
import jp.nyatla.nyartoolkit.core.param.*;\r
import jp.nyatla.nyartoolkit.core.raster.*;\r
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.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;\r
import jp.nyatla.nyartoolkit.core.types.*;\r
import jp.nyatla.nyartoolkit.nyidmarker.*;\r
import jp.nyatla.nyartoolkit.nyidmarker.data.*;\r
-import jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold.*;\r
import jp.nyatla.nyartoolkit.core.squaredetect.*;\r
\r
public abstract class SingleNyIdMarkerProcesser\r
{\r
/**\r
+ * detectMarkerのコールバック関数\r
+ */\r
+ private class DetectSquareCB implements INyARSquareContourDetector.DetectMarkerCallback\r
+ {\r
+ //公開プロパティ\r
+ public final NyARSquare square=new NyARSquare();\r
+ public INyIdMarkerData marker_data;\r
+ public int threshold;\r
+\r
+ \r
+ //参照\r
+ private INyARRgbRaster _ref_raster;\r
+ //所有インスタンス\r
+ private INyIdMarkerData _current_data;\r
+ private final NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();\r
+ private Coord2Linear _coordline;\r
+ private INyIdMarkerDataEncoder _encoder;\r
+\r
+ \r
+ private INyIdMarkerData _data_temp;\r
+ private INyIdMarkerData _prev_data;\r
+ \r
+ public DetectSquareCB(NyARParam i_param,INyIdMarkerDataEncoder i_encoder)\r
+ {\r
+ this._coordline=new Coord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor());\r
+ this._data_temp=i_encoder.createDataInstance();\r
+ this._current_data=i_encoder.createDataInstance();\r
+ this._encoder=i_encoder;\r
+ return;\r
+ }\r
+ private NyARIntPoint2d[] __tmp_vertex=NyARIntPoint2d.createArray(4);\r
+ /**\r
+ * Initialize call back handler.\r
+ */\r
+ public void init(INyARRgbRaster i_raster,INyIdMarkerData i_prev_data)\r
+ {\r
+ this.marker_data=null;\r
+ this._prev_data=i_prev_data;\r
+ this._ref_raster=i_raster;\r
+ }\r
+ private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();\r
+ private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();\r
+\r
+ /**\r
+ * 矩形が見付かるたびに呼び出されます。\r
+ * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。\r
+ */\r
+ public void onSquareDetect(INyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException\r
+ {\r
+ //既に発見済なら終了\r
+ if(this.marker_data!=null){\r
+ return;\r
+ }\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
+ NyIdMarkerParam param=this._marker_param;\r
+ NyIdMarkerPattern patt_data =this._marker_data; \r
+ // 評価基準になるパターンをイメージから切り出す\r
+ if (!this._id_pickup.pickFromRaster(this._ref_raster,vertex, patt_data, param)){\r
+ return;\r
+ }\r
+ //エンコード\r
+ if(!this._encoder.encode(patt_data,this._data_temp)){\r
+ return;\r
+ }\r
+\r
+ //継続認識要求されている?\r
+ if (this._prev_data==null){\r
+ //継続認識要求なし\r
+ this._current_data.copyFrom(this._data_temp);\r
+ }else{\r
+ //継続認識要求あり\r
+ if(!this._prev_data.isEqual((this._data_temp))){\r
+ return;//認識請求のあったIDと違う。\r
+ }\r
+ }\r
+ //新しく認識、または継続認識中に更新があったときだけ、Square情報を更新する。\r
+ //ココから先はこの条件でしか実行されない。\r
+ NyARSquare sq=this.square;\r
+ //directionを考慮して、squareを更新する。\r
+ for(int i=0;i<4;i++){\r
+ int idx=(i+4 - param.direction) % 4;\r
+ sq.imvertex[i].x=vertex[idx].x;\r
+ sq.imvertex[i].y=vertex[idx].y;\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
+ this.threshold=param.threshold;\r
+ this.marker_data=this._current_data;//みつかった。\r
+ }\r
+ } \r
+ /**\r
* オーナーが自由に使えるタグ変数です。\r
*/\r
public Object tag;\r
private int _lost_delay_count = 0;\r
private int _lost_delay = 5;\r
\r
- private NyARSquareDetector_Rle _square_detect;\r
- protected NyARTransMat _transmat;\r
+ private NyARSquareContourDetector_Rle _square_detect;\r
+ protected INyARTransMat _transmat;\r
private double _marker_width=100;\r
-\r
- private NyARSquareStack _square_list = new NyARSquareStack(100);\r
- private INyIdMarkerDataEncoder _encoder;\r
private boolean _is_active;\r
- private INyIdMarkerData _data_temp;\r
- private INyIdMarkerData _data_current;\r
-\r
private int _current_threshold=110;\r
// [AR]検出結果の保存用\r
private NyARBinRaster _bin_raster;\r
-\r
private NyARRasterFilter_ARToolkitThreshold _tobin_filter;\r
-\r
- private NyIdMarkerPickup _id_pickup = new NyIdMarkerPickup();\r
+ private DetectSquareCB _callback;\r
+ private INyIdMarkerData _data_current;\r
\r
\r
protected SingleNyIdMarkerProcesser()\r
{\r
return;\r
}\r
+ private boolean _initialized=false;\r
protected void initInstance(NyARParam i_param,INyIdMarkerDataEncoder i_encoder,int i_raster_format) throws NyARException\r
{\r
+ //初期化済?\r
+ assert this._initialized==false;\r
+ \r
NyARIntSize scr_size = i_param.getScreenSize();\r
// 解析オブジェクトを作る\r
- this._square_detect = new NyARSquareDetector_Rle(i_param.getDistortionFactor(), scr_size);\r
+ this._square_detect = new NyARSquareContourDetector_Rle(i_param.getDistortionFactor(), scr_size);\r
this._transmat = new NyARTransMat(i_param);\r
- this._encoder=i_encoder;\r
+ this._callback=new DetectSquareCB(i_param,i_encoder);\r
\r
// 2値画像バッファを作る\r
this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);\r
//ワーク用のデータオブジェクトを2個作る\r
- this._is_active=false;\r
- this._data_temp=i_encoder.createDataInstance();\r
this._data_current=i_encoder.createDataInstance();\r
- this._tobin_filter = new NyARRasterFilter_ARToolkitThreshold(110,i_raster_format);\r
+ this._tobin_filter =new NyARRasterFilter_ARToolkitThreshold(110,i_raster_format);\r
this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_format,4);\r
+ this._initialized=true;\r
+ this._is_active=false;\r
return;\r
\r
}\r
\r
public void reset(boolean i_is_force)\r
{\r
- if (this._data_current!=null && i_is_force == false) {\r
+ if (i_is_force == false && this._is_active){\r
// 強制書き換えでなければイベントコール\r
this.onLeaveHandler();\r
}\r
- // カレントマーカをリセット\r
- this._data_current = null;\r
+ //マーカ無効\r
+ this._is_active=false;\r
return;\r
}\r
\r
this._tobin_filter.setThreshold(this._current_threshold);\r
this._tobin_filter.doFilter(i_raster, this._bin_raster);\r
\r
- NyARSquareStack square_stack = this._square_list;\r
- // スクエアコードを探す\r
- this._square_detect.detectMarker(this._bin_raster, square_stack);\r
- // 認識処理\r
- if (!this._is_active) {\r
- // マーカ未認識→新規認識\r
- detectNewMarker(i_raster, square_stack);\r
- } else {\r
- // マーカ認識依頼→継続認識\r
- detectExistMarker(i_raster, square_stack);\r
- }\r
- return;\r
- }\r
-\r
- \r
- private final NyIdMarkerPattern _marker_data=new NyIdMarkerPattern();\r
- private final NyIdMarkerParam _marker_param=new NyIdMarkerParam();\r
- private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;\r
- \r
- /**新規マーカ検索 現在認識中のマーカがないものとして、最も認識しやすいマーカを1個認識します。\r
- */\r
- private void detectNewMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException\r
- {\r
- NyIdMarkerParam param=this._marker_param;\r
- NyIdMarkerPattern patt_data =this._marker_data;\r
- int number_of_square = i_stack.getLength();\r
- NyARSquare current_square=null;\r
- INyIdMarkerData marker_id=null;\r
- for (int i = 0; i < number_of_square; i++) {\r
- // 評価基準になるパターンをイメージから切り出す\r
- current_square=i_stack.getItem(i);\r
- if (!this._id_pickup.pickFromRaster(i_raster,current_square, patt_data, param)) {\r
- continue;\r
- }\r
- //エンコード\r
- if(!this._encoder.encode(patt_data,this._data_temp)){\r
- continue;\r
- }\r
- //認識率が一番高いもの(占有面積が一番大きいもの)を選択する(省略)\r
- //id認識が成功したら終了\r
- marker_id=this._data_temp;\r
- break;\r
- }\r
- \r
- // 認識状態を更新\r
- final boolean is_id_found=updateStatus(current_square,marker_id, param);\r
-\r
- //閾値フィードバック(detectExistMarkerにもあるよ)\r
- if(is_id_found){\r
- //マーカがあれば、マーカの周辺閾値を反映\r
- this._current_threshold=(this._current_threshold+param.threshold)/2;\r
- }else{\r
- //マーカがなければ、探索+DualPTailで基準輝度検索\r
- this._threshold_detect.analyzeRaster(i_raster);\r
- this._current_threshold=(this._current_threshold+this._threshold_detect.getThreshold())/2;\r
- }\r
- return;\r
- }\r
+ // スクエアコードを探す(第二引数に指定したマーカ、もしくは新しいマーカを探す。)\r
+ this._callback.init(i_raster,this._is_active?this._data_current:null);\r
+ this._square_detect.detectMarkerCB(this._bin_raster, this._callback);\r
\r
- /**マーカの継続認識 現在認識中のマーカを優先して認識します。 \r
- * (注)この機能はたぶん今後いろいろ発展するからNewと混ぜないこと。\r
- */\r
- private void detectExistMarker(INyARRgbRaster i_raster, NyARSquareStack i_stack) throws NyARException\r
- {\r
- NyIdMarkerParam param=this._marker_param;\r
- NyIdMarkerPattern patt_data =this._marker_data;\r
- int number_of_square = i_stack.getLength();\r
- NyARSquare current_square=null;\r
- INyIdMarkerData marker_id=null;\r
- for (int i = 0; i < number_of_square; i++){\r
- //idマーカを認識\r
- current_square=i_stack.getItem(i);\r
- if (!this._id_pickup.pickFromRaster(i_raster, current_square, patt_data, param)) {\r
- continue;\r
- }\r
- if(!this._encoder.encode(patt_data,this._data_temp)){\r
- continue;\r
- }\r
- //現在認識中のidか確認\r
- if(!this._data_current.isEqual((this._data_temp))){\r
- continue;\r
- }\r
- //現在認識中のものであれば、終了\r
- marker_id=this._data_temp;\r
- break;\r
- }\r
- // 認識状態を更新\r
- final boolean is_id_found=updateStatus(current_square,marker_id,param);\r
+ // 認識状態を更新(マーカを発見したなら、current_dataを渡すかんじ)\r
+ final boolean is_id_found=updateStatus(this._callback.square,this._callback.marker_data);\r
\r
//閾値フィードバック(detectExistMarkerにもあるよ)\r
if(is_id_found){\r
//マーカがあれば、マーカの周辺閾値を反映\r
- this._current_threshold=(this._current_threshold+param.threshold)/2;\r
+ this._current_threshold=(this._current_threshold+this._callback.threshold)/2;\r
}else{\r
//マーカがなければ、探索+DualPTailで基準輝度検索\r
- this._threshold_detect.analyzeRaster(i_raster);\r
- this._current_threshold=(this._current_threshold+this._threshold_detect.getThreshold())/2;\r
- }\r
+ int th=this._threshold_detect.analyzeRaster(i_raster);\r
+ this._current_threshold=(this._current_threshold+th)/2;\r
+ } \r
return;\r
}\r
\r
+ \r
+ private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect;\r
private NyARTransMatResult __NyARSquare_result = new NyARTransMatResult();\r
\r
/**オブジェクトのステータスを更新し、必要に応じてハンドル関数を駆動します。\r
*/\r
- private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data,NyIdMarkerParam i_param) throws NyARException\r
+ private boolean updateStatus(NyARSquare i_square, INyIdMarkerData i_marker_data) throws NyARException\r
{\r
boolean is_id_found=false;\r
NyARTransMatResult result = this.__NyARSquare_result;\r
// OnEnter\r
this.onEnterHandler(this._data_current);\r
// 変換行列を作成\r
- this._transmat.transMat(i_square,i_param.direction, this._marker_width, result);\r
+ this._transmat.transMat(i_square, this._marker_width, result);\r
// OnUpdate\r
this.onUpdateHandler(i_square, result);\r
this._lost_delay_count = 0;\r
}\r
} else if(this._data_current.isEqual(i_marker_data)) {\r
//同じidの再認識\r
- this._transmat.transMat(i_square, i_param.direction, this._marker_width, result);\r
+ this._transmat.transMat(i_square, this._marker_width, result);\r
// OnUpdate\r
this.onUpdateHandler(i_square, result);\r
this._lost_delay_count = 0;\r