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.NyARIntCoordinates;
\r
39 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;
\r
40 import jp.nyatla.nyartoolkit.core.types.NyARLinear;
\r
41 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;
\r
46 * このクラスは、座標配列を直線式に変換します。
\r
47 * 座標配列の連続する要素を主成分分析にかけて、直線式にします。
\r
49 public class NyARCoord2Linear
\r
51 private final double[] _xpos;
\r
52 private final double[] _ypos;
\r
53 private final INyARPca2d _pca;
\r
54 private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22();
\r
55 private final double[] __getSquareLine_mean=new double[2];
\r
56 private final double[] __getSquareLine_ev=new double[2];
\r
57 private final NyARObserv2IdealMap _dist_factor;
\r
60 * 輪郭取得元画像の歪み矯正オブジェクトとサイズを指定して、インスタンスを生成します。
\r
63 * @param i_distfactor_ref
\r
64 * 樽型歪みを補正する場合に、オブジェクトを指定します。
\r
67 public NyARCoord2Linear(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor)
\r
69 if(i_distfactor!=null){
\r
70 this._dist_factor = new NyARObserv2IdealMap(i_distfactor,i_size);
\r
72 this._dist_factor=null;
\r
75 this._pca=new NyARPca2d_MatrixPCA_O2();
\r
76 this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height
\r
77 this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height
\r
83 * この関数は、輪郭点集合からay+bx+c=0の直線式を計算します。
\r
85 * 直線計算の対象とする、輪郭点の開始インデックス
\r
87 * 直線計算の対象とする、輪郭点の終了インデックス
\r
93 * 直線式の計算に成功すると、trueを返します。
\r
94 * @throws NyARException
\r
96 public boolean coord2Line(int i_st,int i_ed,NyARIntCoordinates i_coord, NyARLinear o_line) throws NyARException
\r
101 int cood_num=i_coord.length;
\r
105 //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき
\r
106 w1 = (double) (i_ed - i_st + 1) * 0.05 + 0.5;
\r
108 st = (int) (i_st+w1);
\r
109 ed = (int) (i_ed - w1);
\r
111 //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき
\r
112 w1 = (double)((i_ed+cood_num-i_st+1)%cood_num) * 0.05 + 0.5;
\r
114 st = ((int) (i_st+w1))%cood_num;
\r
115 ed = ((int) (i_ed+cood_num-w1))%cood_num;
\r
121 if(this._dist_factor!=null){
\r
122 this._dist_factor.observ2IdealBatch(i_coord.items, st, n,this._xpos,this._ypos,0);
\r
126 n=ed+1+cood_num-st;
\r
127 if(this._dist_factor!=null){
\r
128 this._dist_factor.observ2IdealBatch(i_coord.items, st,cood_num-st,this._xpos,this._ypos,0);
\r
129 this._dist_factor.observ2IdealBatch(i_coord.items, 0,ed+1,this._xpos,this._ypos,cood_num-st);
\r
134 // nが2以下でmatrix.PCAを計算することはできないので、エラー
\r
138 final NyARDoubleMatrix22 evec=this.__getSquareLine_evec;
\r
139 final double[] mean=this.__getSquareLine_mean;
\r
142 this._pca.pca(this._xpos,this._ypos,n,evec, this.__getSquareLine_ev,mean);
\r
143 o_line.a = evec.m01;// line[i][0] = evec->m[1];
\r
144 o_line.b = -evec.m00;// line[i][1] = -evec->m[0];
\r
145 o_line.c = -(o_line.a * mean[0] + o_line.b * mean[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);
\r