2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\r
16 * of the License, or (at your option) any later version.
\r
18 * This program is distributed in the hope that it will be useful,
\r
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
21 * GNU General Public License for more details.
\r
23 * You should have received a copy of the GNU General Public License
\r
24 * along with this framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.sandbox.quadx2;
\r
33 import jp.nyatla.nyartoolkit.NyARException;
\r
34 import jp.nyatla.nyartoolkit.core.labeling.*;
\r
35 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingImage;
\r
36 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingLabel;
\r
37 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingLabelStack;
\r
38 import jp.nyatla.nyartoolkit.core.raster.*;
\r
39 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector;
\r
40 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
\r
41 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;
\r
42 import jp.nyatla.nyartoolkit.core.types.*;
\r
43 import jp.nyatla.nyartoolkit.core.param.*;
\r
46 import jp.nyatla.nyartoolkit.core2.types.NyARI64Linear;
\r
47 import jp.nyatla.nyartoolkit.core2.types.NyARI64Point2d;
\r
48 import jp.nyatla.nyartoolkit.core2.types.matrix.NyARI64Matrix22;
\r
49 import jp.nyatla.nyartoolkit.core.*;
\r
50 import jp.nyatla.nyartoolkit.sandbox.x2.*;
\r
54 * 1/4に解像度を落して解析するNyARSquareDetector_X2
\r
55 * 与えるBinRasterが既に1/4のサイズになっていないといけないことに注意
\r
57 public class NyARSquareDetector_Quad implements INyARSquareDetector
\r
59 private static int PCA_LENGTH = 20;
\r
60 private static double VERTEX_FACTOR = 1.0;// 線検出のファクタ
\r
62 private static int AR_AREA_MAX = 25000;// #define AR_AREA_MAX 100000
\r
64 private static int AR_AREA_MIN = 20;// #define AR_AREA_MIN 70
\r
66 private int _height;
\r
68 private NyARLabeling_ARToolKit_X2 _labeling;
\r
70 private NyARLabelingImage _limage;
\r
72 private OverlapChecker _overlap_checker = new OverlapChecker();
\r
73 private NyARFixedFloatObserv2IdealMap _dist_factor;
\r
75 * 最大i_squre_max個のマーカーを検出するクラスを作成する。
\r
79 public NyARSquareDetector_Quad(NyARCameraDistortionFactor i_dist_factor_ref, NyARIntSize i_size) throws NyARException
\r
81 this._width = i_size.w / 2;
\r
82 this._height = i_size.h / 2;
\r
83 this._labeling = new NyARLabeling_ARToolKit_X2();
\r
84 this._limage = new NyARLabelingImage(this._width, this._height);
\r
85 this._labeling.attachDestination(this._limage);
\r
87 // 輪郭の最大長は画面に映りうる最大の長方形サイズ。
\r
88 int number_of_coord = (this._width + this._height) * 2;
\r
90 // 輪郭バッファは頂点変換をするので、輪郭バッファの2倍取る。
\r
91 this._max_coord = number_of_coord;
\r
92 this._xcoord = new int[number_of_coord * 2];
\r
93 this._ycoord = new int[number_of_coord * 2];
\r
96 NyARCameraDistortionFactor quadfactor = new NyARCameraDistortionFactor();
\r
97 quadfactor.copyFrom(i_dist_factor_ref);
\r
98 quadfactor.changeScale(0.5);
\r
99 this._dist_factor = new NyARFixedFloatObserv2IdealMap(quadfactor, i_size);
\r
101 this._pca = new NyARFixedFloatPca2d();
\r
102 this._xpos = new int[PCA_LENGTH];//最大辺長はthis._width+this._height
\r
103 this._ypos = new int[PCA_LENGTH];//最大辺長はthis._width+this._height
\r
107 private int _max_coord;
\r
108 private int[] _xcoord;
\r
109 private int[] _ycoord;
\r
111 private void normalizeCoord(int[] i_coord_x, int[] i_coord_y, int i_index, int i_coord_num)
\r
113 // vertex1を境界にして、後方に配列を連結
\r
114 System.arraycopy(i_coord_x, 1, i_coord_x, i_coord_num, i_index);
\r
115 System.arraycopy(i_coord_y, 1, i_coord_y, i_coord_num, i_index);
\r
118 private int[] __detectMarker_mkvertex = new int[5];
\r
121 * arDetectMarker2を基にした関数
\r
122 * この関数はNyARSquare要素のうち、directionを除くパラメータを取得して返します。
\r
123 * directionの確定は行いません。
\r
125 * 解析する2値ラスタイメージを指定します。
\r
126 * @param o_square_stack
\r
127 * 抽出した正方形候補を格納するリスト
\r
128 * @throws NyARException
\r
130 public void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException
\r
132 NyARLabeling_ARToolKit_X2 labeling_proc = this._labeling;
\r
133 NyARLabelingImage limage = this._limage;
\r
138 o_square_stack.clear();
\r
141 labeling_proc.labeling(i_raster);
\r
144 int label_num = limage.getLabelStack().getLength();
\r
150 NyARLabelingLabelStack stack = limage.getLabelStack();
\r
151 NyARLabelingLabel[] labels = stack.getArray();
\r
155 stack.sortByArea();
\r
159 for (i = 0; i < label_num; i++)
\r
161 // 検査対象内のラベルサイズになるまで無視
\r
162 if (labels[i].area <= AR_AREA_MAX)
\r
168 int xsize = this._width;
\r
169 int ysize = this._height;
\r
170 int[] xcoord = this._xcoord;
\r
171 int[] ycoord = this._ycoord;
\r
172 int coord_max = this._max_coord;
\r
173 int[] mkvertex = this.__detectMarker_mkvertex;
\r
174 OverlapChecker overlap = this._overlap_checker;
\r
177 NyARLabelingLabel label_pt;
\r
180 overlap.reset(label_num);
\r
182 for (; i < label_num; i++)
\r
184 label_pt = labels[i];
\r
185 label_area = label_pt.area;
\r
186 // 検査対象サイズよりも小さくなったら終了
\r
187 if (label_area < AR_AREA_MIN)
\r
191 // クリップ領域が画面の枠に接していれば除外
\r
192 if (label_pt.clip_l == 1 || label_pt.clip_r == xsize - 2)
\r
193 {// if(wclip[i*4+0] == 1 || wclip[i*4+1] ==xsize-2){
\r
196 if (label_pt.clip_t == 1 || label_pt.clip_b == ysize - 2)
\r
197 {// if( wclip[i*4+2] == 1 || wclip[i*4+3] ==ysize-2){
\r
200 // 既に検出された矩形との重なりを確認
\r
201 if (!overlap.check(label_pt))
\r
208 coord_num = limage.getContour(i, coord_max, xcoord, ycoord);
\r
209 if (coord_num == coord_max)
\r
215 int vertex1 = scanVertex(xcoord, ycoord, coord_num);
\r
217 // 頂点候補(vertex1)を先頭に並べなおした配列を作成する。
\r
218 normalizeCoord(xcoord, ycoord, vertex1, coord_num);
\r
221 NyARSquare square_ptr = (NyARSquare)o_square_stack.prePush();
\r
224 if (!getSquareVertex(xcoord, ycoord, vertex1, coord_num, label_area, mkvertex))
\r
226 o_square_stack.pop();// 頂点の取得が出来なかったので破棄
\r
230 if (!getSquareLine(mkvertex, xcoord, ycoord, square_ptr))
\r
233 o_square_stack.pop();
\r
236 // 検出済の矩形の属したラベルを重なりチェックに追加する。
\r
237 overlap.push(label_pt);
\r
243 * 辺からの対角線が最長になる点を対角線候補として返す。
\r
247 * @param i_coord_num
\r
250 private int scanVertex(int[] i_xcoord, int[] i_ycoord, int i_coord_num)
\r
252 int sx = i_xcoord[0];
\r
253 int sy = i_ycoord[0];
\r
257 for (int i = 1; i < i_coord_num; i++)
\r
259 x = i_xcoord[i] - sx;
\r
260 y = i_ycoord[i] - sy;
\r
267 // ここでうまく終了条件入れられないかな。
\r
272 private NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter();
\r
274 private NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter();
\r
277 * static int arDetectMarker2_check_square( int area, ARMarkerInfo2 *marker_info2, double factor ) 関数の代替関数 OPTIMIZED STEP [450->415] o_squareに頂点情報をセットします。
\r
281 * @param i_vertex1_index
\r
282 * @param i_coord_num
\r
288 private boolean getSquareVertex(int[] i_x_coord, int[] i_y_coord, int i_vertex1_index, int i_coord_num, int i_area, int[] o_vertex)
\r
290 NyARVertexCounter wv1 = this.__getSquareVertex_wv1;
\r
291 NyARVertexCounter wv2 = this.__getSquareVertex_wv2;
\r
292 int end_of_coord = i_vertex1_index + i_coord_num - 1;
\r
293 int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0];
\r
294 int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0];
\r
296 int v1 = i_vertex1_index;
\r
297 for (int i = 1 + i_vertex1_index; i < end_of_coord; i++)
\r
298 {// for(i=1;i<marker_info2->coord_num-1;i++)
\r
300 int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy);
\r
307 double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR;
\r
309 o_vertex[0] = i_vertex1_index;
\r
311 if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v1, thresh))
\r
312 { // if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,0,v1,thresh,wv1,&wvnum1)<
\r
316 if (!wv2.getVertex(i_x_coord, i_y_coord, v1, end_of_coord, thresh))
\r
317 {// if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,v1,marker_info2->coord_num-1,thresh,wv2,&wvnum2)
\r
323 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1)
\r
324 {// if(wvnum1 == 1 && wvnum2== 1) {
\r
325 o_vertex[1] = wv1.vertex[0];
\r
327 o_vertex[3] = wv2.vertex[0];
\r
329 else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0)
\r
330 {// }else if( wvnum1 > 1 && wvnum2== 0) {
\r
331 //頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。
\r
332 v2 = (v1 - i_vertex1_index) / 2 + i_vertex1_index;
\r
333 if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh))
\r
337 if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh))
\r
341 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1)
\r
343 o_vertex[1] = wv1.vertex[0];
\r
344 o_vertex[2] = wv2.vertex[0];
\r
352 else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1)
\r
354 //v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index;
\r
355 v2 = (v1 + end_of_coord) / 2;
\r
357 if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh))
\r
361 if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh))
\r
365 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1)
\r
368 o_vertex[2] = wv1.vertex[0];
\r
369 o_vertex[3] = wv2.vertex[0];
\r
380 o_vertex[4] = end_of_coord;
\r
383 private int[] _xpos;
\r
384 private int[] _ypos;
\r
385 private NyARFixedFloatPca2d _pca;
\r
386 private NyARI64Matrix22 __getSquareLine_evec = new NyARI64Matrix22();
\r
387 private NyARI64Point2d __getSquareLine_mean = new NyARI64Point2d();
\r
388 private NyARI64Point2d __getSquareLine_ev = new NyARI64Point2d();
\r
389 private NyARI64Linear[] __getSquareLine_i64liner = NyARI64Linear.createArray(4);
\r
391 * arGetLine(int x_coord[], int y_coord[], int coord_num,int vertex[], double line[4][3], double v[4][2]) arGetLine2(int x_coord[], int y_coord[], int
\r
392 * coord_num,int vertex[], double line[4][3], double v[4][2], double *dist_factor) の2関数の合成品です。 マーカーのvertex,lineを計算して、結果をo_squareに保管します。
\r
393 * Optimize:STEP[424->391]
\r
397 * @throws NyARException
\r
399 private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord, NyARSquare o_square) throws NyARException
\r
401 NyARLinear[] l_line = o_square.line;
\r
402 NyARI64Matrix22 evec = this.__getSquareLine_evec;
\r
403 NyARI64Point2d mean = this.__getSquareLine_mean;
\r
404 NyARI64Point2d ev = this.__getSquareLine_ev;
\r
405 NyARI64Linear[] i64liner = this.__getSquareLine_i64liner;
\r
408 for (int i = 0; i < 4; i++)
\r
410 double w1 = (double)(i_mkvertex[i + 1] - i_mkvertex[i] + 1) * 0.05 + 0.5;
\r
411 int st = (int)(i_mkvertex[i] + w1);
\r
412 int ed = (int)(i_mkvertex[i + 1] - w1);
\r
413 int n = ed - st + 1;
\r
416 // nが2以下でmatrix.PCAを計算することはできないので、エラー
\r
420 n = this._dist_factor.observ2IdealSampling(i_xcoord, i_ycoord, st, n, this._xpos, this._ypos, PCA_LENGTH);
\r
423 this._pca.pcaF16(this._xpos, this._ypos, n, evec, ev, mean);
\r
424 NyARI64Linear l_line_i = i64liner[i];
\r
425 l_line_i.run = evec.m01;// line[i][0] = evec->m[1];
\r
426 l_line_i.rise = -evec.m00;// line[i][1] = -evec->m[0];
\r
427 l_line_i.intercept = -((l_line_i.run * mean.x + l_line_i.rise * mean.y) >> 16);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);
\r
430 NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex;
\r
431 NyARIntPoint2d[] l_imvertex = o_square.imvertex;
\r
432 for (int i = 0; i < 4; i++)
\r
434 NyARI64Linear l_line_i = i64liner[i];
\r
435 NyARI64Linear l_line_2 = i64liner[(i + 3) % 4];
\r
436 long w1 = (l_line_2.run * l_line_i.rise - l_line_i.run * l_line_2.rise) >> 16;
\r
441 l_sqvertex[i].x = (double)((l_line_2.rise * l_line_i.intercept - l_line_i.rise * l_line_2.intercept) / w1) *2/ 65536.0;
\r
442 l_sqvertex[i].y = (double)((l_line_i.run * l_line_2.intercept - l_line_2.run * l_line_i.intercept) / w1) *2/ 65536.0;
\r
443 // 頂点インデクスから頂点座標を得て保存
\r
444 l_imvertex[i].x = i_xcoord[i_mkvertex[i]]*2;
\r
445 l_imvertex[i].y = i_ycoord[i_mkvertex[i]]*2;
\r
446 l_line[i].run = (double)l_line_i.run / 65536.0;
\r
447 l_line[i].rise = (double)l_line_i.rise / 65536.0;
\r
448 l_line[i].intercept = (double)l_line_i.intercept*2 / 65536.0;
\r
456 * ラベル同士の重なり(内包関係)を調べるクラスです。
\r
457 * ラベルリストに内包するラベルを蓄積し、それにターゲットのラベルが内包されているか を確認します。
\r
459 class OverlapChecker
\r
461 private NyARLabelingLabel[] _labels = new NyARLabelingLabel[32];
\r
463 private int _length;
\r
466 * 最大i_max_label個のラベルを蓄積できるようにオブジェクトをリセットする
\r
468 * @param i_max_label
\r
470 public void reset(int i_max_label)
\r
472 if (i_max_label > this._labels.length)
\r
474 this._labels = new NyARLabelingLabel[i_max_label];
\r
482 * @param i_label_ref
\r
484 public void push(NyARLabelingLabel i_label_ref)
\r
486 this._labels[this._length] = i_label_ref;
\r
491 * 現在リストにあるラベルと重なっているかを返す。
\r
494 * @return 何れかのラベルの内側にあるならばfalse,独立したラベルである可能性が高ければtrueです.
\r
496 public boolean check(NyARLabelingLabel i_label)
\r
499 NyARLabelingLabel[] label_pt = this._labels;
\r
500 int px1 = (int)i_label.pos_x;
\r
501 int py1 = (int)i_label.pos_y;
\r
502 for (int i = this._length - 1; i >= 0; i--)
\r
504 int px2 = (int)label_pt[i].pos_x;
\r
505 int py2 = (int)label_pt[i].pos_y;
\r
506 int d = (px1 - px2) * (px1 - px2) + (py1 - py2) * (py1 - py2);
\r
507 if (d < label_pt[i].area / 4)
\r