OSDN Git Service

[Backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / sample / sandbox / jp / nyatla / nyartoolkit / sandbox / quadx2 / NyARSquareDetector_Quad.java
index 5bc4185..bce9c29 100644 (file)
 package jp.nyatla.nyartoolkit.sandbox.quadx2;\r
 import jp.nyatla.nyartoolkit.NyARException;\r
 import jp.nyatla.nyartoolkit.core.labeling.*;\r
-import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingImage;\r
-import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingLabel;\r
-import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingLabelStack;\r
+import jp.nyatla.nyartoolkit.core.labeling.artoolkit.*;\r
 import jp.nyatla.nyartoolkit.core.raster.*;\r
+import jp.nyatla.nyartoolkit.core.squaredetect.ContourPickup;\r
 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector;\r
 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;\r
+import jp.nyatla.nyartoolkit.core.squaredetect.SquareContourDetector;\r
 import jp.nyatla.nyartoolkit.core.types.*;\r
 import jp.nyatla.nyartoolkit.core.param.*;\r
 \r
-\r
-import jp.nyatla.nyartoolkit.core2.types.NyARI64Linear;\r
-import jp.nyatla.nyartoolkit.core2.types.NyARI64Point2d;\r
-import jp.nyatla.nyartoolkit.core2.types.matrix.NyARI64Matrix22;\r
-import jp.nyatla.nyartoolkit.core.*;\r
 import jp.nyatla.nyartoolkit.sandbox.x2.*;\r
 \r
 \r
@@ -56,21 +51,19 @@ import jp.nyatla.nyartoolkit.sandbox.x2.*;
  */\r
 public class NyARSquareDetector_Quad implements INyARSquareDetector\r
 {\r
-    private static int PCA_LENGTH = 20;\r
-    private static double VERTEX_FACTOR = 1.0;// 線検出のファクタ\r
-\r
     private static int AR_AREA_MAX = 25000;// #define AR_AREA_MAX 100000\r
 \r
     private static int AR_AREA_MIN = 20;// #define AR_AREA_MIN 70\r
     private int _width;\r
     private int _height;\r
 \r
-    private NyARLabeling_ARToolKit_X2 _labeling;\r
+    private NyARLabeling_ARToolKit _labeling;\r
 \r
     private NyARLabelingImage _limage;\r
 \r
-    private OverlapChecker _overlap_checker = new OverlapChecker();\r
-    private NyARFixedFloatObserv2IdealMap _dist_factor;\r
+       private final LabelOverlapChecker<NyARLabelingLabel> _overlap_checker = new LabelOverlapChecker<NyARLabelingLabel>(32,NyARLabelingLabel.class);\r
+       private final SquareContourDetector _sqconvertor;\r
+       private final ContourPickup _cpickup=new ContourPickup();\r
     /**\r
      * 最大i_squre_max個のマーカーを検出するクラスを作成する。\r
      * \r
@@ -80,9 +73,9 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector
     {\r
         this._width = i_size.w / 2;\r
         this._height = i_size.h / 2;\r
-        this._labeling = new NyARLabeling_ARToolKit_X2();\r
+        this._labeling = new NyARLabeling_ARToolKit();\r
         this._limage = new NyARLabelingImage(this._width, this._height);\r
-        this._labeling.attachDestination(this._limage);\r
+        this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref);        \r
 \r
         // 輪郭の最大長は画面に映りうる最大の長方形サイズ。\r
         int number_of_coord = (this._width + this._height) * 2;\r
@@ -96,27 +89,12 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector
         NyARCameraDistortionFactor quadfactor = new NyARCameraDistortionFactor();\r
         quadfactor.copyFrom(i_dist_factor_ref);\r
         quadfactor.changeScale(0.5);\r
-        this._dist_factor = new NyARFixedFloatObserv2IdealMap(quadfactor, i_size);\r
-        //PCA\r
-        this._pca = new NyARFixedFloatPca2d();\r
-        this._xpos = new int[PCA_LENGTH];//最大辺長はthis._width+this._height\r
-        this._ypos = new int[PCA_LENGTH];//最大辺長はthis._width+this._height\r
-\r
     }\r
 \r
     private int _max_coord;\r
     private int[] _xcoord;\r
     private int[] _ycoord;\r
 \r
-    private void normalizeCoord(int[] i_coord_x, int[] i_coord_y, int i_index, int i_coord_num)\r
-    {\r
-        // vertex1を境界にして、後方に配列を連結\r
-        System.arraycopy(i_coord_x, 1, i_coord_x, i_coord_num, i_index);\r
-        System.arraycopy(i_coord_y, 1, i_coord_y, i_coord_num, i_index);\r
-    }\r
-\r
-    private int[] __detectMarker_mkvertex = new int[5];\r
-\r
     /**\r
      * arDetectMarker2を基にした関数\r
      * この関数はNyARSquare要素のうち、directionを除くパラメータを取得して返します。\r
@@ -129,7 +107,6 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector
      */\r
     public void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException\r
     {\r
-       NyARLabeling_ARToolKit_X2 labeling_proc = this._labeling;\r
         NyARLabelingImage limage = this._limage;\r
 \r
         // 初期化\r
@@ -138,7 +115,7 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector
         o_square_stack.clear();\r
 \r
         // ラベリング\r
-        labeling_proc.labeling(i_raster);\r
+        this._labeling.labeling(i_raster,limage);\r
 \r
         // ラベル数が0ならここまで\r
         int label_num = limage.getLabelStack().getLength();\r
@@ -170,14 +147,13 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector
         int[] xcoord = this._xcoord;\r
         int[] ycoord = this._ycoord;\r
         int coord_max = this._max_coord;\r
-        int[] mkvertex = this.__detectMarker_mkvertex;\r
-        OverlapChecker overlap = this._overlap_checker;\r
-        int coord_num;\r
+               final LabelOverlapChecker<NyARLabelingLabel> overlap = this._overlap_checker;\r
+\r
         int label_area;\r
         NyARLabelingLabel label_pt;\r
 \r
         //重なりチェッカの最大数を設定\r
-        overlap.reset(label_num);\r
+        overlap.setMaxLabels(label_num);\r
 \r
         for (; i < label_num; i++)\r
         {\r
@@ -203,316 +179,32 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector
                 // 重なっているようだ。\r
                 continue;\r
             }\r
-\r
-            // 輪郭を取得\r
-            coord_num = limage.getContour(i, coord_max, xcoord, ycoord);\r
-            if (coord_num == coord_max)\r
-            {\r
-                // 輪郭が大きすぎる。\r
-                continue;\r
-            }\r
-            //頂点候補のインデクスを取得\r
-            int vertex1 = scanVertex(xcoord, ycoord, coord_num);\r
-\r
-            // 頂点候補(vertex1)を先頭に並べなおした配列を作成する。\r
-            normalizeCoord(xcoord, ycoord, vertex1, coord_num);\r
-\r
-            // 領域を準備する。\r
-            NyARSquare square_ptr = (NyARSquare)o_square_stack.prePush();\r
-\r
-            // 頂点情報を取得\r
-            if (!getSquareVertex(xcoord, ycoord, vertex1, coord_num, label_area, mkvertex))\r
-            {\r
-                o_square_stack.pop();// 頂点の取得が出来なかったので破棄\r
-                continue;\r
-            }\r
-            // マーカーを検出\r
-            if (!getSquareLine(mkvertex, xcoord, ycoord, square_ptr))\r
-            {\r
-                // 矩形が成立しなかった。\r
-                o_square_stack.pop();\r
-                continue;\r
-            }\r
+                       // 既に検出された矩形との重なりを確認\r
+                       if (!overlap.check(label_pt)) {\r
+                               // 重なっているようだ。\r
+                               continue;\r
+                       }\r
+                       // 輪郭を取得\r
+                       final int coord_num = _cpickup.getContour(limage,limage.getTopClipTangentX(label_pt),label_pt.clip_t, coord_max, xcoord, ycoord);\r
+                       if (coord_num == coord_max) {\r
+                               // 輪郭が大きすぎる。\r
+                               continue;\r
+                       }\r
+                       //輪郭分析用に正規化する。\r
+                       final int vertex1 = SquareContourDetector.normalizeCoord(xcoord, ycoord, coord_num);\r
+\r
+                       //ここから先が輪郭分析\r
+                       NyARSquare square_ptr = o_square_stack.prePush();\r
+                       if(!this._sqconvertor.coordToSquare(xcoord,ycoord,vertex1,coord_num,label_area,square_ptr)){\r
+                               o_square_stack.pop();// 頂点の取得が出来なかったので破棄\r
+                               continue;                               \r
+                       }\r
+                                   \r
             // 検出済の矩形の属したラベルを重なりチェックに追加する。\r
             overlap.push(label_pt);\r
         }\r
         return;\r
     }\r
-\r
-    /**\r
-     * 辺からの対角線が最長になる点を対角線候補として返す。\r
-     * \r
-     * @param i_xcoord\r
-     * @param i_ycoord\r
-     * @param i_coord_num\r
-     * @return\r
-     */\r
-    private int scanVertex(int[] i_xcoord, int[] i_ycoord, int i_coord_num)\r
-    {\r
-        int sx = i_xcoord[0];\r
-        int sy = i_ycoord[0];\r
-        int d = 0;\r
-        int w, x, y;\r
-        int ret = 0;\r
-        for (int i = 1; i < i_coord_num; i++)\r
-        {\r
-            x = i_xcoord[i] - sx;\r
-            y = i_ycoord[i] - sy;\r
-            w = x * x + y * y;\r
-            if (w > d)\r
-            {\r
-                d = w;\r
-                ret = i;\r
-            }\r
-            // ここでうまく終了条件入れられないかな。\r
-        }\r
-        return ret;\r
-    }\r
-\r
-    private NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter();\r
-\r
-    private NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter();\r
-\r
-    /**\r
-     * static int arDetectMarker2_check_square( int area, ARMarkerInfo2 *marker_info2, double factor ) 関数の代替関数 OPTIMIZED STEP [450->415] o_squareに頂点情報をセットします。\r
-     * \r
-     * @param i_x_coord\r
-     * @param i_y_coord\r
-     * @param i_vertex1_index\r
-     * @param i_coord_num\r
-     * @param i_area\r
-     * @param o_vertex\r
-     * 要素数はint[4]である事\r
-     * @return\r
-     */\r
-    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
-    {\r
-        NyARVertexCounter wv1 = this.__getSquareVertex_wv1;\r
-        NyARVertexCounter wv2 = this.__getSquareVertex_wv2;\r
-        int end_of_coord = i_vertex1_index + i_coord_num - 1;\r
-        int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0];\r
-        int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0];\r
-        int dmax = 0;\r
-        int v1 = i_vertex1_index;\r
-        for (int i = 1 + i_vertex1_index; i < end_of_coord; i++)\r
-        {// for(i=1;i<marker_info2->coord_num-1;i++)\r
-            // {\r
-            int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy);\r
-            if (d > dmax)\r
-            {\r
-                dmax = d;\r
-                v1 = i;\r
-            }\r
-        }\r
-        double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR;\r
-\r
-        o_vertex[0] = i_vertex1_index;\r
-\r
-        if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v1, thresh))\r
-        { // if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,0,v1,thresh,wv1,&wvnum1)<\r
-            // 0 ) {\r
-            return false;\r
-        }\r
-        if (!wv2.getVertex(i_x_coord, i_y_coord, v1, end_of_coord, thresh))\r
-        {// if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,v1,marker_info2->coord_num-1,thresh,wv2,&wvnum2)\r
-            // < 0) {\r
-            return false;\r
-        }\r
-\r
-        int v2;\r
-        if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1)\r
-        {// if(wvnum1 == 1 && wvnum2== 1) {\r
-            o_vertex[1] = wv1.vertex[0];\r
-            o_vertex[2] = v1;\r
-            o_vertex[3] = wv2.vertex[0];\r
-        }\r
-        else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0)\r
-        {// }else if( wvnum1 > 1 && wvnum2== 0) {\r
-            //頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。\r
-            v2 = (v1 - i_vertex1_index) / 2 + i_vertex1_index;\r
-            if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh))\r
-            {\r
-                return false;\r
-            }\r
-            if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh))\r
-            {\r
-                return false;\r
-            }\r
-            if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1)\r
-            {\r
-                o_vertex[1] = wv1.vertex[0];\r
-                o_vertex[2] = wv2.vertex[0];\r
-                o_vertex[3] = v1;\r
-            }\r
-            else\r
-            {\r
-                return false;\r
-            }\r
-        }\r
-        else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1)\r
-        {\r
-            //v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index;\r
-            v2 = (v1 + end_of_coord) / 2;\r
-\r
-            if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh))\r
-            {\r
-                return false;\r
-            }\r
-            if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh))\r
-            {\r
-                return false;\r
-            }\r
-            if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1)\r
-            {\r
-                o_vertex[1] = v1;\r
-                o_vertex[2] = wv1.vertex[0];\r
-                o_vertex[3] = wv2.vertex[0];\r
-            }\r
-            else\r
-            {\r
-                return false;\r
-            }\r
-        }\r
-        else\r
-        {\r
-            return false;\r
-        }\r
-        o_vertex[4] = end_of_coord;\r
-        return true;\r
-    }\r
-    private int[] _xpos;\r
-    private int[] _ypos;\r
-    private NyARFixedFloatPca2d _pca;\r
-    private NyARI64Matrix22 __getSquareLine_evec = new NyARI64Matrix22();\r
-    private NyARI64Point2d __getSquareLine_mean = new NyARI64Point2d();\r
-    private NyARI64Point2d __getSquareLine_ev = new NyARI64Point2d();\r
-    private NyARI64Linear[] __getSquareLine_i64liner = NyARI64Linear.createArray(4);\r
-    /**\r
-     * 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
-     * coord_num,int vertex[], double line[4][3], double v[4][2], double *dist_factor) の2関数の合成品です。 マーカーのvertex,lineを計算して、結果をo_squareに保管します。\r
-     * Optimize:STEP[424->391]\r
-     * \r
-     * @param i_cparam\r
-     * @return\r
-     * @throws NyARException\r
-     */\r
-    private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord, NyARSquare o_square) throws NyARException\r
-    {\r
-        NyARLinear[] l_line = o_square.line;\r
-        NyARI64Matrix22 evec = this.__getSquareLine_evec;\r
-        NyARI64Point2d mean = this.__getSquareLine_mean;\r
-        NyARI64Point2d ev = this.__getSquareLine_ev;\r
-        NyARI64Linear[] i64liner = this.__getSquareLine_i64liner;\r
-\r
-\r
-        for (int i = 0; i < 4; i++)\r
-        {\r
-            double w1 = (double)(i_mkvertex[i + 1] - i_mkvertex[i] + 1) * 0.05 + 0.5;\r
-            int st = (int)(i_mkvertex[i] + w1);\r
-            int ed = (int)(i_mkvertex[i + 1] - w1);\r
-            int n = ed - st + 1;\r
-            if (n < 2)\r
-            {\r
-                // nが2以下でmatrix.PCAを計算することはできないので、エラー\r
-                return false;\r
-            }\r
-            //配列作成\r
-            n = this._dist_factor.observ2IdealSampling(i_xcoord, i_ycoord, st, n, this._xpos, this._ypos, PCA_LENGTH);\r
-\r
-            //主成分分析する。\r
-            this._pca.pcaF16(this._xpos, this._ypos, n, evec, ev, mean);\r
-            NyARI64Linear l_line_i = i64liner[i];\r
-            l_line_i.run = evec.m01;// line[i][0] = evec->m[1];\r
-            l_line_i.rise = -evec.m00;// line[i][1] = -evec->m[0];\r
-            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
-        }\r
-\r
-        NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex;\r
-        NyARIntPoint2d[] l_imvertex = o_square.imvertex;\r
-        for (int i = 0; i < 4; i++)\r
-        {\r
-            NyARI64Linear l_line_i = i64liner[i];\r
-            NyARI64Linear l_line_2 = i64liner[(i + 3) % 4];\r
-            long w1 = (l_line_2.run * l_line_i.rise - l_line_i.run * l_line_2.rise) >> 16;\r
-            if (w1 == 0)\r
-            {\r
-                return false;\r
-            }\r
-            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
-            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
-            // 頂点インデクスから頂点座標を得て保存\r
-            l_imvertex[i].x = i_xcoord[i_mkvertex[i]]*2;\r
-            l_imvertex[i].y = i_ycoord[i_mkvertex[i]]*2;\r
-            l_line[i].run = (double)l_line_i.run / 65536.0;\r
-            l_line[i].rise = (double)l_line_i.rise / 65536.0;\r
-            l_line[i].intercept = (double)l_line_i.intercept*2 / 65536.0;\r
-        }\r
-        return true;\r
-    }\r
-}\r
-\r
-\r
-/**\r
- * ラベル同士の重なり(内包関係)を調べるクラスです。 \r
- * ラベルリストに内包するラベルを蓄積し、それにターゲットのラベルが内包されているか を確認します。\r
- */\r
-class OverlapChecker\r
-{\r
-    private NyARLabelingLabel[] _labels = new NyARLabelingLabel[32];\r
-\r
-    private int _length;\r
-\r
-    /**\r
-     * 最大i_max_label個のラベルを蓄積できるようにオブジェクトをリセットする\r
-     * \r
-     * @param i_max_label\r
-     */\r
-    public void reset(int i_max_label)\r
-    {\r
-        if (i_max_label > this._labels.length)\r
-        {\r
-            this._labels = new NyARLabelingLabel[i_max_label];\r
-        }\r
-        this._length = 0;\r
-    }\r
-\r
-    /**\r
-     * チェック対象のラベルを追加する。\r
-     * \r
-     * @param i_label_ref\r
-     */\r
-    public void push(NyARLabelingLabel i_label_ref)\r
-    {\r
-        this._labels[this._length] = i_label_ref;\r
-        this._length++;\r
-    }\r
-\r
-    /**\r
-     * 現在リストにあるラベルと重なっているかを返す。\r
-     * \r
-     * @param i_label\r
-     * @return 何れかのラベルの内側にあるならばfalse,独立したラベルである可能性が高ければtrueです.\r
-     */\r
-    public boolean check(NyARLabelingLabel i_label)\r
-    {\r
-        // 重なり処理かな?\r
-        NyARLabelingLabel[] label_pt = this._labels;\r
-        int px1 = (int)i_label.pos_x;\r
-        int py1 = (int)i_label.pos_y;\r
-        for (int i = this._length - 1; i >= 0; i--)\r
-        {\r
-            int px2 = (int)label_pt[i].pos_x;\r
-            int py2 = (int)label_pt[i].pos_y;\r
-            int d = (px1 - px2) * (px1 - px2) + (py1 - py2) * (py1 - py2);\r
-            if (d < label_pt[i].area / 4)\r
-            {\r
-                // 対象外\r
-                return false;\r
-            }\r
-        }\r
-        // 対象\r
-        return true;\r
-    }\r
 }\r
 \r
 \r