OSDN Git Service

[backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / squaredetect / NyARSquareDetector_ARToolKit.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\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
17  * \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
22  *\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
25  * \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
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.squaredetect;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.labeling.LabelOverlapChecker;\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.labeling.artoolkit.NyARLabeling_ARToolKit;\r
39 import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor;\r
40 import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster;\r
41 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
42 \r
43 \r
44 \r
45 public class NyARSquareDetector_ARToolKit implements INyARSquareDetector\r
46 {\r
47         private static final int AR_AREA_MAX = 100000;// #define AR_AREA_MAX 100000\r
48         private static final int AR_AREA_MIN = 70;// #define AR_AREA_MIN 70\r
49         private final int _width;\r
50         private final int _height;\r
51 \r
52         private final NyARLabeling_ARToolKit _labeling;\r
53 \r
54         private final NyARLabelingImage _limage;\r
55 \r
56         private final LabelOverlapChecker<NyARLabelingLabel> _overlap_checker = new LabelOverlapChecker<NyARLabelingLabel>(32,NyARLabelingLabel.class);\r
57         private final SquareContourDetector _sqconvertor;\r
58         private final ContourPickup _cpickup=new ContourPickup();\r
59         \r
60         private final int _max_coord;\r
61         private final int[] _xcoord;\r
62         private final int[] _ycoord;    \r
63         /**\r
64          * 最大i_squre_max個のマーカーを検出するクラスを作成する。\r
65          * \r
66          * @param i_param\r
67          */\r
68         public NyARSquareDetector_ARToolKit(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException\r
69         {\r
70                 this._width = i_size.w;\r
71                 this._height = i_size.h;\r
72                 this._labeling = new NyARLabeling_ARToolKit();\r
73                 this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref);\r
74                 this._limage = new NyARLabelingImage(this._width, this._height);\r
75 \r
76                 // 輪郭の最大長は画面に映りうる最大の長方形サイズ。\r
77                 int number_of_coord = (this._width + this._height) * 2;\r
78 \r
79                 // 輪郭バッファは頂点変換をするので、輪郭バッファの2倍取る。\r
80                 this._max_coord = number_of_coord;\r
81                 this._xcoord = new int[number_of_coord];\r
82                 this._ycoord = new int[number_of_coord];\r
83                 return;\r
84         }\r
85 \r
86         /**\r
87          * arDetectMarker2を基にした関数\r
88          * この関数はNyARSquare要素のうち、directionを除くパラメータを取得して返します。\r
89          * directionの確定は行いません。\r
90          * @param i_raster\r
91          * 解析する2値ラスタイメージを指定します。\r
92          * @param o_square_stack\r
93          * 抽出した正方形候補を格納するリスト\r
94          * @throws NyARException\r
95          */\r
96         public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException\r
97         {\r
98                 final NyARLabelingImage limage = this._limage;\r
99 \r
100                 // 初期化\r
101 \r
102                 // マーカーホルダをリセット\r
103                 o_square_stack.clear();\r
104 \r
105                 // ラベル数が0ならここまで(Labeling内部でソートするようにした。)\r
106                 final int label_num = this._labeling.labeling(i_raster,this._limage);\r
107                 if (label_num < 1) {\r
108                         return;\r
109                 }\r
110 \r
111                 final NyARLabelingLabelStack stack = limage.getLabelStack();\r
112                 //ラベルをソートしておく\r
113                 stack.sortByArea();\r
114                 //\r
115                 final NyARLabelingLabel[] labels = stack.getArray();\r
116 \r
117                 // デカいラベルを読み飛ばし\r
118                 int i;\r
119                 for (i = 0; i < label_num; i++) {\r
120                         // 検査対象内のラベルサイズになるまで無視\r
121                         if (labels[i].area <= AR_AREA_MAX) {\r
122                                 break;\r
123                         }\r
124                 }\r
125 \r
126                 final int xsize = this._width;\r
127                 final int ysize = this._height;\r
128                 final int[] xcoord = this._xcoord;\r
129                 final int[] ycoord = this._ycoord;\r
130                 final int coord_max = this._max_coord;\r
131                 final LabelOverlapChecker<NyARLabelingLabel> overlap = this._overlap_checker;\r
132 \r
133                 //重なりチェッカの最大数を設定\r
134                 overlap.setMaxLabels(label_num);\r
135 \r
136                 for (; i < label_num; i++) {\r
137                         final NyARLabelingLabel label_pt = labels[i];\r
138                         final int label_area = label_pt.area;\r
139                         // 検査対象サイズよりも小さくなったら終了\r
140                         if (label_area < AR_AREA_MIN) {\r
141                                 break;\r
142                         }\r
143                         // クリップ領域が画面の枠に接していれば除外\r
144                         if (label_pt.clip_l == 1 || label_pt.clip_r == xsize - 2) {// if(wclip[i*4+0] == 1 || wclip[i*4+1] ==xsize-2){\r
145                                 continue;\r
146                         }\r
147                         if (label_pt.clip_t == 1 || label_pt.clip_b == ysize - 2) {// if( wclip[i*4+2] == 1 || wclip[i*4+3] ==ysize-2){\r
148                                 continue;\r
149                         }\r
150                         // 既に検出された矩形との重なりを確認\r
151                         if (!overlap.check(label_pt)) {\r
152                                 // 重なっているようだ。\r
153                                 continue;\r
154                         }\r
155                         // 輪郭を取得\r
156                         final int coord_num = _cpickup.getContour(limage,limage.getTopClipTangentX(label_pt),label_pt.clip_t, coord_max, xcoord, ycoord);\r
157                         if (coord_num == coord_max) {\r
158                                 // 輪郭が大きすぎる。\r
159                                 continue;\r
160                         }\r
161                         //ここから先が輪郭分析\r
162                         NyARSquare square_ptr = o_square_stack.prePush();\r
163                         if(!this._sqconvertor.coordToSquare(xcoord,ycoord,coord_num,label_area,square_ptr)){\r
164                                 o_square_stack.pop();// 頂点の取得が出来なかったので破棄\r
165                                 continue;                               \r
166                         }\r
167                         // 検出済の矩形の属したラベルを重なりチェックに追加する。\r
168                         overlap.push(label_pt);\r
169                 }\r
170                 return;\r
171         }\r
172 \r
173 }\r
174 \r
175 \r
176 \r