| 1 |
/* |
|---|
| 2 |
* PROJECT: FLARToolKit |
|---|
| 3 |
* -------------------------------------------------------------------------------- |
|---|
| 4 |
* This work is based on the FLARToolKit developed by |
|---|
| 5 |
* R.Iizuka (nyatla) |
|---|
| 6 |
* http://nyatla.jp/nyatoolkit/ |
|---|
| 7 |
* |
|---|
| 8 |
* The FLARToolKit is ActionScript 3.0 version ARToolkit class library. |
|---|
| 9 |
* Copyright (C)2008 Saqoosha |
|---|
| 10 |
* |
|---|
| 11 |
* This program is free software: you can redistribute it and/or modify |
|---|
| 12 |
* it under the terms of the GNU General Public License as published by |
|---|
| 13 |
* the Free Software Foundation, either version 3 of the License, or |
|---|
| 14 |
* (at your option) any later version. |
|---|
| 15 |
* |
|---|
| 16 |
* This program is distributed in the hope that it will be useful, |
|---|
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 |
* GNU General Public License for more details. |
|---|
| 20 |
* |
|---|
| 21 |
* You should have received a copy of the GNU General Public License |
|---|
| 22 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 23 |
* |
|---|
| 24 |
* For further information please contact. |
|---|
| 25 |
* http://www.libspark.org/wiki/saqoosha/FLARToolKit |
|---|
| 26 |
* <saq(at)saqoosha.net> |
|---|
| 27 |
* |
|---|
| 28 |
*/ |
|---|
| 29 |
package org.libspark.flartoolkit.detector |
|---|
| 30 |
{ |
|---|
| 31 |
import flash.display.*; |
|---|
| 32 |
import org.libspark.flartoolkit.core.*; |
|---|
| 33 |
import org.libspark.flartoolkit.core.match.*; |
|---|
| 34 |
import org.libspark.flartoolkit.core.param.*; |
|---|
| 35 |
import org.libspark.flartoolkit.core.pickup.*; |
|---|
| 36 |
import org.libspark.flartoolkit.core.squaredetect.*; |
|---|
| 37 |
import org.libspark.flartoolkit.core.transmat.*; |
|---|
| 38 |
import org.libspark.flartoolkit.core.raster.*; |
|---|
| 39 |
import org.libspark.flartoolkit.core.raster.rgb.*; |
|---|
| 40 |
import org.libspark.flartoolkit.core.types.*; |
|---|
| 41 |
import org.libspark.flartoolkit.core.types.matrix.FLARDoubleMatrix44; |
|---|
| 42 |
import org.libspark.flartoolkit.core.raster.*; |
|---|
| 43 |
import org.libspark.flartoolkit.core.rasterfilter.rgb2gs.*; |
|---|
| 44 |
import org.libspark.flartoolkit.core.squaredetect.*; |
|---|
| 45 |
import org.libspark.flartoolkit.core.*; |
|---|
| 46 |
import org.libspark.flartoolkit.*; |
|---|
| 47 |
import org.libspark.flartoolkit.core.raster.rgb.*; |
|---|
| 48 |
public class FLARSingleMarkerDetector |
|---|
| 49 |
{ |
|---|
| 50 |
/** 一致率*/ |
|---|
| 51 |
private var _confidence:Number; |
|---|
| 52 |
private var _square:FLARSquare=new FLARSquare(); |
|---|
| 53 |
|
|---|
| 54 |
//所有インスタンス |
|---|
| 55 |
private var _inst_patt:IFLARColorPatt; |
|---|
| 56 |
private var _deviation_data:FLARMatchPattDeviationColorData; |
|---|
| 57 |
private var _match_patt:FLARMatchPatt_Color_WITHOUT_PCA; |
|---|
| 58 |
private var __detectMarkerLite_mr:FLARMatchPattResult=new FLARMatchPattResult(); |
|---|
| 59 |
private var _coordline:FLARCoord2Linear; |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
private var __ref_vertex:Vector.<FLARIntPoint2d>=new Vector.<FLARIntPoint2d>(4); |
|---|
| 63 |
|
|---|
| 64 |
/** |
|---|
| 65 |
* 矩形が見付かるたびに呼び出されます。 |
|---|
| 66 |
* 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 |
|---|
| 67 |
*/ |
|---|
| 68 |
public function updateSquareInfo(i_coord:FLARIntCoordinates,i_vertex_index:Vector.<int>):void |
|---|
| 69 |
{ |
|---|
| 70 |
var mr:FLARMatchPattResult=this.__detectMarkerLite_mr; |
|---|
| 71 |
//輪郭座標から頂点リストに変換 |
|---|
| 72 |
var vertex:Vector.<FLARIntPoint2d>=this.__ref_vertex; //C言語ならポインタ扱いで実装 |
|---|
| 73 |
vertex[0]=i_coord.items[i_vertex_index[0]]; |
|---|
| 74 |
vertex[1]=i_coord.items[i_vertex_index[1]]; |
|---|
| 75 |
vertex[2]=i_coord.items[i_vertex_index[2]]; |
|---|
| 76 |
vertex[3] = i_coord.items[i_vertex_index[3]]; |
|---|
| 77 |
var i:int; |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
//画像を取得 |
|---|
| 81 |
if (!this._inst_patt.pickFromRaster(this._last_input_raster,vertex)){ |
|---|
| 82 |
return; |
|---|
| 83 |
} |
|---|
| 84 |
//取得パターンをカラー差分データに変換して評価する。 |
|---|
| 85 |
this._deviation_data.setRaster(this._inst_patt); |
|---|
| 86 |
if(!this._match_patt.evaluate(this._deviation_data,mr)){ |
|---|
| 87 |
return; |
|---|
| 88 |
} |
|---|
| 89 |
//現在の一致率より低ければ終了 |
|---|
| 90 |
if (this._confidence > mr.confidence){ |
|---|
| 91 |
return; |
|---|
| 92 |
} |
|---|
| 93 |
//一致率の高い矩形があれば、方位を考慮して頂点情報を作成 |
|---|
| 94 |
var sq:FLARSquare=this._square; |
|---|
| 95 |
this._confidence = mr.confidence; |
|---|
| 96 |
//directionを考慮して、squareを更新する。 |
|---|
| 97 |
for(i=0;i<4;i++){ |
|---|
| 98 |
var idx:int=(i+4 - mr.direction) % 4; |
|---|
| 99 |
this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coord,sq.line[i]); |
|---|
| 100 |
} |
|---|
| 101 |
//ちょっと、ひっくり返してみようか。 |
|---|
| 102 |
for (i = 0; i < 4; i++) { |
|---|
| 103 |
//直線同士の交点計算 |
|---|
| 104 |
if(!sq.line[i].crossPos(sq.line[(i + 3) % 4],sq.sqvertex[i])){ |
|---|
| 105 |
throw new FLARException();//ここのエラー復帰するならダブルバッファにすればOK |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
private var _is_continue:Boolean = false; |
|---|
| 112 |
private var _square_detect:FLARDetector; |
|---|
| 113 |
protected var _transmat:IFLARTransMat; |
|---|
| 114 |
//画処理用 |
|---|
| 115 |
private var _bin_raster:FLARBinRaster; |
|---|
| 116 |
protected var _tobin_filter:IFLARRgb2GsFilterArtkTh; |
|---|
| 117 |
|
|---|
| 118 |
private var _offset:FLARRectOffset; |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
public function FLARSingleMarkerDetector(i_ref_param:FLARParam,i_ref_code:FLARCode,i_marker_width:Number) |
|---|
| 122 |
{ |
|---|
| 123 |
var patt_inst:IFLARColorPatt; |
|---|
| 124 |
var sqdetect_inst:FLARDetector; |
|---|
| 125 |
var transmat_inst:IFLARTransMat; |
|---|
| 126 |
|
|---|
| 127 |
sqdetect_inst=new FLARDetector(this,i_ref_param.getScreenSize()); |
|---|
| 128 |
transmat_inst=new FLARTransMat(i_ref_param); |
|---|
| 129 |
initInstance(sqdetect_inst, transmat_inst, i_ref_param, i_ref_code, i_marker_width); |
|---|
| 130 |
return; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
protected function initInstance( |
|---|
| 134 |
i_sqdetect_inst:FLARDetector, |
|---|
| 135 |
i_transmat_inst:IFLARTransMat, |
|---|
| 136 |
i_ref_param:FLARParam, |
|---|
| 137 |
i_ref_code:FLARCode, |
|---|
| 138 |
i_marker_width:Number):void |
|---|
| 139 |
{ |
|---|
| 140 |
var scr_size:FLARIntSize=i_ref_param.getScreenSize(); |
|---|
| 141 |
// 解析オブジェクトを作る |
|---|
| 142 |
this._inst_patt=new FLARColorPatt_Perspective(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25); |
|---|
| 143 |
this._square_detect = i_sqdetect_inst; |
|---|
| 144 |
this._transmat = i_transmat_inst; |
|---|
| 145 |
//2値画像バッファを作る |
|---|
| 146 |
this._bin_raster=new FLARBinRaster(scr_size.w,scr_size.h); |
|---|
| 147 |
//パターンの一致検索処理用 |
|---|
| 148 |
this._deviation_data=new FLARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight()); |
|---|
| 149 |
this._coordline=new FLARCoord2Linear(i_ref_param.getScreenSize(),i_ref_param.getDistortionFactor()); |
|---|
| 150 |
this._match_patt=new FLARMatchPatt_Color_WITHOUT_PCA(i_ref_code); |
|---|
| 151 |
//オフセットを作成 |
|---|
| 152 |
this._offset=new FLARRectOffset(); |
|---|
| 153 |
this._offset.setSquare(i_marker_width); |
|---|
| 154 |
return; |
|---|
| 155 |
|
|---|
| 156 |
} |
|---|
| 157 |
/** 参照インスタンス*/ |
|---|
| 158 |
private var _last_input_raster:IFLARRgbRaster=null; |
|---|
| 159 |
private var _bin_filter:IFLARRgb2GsFilterArtkTh=null; |
|---|
| 160 |
/** |
|---|
| 161 |
* i_imageにマーカー検出処理を実行し、結果を記録します。 |
|---|
| 162 |
* |
|---|
| 163 |
* @param i_raster |
|---|
| 164 |
* マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ |
|---|
| 165 |
* と一致していなければなりません。 |
|---|
| 166 |
* @return マーカーが検出できたかを真偽値で返します。 |
|---|
| 167 |
* @throws FLARException |
|---|
| 168 |
*/ |
|---|
| 169 |
public function detectMarkerLite(i_raster:IFLARRgbRaster,i_threshold:int):Boolean |
|---|
| 170 |
{ |
|---|
| 171 |
//サイズチェック |
|---|
| 172 |
if(!this._bin_raster.getSize().isEqualSize_2(i_raster.getSize())){ |
|---|
| 173 |
throw new FLARException(); |
|---|
| 174 |
} |
|---|
| 175 |
//最終入力ラスタを更新 |
|---|
| 176 |
if(this._last_input_raster!=i_raster){ |
|---|
| 177 |
this._bin_filter=IFLARRgb2GsFilterArtkTh(i_raster.createInterface(IFLARRgb2GsFilterArtkTh)); |
|---|
| 178 |
this._last_input_raster=i_raster; |
|---|
| 179 |
} |
|---|
| 180 |
//ラスタを2値イメージに変換する. |
|---|
| 181 |
this._bin_filter.doFilter(i_threshold,this._bin_raster); |
|---|
| 182 |
|
|---|
| 183 |
//コールバックハンドラの準備 |
|---|
| 184 |
this._confidence=0; |
|---|
| 185 |
this._last_input_raster=i_raster; |
|---|
| 186 |
// |
|---|
| 187 |
//マーカ検出器をコール |
|---|
| 188 |
this._square_detect.detectMarker(this._bin_raster,this._square_detect); |
|---|
| 189 |
if(this._confidence==0){ |
|---|
| 190 |
return false; |
|---|
| 191 |
} |
|---|
| 192 |
return true; |
|---|
| 193 |
} |
|---|
| 194 |
/** |
|---|
| 195 |
* 検出したマーカーの変換行列を計算して、o_resultへ値を返します。 |
|---|
| 196 |
* 直前に実行したdetectMarkerLiteが成功していないと使えません。 |
|---|
| 197 |
* |
|---|
| 198 |
* @param o_result |
|---|
| 199 |
* 変換行列を受け取るオブジェクトを指定します。 |
|---|
| 200 |
* @throws FLARException |
|---|
| 201 |
*/ |
|---|
| 202 |
public function getTransformMatrix(o_result:FLARTransMatResult):void |
|---|
| 203 |
{ |
|---|
| 204 |
// 一番一致したマーカーの位置とかその辺を計算 |
|---|
| 205 |
if (this._is_continue) { |
|---|
| 206 |
this._transmat.transMatContinue(this._square,this._offset,o_result, o_result); |
|---|
| 207 |
} else { |
|---|
| 208 |
this._transmat.transMat(this._square,this._offset, o_result); |
|---|
| 209 |
} |
|---|
| 210 |
return; |
|---|
| 211 |
} |
|---|
| 212 |
/** |
|---|
| 213 |
* 現在の矩形を返します。 |
|---|
| 214 |
* @return |
|---|
| 215 |
*/ |
|---|
| 216 |
public function refSquare():FLARSquare |
|---|
| 217 |
{ |
|---|
| 218 |
return this._square; |
|---|
| 219 |
} |
|---|
| 220 |
/** |
|---|
| 221 |
* 検出したマーカーの一致度を返します。 |
|---|
| 222 |
* |
|---|
| 223 |
* @return マーカーの一致度を返します。0~1までの値をとります。 一致度が低い場合には、誤認識の可能性が高くなります。 |
|---|
| 224 |
* @throws FLARException |
|---|
| 225 |
*/ |
|---|
| 226 |
public function getConfidence():Number |
|---|
| 227 |
{ |
|---|
| 228 |
return this._confidence; |
|---|
| 229 |
} |
|---|
| 230 |
/** |
|---|
| 231 |
* getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。 |
|---|
| 232 |
* |
|---|
| 233 |
* @param i_is_continue |
|---|
| 234 |
* TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。 |
|---|
| 235 |
*/ |
|---|
| 236 |
public function setContinueMode(i_is_continue:Boolean):void |
|---|
| 237 |
{ |
|---|
| 238 |
this._is_continue = i_is_continue; |
|---|
| 239 |
} |
|---|
| 240 |
} |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
import org.libspark.flartoolkit.detector.*; |
|---|
| 268 |
import org.libspark.flartoolkit.core.squaredetect.*; |
|---|
| 269 |
import org.libspark.flartoolkit.core.types.*; |
|---|
| 270 |
import org.libspark.flartoolkit.core.squaredetect.*; |
|---|
| 271 |
import org.libspark.flartoolkit.detector.*; |
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
/** |
|---|
| 282 |
* Rleラ矩形Detectorのブリッジ |
|---|
| 283 |
* |
|---|
| 284 |
*/ |
|---|
| 285 |
class FLARDetector extends FLARSquareContourDetector_FlaFill implements FLARSquareContourDetector_CbHandler |
|---|
| 286 |
{ |
|---|
| 287 |
private var _parent:FLARSingleMarkerDetector; |
|---|
| 288 |
public function FLARDetector(i_parent:FLARSingleMarkerDetector,i_size:FLARIntSize):void |
|---|
| 289 |
{ |
|---|
| 290 |
super(i_size); |
|---|
| 291 |
this._parent=i_parent; |
|---|
| 292 |
} |
|---|
| 293 |
public function detectMarkerCallback(i_coord:FLARIntCoordinates, i_vertex_index:Vector.<int>):void |
|---|
| 294 |
{ |
|---|
| 295 |
|
|---|
| 296 |
this._parent.updateSquareInfo(i_coord, i_vertex_index); |
|---|
| 297 |
} |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|