-/* \r
- * PROJECT: NyARToolkit\r
- * --------------------------------------------------------------------------------\r
- * This work is based on the original ARToolKit developed by\r
- * Hirokazu Kato\r
- * Mark Billinghurst\r
- * HITLab, University of Washington, Seattle\r
- * http://www.hitl.washington.edu/artoolkit/\r
- *\r
- * The NyARToolkit is Java edition ARToolKit class library.\r
- * Copyright (C)2008-2009 Ryo Iizuka\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- * \r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- * \r
- * For further information please contact.\r
- * http://nyatla.jp/nyatoolkit/\r
- * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
- * \r
- */\r
-package jp.nyatla.nyartoolkit.core.squaredetect;\r
-\r
-import jp.nyatla.nyartoolkit.NyARException;\r
-import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor;\r
-import jp.nyatla.nyartoolkit.core.param.NyARObserv2IdealMap;\r
-import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d;\r
-import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2;\r
-import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;\r
-import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d;\r
-import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
-import jp.nyatla.nyartoolkit.core.types.NyARLinear;\r
-import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;\r
-\r
-\r
-\r
-\r
-public class SquareContourDetector\r
-{\r
- private final double[] _xpos;\r
- private final double[] _ypos; \r
- private final int[] __detectMarker_mkvertex = new int[4];\r
- private final INyARPca2d _pca;\r
- private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22();\r
- private final double[] __getSquareLine_mean=new double[2];\r
- private final double[] __getSquareLine_ev=new double[2];\r
- private final Coord2SquareVertexIndexes _coord2vertex=new Coord2SquareVertexIndexes();\r
- private final NyARObserv2IdealMap _dist_factor;\r
- public SquareContourDetector(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor_ref)\r
- {\r
- //歪み計算テーブルを作ると、8*width/height*2の領域を消費します。\r
- //領域を取りたくない場合は、i_dist_factor_refの値をそのまま使ってください。\r
- this._dist_factor = new NyARObserv2IdealMap(i_distfactor_ref,i_size);\r
-\r
-\r
- // 輪郭バッファ\r
- this._pca=new NyARPca2d_MatrixPCA_O2();\r
- this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height\r
- this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height\r
- return;\r
- }\r
-\r
- public boolean coordToSquare(int[] i_xcoord,int[] i_ycoord,int i_coord_num,int i_label_area,NyARSquare o_square) throws NyARException\r
- {\r
-\r
- final int[] mkvertex = this.__detectMarker_mkvertex;\r
- // 頂点情報を取得\r
- if (!this._coord2vertex.getVertexIndexes(i_xcoord, i_ycoord, i_coord_num, i_label_area, mkvertex)) {\r
- // 頂点の取得が出来なかったので破棄\r
- return false;\r
- }\r
- // マーカーを検出\r
- if (!getSquareLine(mkvertex, i_xcoord, i_ycoord,i_coord_num-1, o_square)){\r
- // 矩形が成立しなかった。\r
- return false;\r
- }\r
- return true;\r
- }\r
- \r
- private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord,int i_cood_num, NyARSquare o_square) throws NyARException\r
- {\r
- final NyARLinear[] l_line = o_square.line;\r
- final NyARDoubleMatrix22 evec=this.__getSquareLine_evec;\r
- final double[] mean=this.__getSquareLine_mean;\r
- final double[] ev=this.__getSquareLine_ev;\r
- \r
- double w1;\r
- for (int i = 0; i < 4; i++){\r
- //頂点を取得\r
- int ver1=i_mkvertex[i];\r
- int ver2=i_mkvertex[(i+1)%4];\r
- int n,st,ed;\r
- //探索区間の決定\r
- if(ver2>=i_mkvertex[i]){\r
- //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき\r
- w1 = (double) (ver2 - ver1 + 1) * 0.05 + 0.5;\r
- //探索区間の決定\r
- st = (int) (ver1+w1);\r
- ed = (int) (ver2 - w1);\r
- }else{\r
- //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき\r
- w1 = (double) (ver2+i_cood_num-ver1+1)%i_cood_num * 0.05 + 0.5;\r
- //探索区間の決定\r
- st = (int) (ver1+w1)%i_cood_num;\r
- ed = (int) (ver2+i_cood_num-w1)%i_cood_num;\r
- }\r
- //探索区間数を確認\r
- if(st<=ed){\r
- //探索区間は1区間\r
- n = ed - st + 1;\r
- this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st, n,this._xpos,this._ypos,0);\r
- }else{\r
- //探索区間は2区間\r
- n=ed+1+i_cood_num-st;\r
- this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st,i_cood_num-st,this._xpos,this._ypos,0);\r
- this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, 0,ed+1,this._xpos,this._ypos,i_cood_num-st);\r
- }\r
- //要素数の確認\r
- if (n < 2) {\r
- // nが2以下でmatrix.PCAを計算することはできないので、エラー\r
- return false;\r
- }\r
- //主成分分析する。\r
- this._pca.pca(this._xpos,this._ypos,n,evec, ev,mean);\r
- final NyARLinear l_line_i = l_line[i];\r
- l_line_i.dy = evec.m01;// line[i][0] = evec->m[1];\r
- l_line_i.dx = -evec.m00;// line[i][1] = -evec->m[0];\r
- 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
- }\r
-\r
- final NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex;\r
- final NyARIntPoint2d[] l_imvertex = o_square.imvertex;\r
- for (int i = 0; i < 4; i++) {\r
- //直線同士の交点計算\r
- if(!NyARLinear.crossPos(l_line[i],l_line[(i + 3) % 4],l_sqvertex[i])){\r
- return false;\r
- }\r
- // 頂点インデクスから頂点座標を得て保存\r
- l_imvertex[i].x = i_xcoord[i_mkvertex[i]];\r
- l_imvertex[i].y = i_ycoord[i_mkvertex[i]];\r
- }\r
- return true;\r
- } \r
-}
\ No newline at end of file