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 edition ARToolKit class library.
\r
11 * Copyright (C)2008-2009 Ryo Iizuka
\r
13 * This program is free software: you can redistribute it and/or modify
\r
14 * it under the terms of the GNU General Public License as published by
\r
15 * the Free Software Foundation, either version 3 of the License, or
\r
16 * (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 program. If not, see <http://www.gnu.org/licenses/>.
\r
26 * For further information please contact.
\r
27 * http://nyatla.jp/nyatoolkit/
\r
28 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
\r
31 package jp.nyatla.nyartoolkit.core.squaredetect;
\r
33 import jp.nyatla.nyartoolkit.NyARException;
\r
34 import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor;
\r
35 import jp.nyatla.nyartoolkit.core.param.NyARObserv2IdealMap;
\r
36 import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d;
\r
37 import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2;
\r
38 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;
\r
39 import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d;
\r
40 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;
\r
41 import jp.nyatla.nyartoolkit.core.types.NyARLinear;
\r
42 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;
\r
44 public class SquareContourDetector
\r
46 private static final double VERTEX_FACTOR = 1.0;// 線検出のファクタ
\r
47 private final double[] _xpos;
\r
48 private final double[] _ypos;
\r
49 private final int[] __detectMarker_mkvertex = new int[5];
\r
50 private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter();
\r
51 private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter();
\r
52 private final INyARPca2d _pca;
\r
53 private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22();
\r
54 private final double[] __getSquareLine_mean=new double[2];
\r
55 private final double[] __getSquareLine_ev=new double[2];
\r
56 private final NyARObserv2IdealMap _dist_factor;
\r
57 public SquareContourDetector(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor_ref)
\r
59 //歪み計算テーブルを作ると、8*width/height*2の領域を消費します。
\r
60 //領域を取りたくない場合は、i_dist_factor_refの値をそのまま使ってください。
\r
61 this._dist_factor = new NyARObserv2IdealMap(i_distfactor_ref,i_size);
\r
64 // 輪郭バッファは頂点変換をするので、輪郭バッファの2倍取る。
\r
65 this._pca=new NyARPca2d_MatrixPCA_O2();
\r
66 this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height
\r
67 this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height
\r
71 public boolean coordToSquare(int[] i_xcoord,int[] i_ycoord,int i_st_index,int i_coord_num,int i_label_area,NyARSquare o_square) throws NyARException
\r
74 final int[] mkvertex = this.__detectMarker_mkvertex;
\r
77 if (!getSquareVertex(i_xcoord, i_ycoord, i_st_index, i_coord_num, i_label_area, mkvertex)) {
\r
82 if (!getSquareLine(mkvertex, i_xcoord, i_ycoord, o_square)){
\r
89 private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord, NyARSquare o_square) throws NyARException
\r
91 final NyARLinear[] l_line = o_square.line;
\r
92 final NyARDoubleMatrix22 evec=this.__getSquareLine_evec;
\r
93 final double[] mean=this.__getSquareLine_mean;
\r
94 final double[] ev=this.__getSquareLine_ev;
\r
97 for (int i = 0; i < 4; i++) {
\r
98 final double w1 = (double) (i_mkvertex[i + 1] - i_mkvertex[i] + 1) * 0.05 + 0.5;
\r
99 final int st = (int) (i_mkvertex[i] + w1);
\r
100 final int ed = (int) (i_mkvertex[i + 1] - w1);
\r
101 final int n = ed - st + 1;
\r
103 // nが2以下でmatrix.PCAを計算することはできないので、エラー
\r
107 this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st, n,this._xpos,this._ypos);
\r
110 this._pca.pca(this._xpos,this._ypos,n,evec, ev,mean);
\r
111 final NyARLinear l_line_i = l_line[i];
\r
112 l_line_i.dy = evec.m01;// line[i][0] = evec->m[1];
\r
113 l_line_i.dx = -evec.m00;// line[i][1] = -evec->m[0];
\r
114 l_line_i.c = -(l_line_i.dy * mean[0] + l_line_i.dx * mean[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);
\r
117 final NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex;
\r
118 final NyARIntPoint2d[] l_imvertex = o_square.imvertex;
\r
119 for (int i = 0; i < 4; i++) {
\r
120 final NyARLinear l_line_i = l_line[i];
\r
121 final NyARLinear l_line_2 = l_line[(i + 3) % 4];
\r
122 final double w1 = l_line_2.dy * l_line_i.dx - l_line_i.dy * l_line_2.dx;
\r
126 l_sqvertex[i].x = (l_line_2.dx * l_line_i.c - l_line_i.dx * l_line_2.c) / w1;
\r
127 l_sqvertex[i].y = (l_line_i.dy * l_line_2.c - l_line_2.dy * l_line_i.c) / w1;
\r
128 // 頂点インデクスから頂点座標を得て保存
\r
129 l_imvertex[i].x = i_xcoord[i_mkvertex[i]];
\r
130 l_imvertex[i].y = i_ycoord[i_mkvertex[i]];
\r
134 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
136 final NyARVertexCounter wv1 = this.__getSquareVertex_wv1;
\r
137 final NyARVertexCounter wv2 = this.__getSquareVertex_wv2;
\r
138 final int end_of_coord = i_vertex1_index + i_coord_num - 1;
\r
139 final int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0];
\r
140 final int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0];
\r
142 int v1 = i_vertex1_index;
\r
143 for (int i = 1 + i_vertex1_index; i < end_of_coord; i++) {// for(i=1;i<marker_info2->coord_num-1;i++)
\r
145 final int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy);
\r
151 final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR;
\r
153 o_vertex[0] = i_vertex1_index;
\r
155 if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v1, thresh)) { // if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,0,v1,thresh,wv1,&wvnum1)<
\r
159 if (!wv2.getVertex(i_x_coord, i_y_coord, v1, end_of_coord, thresh)) {// if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,v1,marker_info2->coord_num-1,thresh,wv2,&wvnum2)
\r
165 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {// if(wvnum1 == 1 && wvnum2== 1) {
\r
166 o_vertex[1] = wv1.vertex[0];
\r
168 o_vertex[3] = wv2.vertex[0];
\r
169 } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {// }else if( wvnum1 > 1 && wvnum2== 0) {
\r
170 //頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。
\r
171 v2 = (v1-i_vertex1_index)/2+i_vertex1_index;
\r
172 if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh)) {
\r
175 if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh)) {
\r
178 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {
\r
179 o_vertex[1] = wv1.vertex[0];
\r
180 o_vertex[2] = wv2.vertex[0];
\r
185 } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) {
\r
186 //v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index;
\r
187 v2 = (v1+ end_of_coord)/2;
\r
189 if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh)) {
\r
192 if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh)) {
\r
195 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {
\r
197 o_vertex[2] = wv1.vertex[0];
\r
198 o_vertex[3] = wv2.vertex[0];
\r
205 o_vertex[4] = end_of_coord;
\r
210 * 輪郭線の矩形検出開始ポイントを特定して、座標を並べ替えます。
\r
211 * 輪郭線の先頭から、対角線が最長になる点を1点検索し、それより前の区間をバッファの後方に接続します。
\r
212 * 戻り値は対角線が最長になった点です。関数終了後、返却値+i_coord_numの要素が有効になります。
\r
215 * @param i_coord_num
\r
218 public static int normalizeCoord(int[] i_coord_x, int[] i_coord_y,int i_coord_num)
\r
221 final int sx = i_coord_x[0];
\r
222 final int sy = i_coord_y[0];
\r
226 for (int i = 1; i < i_coord_num; i++) {
\r
227 x = i_coord_x[i] - sx;
\r
228 y = i_coord_y[i] - sy;
\r
234 // ここでうまく終了条件入れられないかな。
\r
236 // vertex1を境界にして、後方に配列を連結
\r
237 System.arraycopy(i_coord_x, 1, i_coord_x, i_coord_num, ret);
\r
238 System.arraycopy(i_coord_y, 1, i_coord_y, i_coord_num, ret);
\r