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.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
45 public class NyARSquareDetector_ARToolKit implements INyARSquareDetector
\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
52 private final NyARLabeling_ARToolKit _labeling;
\r
54 private final NyARLabelingImage _limage;
\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
60 private final int _max_coord;
\r
61 private final int[] _xcoord;
\r
62 private final int[] _ycoord;
\r
64 * 最大i_squre_max個のマーカーを検出するクラスを作成する。
\r
68 public NyARSquareDetector_ARToolKit(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException
\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
76 // 輪郭の最大長は画面に映りうる最大の長方形サイズ。
\r
77 int number_of_coord = (this._width + this._height) * 2;
\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
87 * arDetectMarker2を基にした関数
\r
88 * この関数はNyARSquare要素のうち、directionを除くパラメータを取得して返します。
\r
89 * directionの確定は行いません。
\r
91 * 解析する2値ラスタイメージを指定します。
\r
92 * @param o_square_stack
\r
94 * @throws NyARException
\r
96 public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException
\r
98 final NyARLabelingImage limage = this._limage;
\r
103 o_square_stack.clear();
\r
105 // ラベル数が0ならここまで(Labeling内部でソートするようにした。)
\r
106 final int label_num = this._labeling.labeling(i_raster,this._limage);
\r
107 if (label_num < 1) {
\r
111 final NyARLabelingLabelStack stack = limage.getLabelStack();
\r
113 stack.sortByArea();
\r
115 final NyARLabelingLabel[] labels = stack.getArray();
\r
119 for (i = 0; i < label_num; i++) {
\r
120 // 検査対象内のラベルサイズになるまで無視
\r
121 if (labels[i].area <= AR_AREA_MAX) {
\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
134 overlap.setMaxLabels(label_num);
\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
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
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
150 // 既に検出された矩形との重なりを確認
\r
151 if (!overlap.check(label_pt)) {
\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
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
167 // 検出済の矩形の属したラベルを重なりチェックに追加する。
\r
168 overlap.push(label_pt);
\r