| 225 | | |
|---|
| 226 | | import jp.nyatla.nyartoolkit.as3.core.match.*; |
|---|
| 227 | | import jp.nyatla.nyartoolkit.as3.core.pickup.*; |
|---|
| 228 | | import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; |
|---|
| 229 | | import jp.nyatla.nyartoolkit.as3.*; |
|---|
| 230 | | import jp.nyatla.nyartoolkit.as3.core.types.stack.*; |
|---|
| 231 | | import jp.nyatla.nyartoolkit.as3.core.match.*; |
|---|
| 232 | | import jp.nyatla.nyartoolkit.as3.core.param.*; |
|---|
| 233 | | import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; |
|---|
| 234 | | import jp.nyatla.nyartoolkit.as3.detector.*; |
|---|
| 235 | | import jp.nyatla.nyartoolkit.as3.core.types.*; |
|---|
| 236 | | import org.libspark.flartoolkit.core.* |
|---|
| 237 | | /** |
|---|
| 238 | | * detectMarkerのコールバック関数 |
|---|
| 239 | | */ |
|---|
| 240 | | class DetectSquareCB implements NyARSquareContourDetector_IDetectMarkerCallback |
|---|
| 241 | | { |
|---|
| 242 | | //公開プロパティ |
|---|
| 243 | | public var result_stack:NyARDetectMarkerResultStack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX); |
|---|
| 244 | | //参照インスタンス |
|---|
| 245 | | public var _ref_raster:INyARRgbRaster; |
|---|
| 246 | | //所有インスタンス |
|---|
| 247 | | private var _inst_patt:INyARColorPatt; |
|---|
| 248 | | private var _deviation_data:NyARMatchPattDeviationColorData; |
|---|
| 249 | | private var _match_patt:Vector.<NyARMatchPatt_Color_WITHOUT_PCA>; |
|---|
| 250 | | private var __detectMarkerLite_mr:NyARMatchPattResult=new NyARMatchPattResult(); |
|---|
| 251 | | private var _coordline:NyARCoord2Linear; |
|---|
| 252 | | |
|---|
| 253 | | public function DetectSquareCB(i_inst_patt:INyARColorPatt, i_ref_code:Vector.<FLARCode>, i_num_of_code:int, i_param:NyARParam) |
|---|
| 254 | | { |
|---|
| 255 | | var cw:int = i_ref_code[0].getWidth(); |
|---|
| 256 | | var ch:int = i_ref_code[0].getHeight(); |
|---|
| 257 | | |
|---|
| 258 | | this._inst_patt=i_inst_patt; |
|---|
| 259 | | this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); |
|---|
| 260 | | this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch); |
|---|
| 261 | | |
|---|
| 262 | | //NyARMatchPatt_Color_WITHOUT_PCA[]の作成 |
|---|
| 263 | | this._match_patt=new Vector.<NyARMatchPatt_Color_WITHOUT_PCA>(i_num_of_code); |
|---|
| 264 | | this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]); |
|---|
| 265 | | for (var i:int = 1; i < i_num_of_code; i++){ |
|---|
| 266 | | //解像度チェック |
|---|
| 267 | | if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) { |
|---|
| 268 | | throw new NyARException(); |
|---|
| 269 | | } |
|---|
| 270 | | this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]); |
|---|
| 271 | | } |
|---|
| 272 | | return; |
|---|
| 273 | | } |
|---|
| 274 | | private var __tmp_vertex:Vector.<NyARIntPoint2d>=NyARIntPoint2d.createArray(4); |
|---|
| 275 | | /** |
|---|
| 276 | | * 矩形が見付かるたびに呼び出されます。 |
|---|
| 277 | | * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 |
|---|
| 278 | | */ |
|---|
| 279 | | public function onSquareDetect(i_sender:NyARSquareContourDetector,i_coordx:Vector.<int>,i_coordy:Vector.<int>,i_coor_num:int ,i_vertex_index:Vector.<int>):void |
|---|
| 280 | | { |
|---|
| 281 | | var mr:NyARMatchPattResult=this.__detectMarkerLite_mr; |
|---|
| 282 | | //輪郭座標から頂点リストに変換 |
|---|
| 283 | | var vertex:Vector.<NyARIntPoint2d>=this.__tmp_vertex; |
|---|
| 284 | | vertex[0].x=i_coordx[i_vertex_index[0]]; |
|---|
| 285 | | vertex[0].y=i_coordy[i_vertex_index[0]]; |
|---|
| 286 | | vertex[1].x=i_coordx[i_vertex_index[1]]; |
|---|
| 287 | | vertex[1].y=i_coordy[i_vertex_index[1]]; |
|---|
| 288 | | vertex[2].x=i_coordx[i_vertex_index[2]]; |
|---|
| 289 | | vertex[2].y=i_coordy[i_vertex_index[2]]; |
|---|
| 290 | | vertex[3].x=i_coordx[i_vertex_index[3]]; |
|---|
| 291 | | vertex[3].y=i_coordy[i_vertex_index[3]]; |
|---|
| 292 | | |
|---|
| 293 | | //画像を取得 |
|---|
| 294 | | if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ |
|---|
| 295 | | return; |
|---|
| 296 | | } |
|---|
| 297 | | //取得パターンをカラー差分データに変換して評価する。 |
|---|
| 298 | | this._deviation_data.setRaster(this._inst_patt); |
|---|
| 299 | | |
|---|
| 300 | | //最も一致するパターンを割り当てる。 |
|---|
| 301 | | var square_index:int,direction:int; |
|---|
| 302 | | var confidence:Number; |
|---|
| 303 | | this._match_patt[0].evaluate(this._deviation_data,mr); |
|---|
| 304 | | square_index=0; |
|---|
| 305 | | direction=mr.direction; |
|---|
| 306 | | confidence=mr.confidence; |
|---|
| 307 | | //2番目以降 |
|---|
| 308 | | var i:int; |
|---|
| 309 | | for(i=1;i<this._match_patt.length;i++){ |
|---|
| 310 | | this._match_patt[i].evaluate(this._deviation_data,mr); |
|---|
| 311 | | if (confidence > mr.confidence) { |
|---|
| 312 | | continue; |
|---|
| 313 | | } |
|---|
| 314 | | // もっと一致するマーカーがあったぽい |
|---|
| 315 | | square_index = i; |
|---|
| 316 | | direction = mr.direction; |
|---|
| 317 | | confidence = mr.confidence; |
|---|
| 318 | | } |
|---|
| 319 | | //最も一致したマーカ情報を、この矩形の情報として記録する。 |
|---|
| 320 | | var result:NyARDetectMarkerResult = this.result_stack.prePush(); |
|---|
| 321 | | result.arcode_id = square_index; |
|---|
| 322 | | result.confidence = confidence; |
|---|
| 323 | | |
|---|
| 324 | | var sq:NyARSquare=result.square; |
|---|
| 325 | | //directionを考慮して、squareを更新する。 |
|---|
| 326 | | for(i=0;i<4;i++){ |
|---|
| 327 | | var idx:int=(i+4 - direction) % 4; |
|---|
| 328 | | this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); |
|---|
| 329 | | } |
|---|
| 330 | | for (i = 0; i < 4; i++) { |
|---|
| 331 | | //直線同士の交点計算 |
|---|
| 332 | | if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ |
|---|
| 333 | | throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK |
|---|
| 334 | | } |
|---|
| 335 | | } |
|---|
| 336 | | } |
|---|
| 337 | | public function init(i_raster:INyARRgbRaster):void |
|---|
| 338 | | { |
|---|
| 339 | | this._ref_raster=i_raster; |
|---|
| 340 | | this.result_stack.clear(); |
|---|
| 341 | | |
|---|
| 342 | | } |
|---|
| 343 | | } |
|---|
| 344 | | |
|---|
| 345 | | |
|---|
| 346 | | class NyARDetectMarkerResult |
|---|
| 347 | | { |
|---|
| 348 | | public var arcode_id:int; |
|---|
| 349 | | public var confidence:Number; |
|---|
| 350 | | |
|---|
| 351 | | public var square:NyARSquare=new NyARSquare(); |
|---|
| 352 | | } |
|---|
| 353 | | |
|---|
| 354 | | |
|---|
| 355 | | class NyARDetectMarkerResultStack extends NyARObjectStack |
|---|
| 356 | | { |
|---|
| 357 | | public function NyARDetectMarkerResultStack(i_length:int) |
|---|
| 358 | | { |
|---|
| 359 | | super(i_length); |
|---|
| 360 | | } |
|---|
| 361 | | protected override function createArray(i_length:int):Vector.<*> |
|---|
| 362 | | { |
|---|
| 363 | | var ret:Vector.<NyARDetectMarkerResult>= new Vector.<NyARDetectMarkerResult>(i_length); |
|---|
| 364 | | for (var i:int =0; i < i_length; i++){ |
|---|
| 365 | | ret[i] = new NyARDetectMarkerResult(); |
|---|
| 366 | | } |
|---|
| 367 | | return Vector.<*>(ret); |
|---|
| 368 | | } |
|---|
| 369 | | } |
|---|