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 NyARSquareContourDetector_ARToolKit implements INyARSquareContourDetector
\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 ContourPickup _cpickup=new ContourPickup();
\r
58 private final Coord2SquareVertexIndexes _coord2vertex=new Coord2SquareVertexIndexes();
\r
60 private final int _max_coord;
\r
61 private final int[] _xcoord;
\r
62 private final int[] _ycoord;
\r
63 private final int[] __detectMarker_mkvertex = new int[4];
\r
65 * 最大i_squre_max個のマーカーを検出するクラスを作成する。
\r
69 public NyARSquareContourDetector_ARToolKit(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException
\r
71 this._width = i_size.w;
\r
72 this._height = i_size.h;
\r
73 this._labeling = new NyARLabeling_ARToolKit();
\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 detectMarkerCB(NyARBinRaster i_raster, DetectMarkerCallback i_callback) throws NyARException
\r
98 final NyARLabelingImage limage = this._limage;
\r
101 final int label_num = this._labeling.labeling(i_raster,this._limage);
\r
102 if (label_num < 1) {
\r
106 final NyARLabelingLabelStack stack = limage.getLabelStack();
\r
108 stack.sortByArea();
\r
110 final NyARLabelingLabel[] labels = stack.getArray();
\r
114 for (i = 0; i < label_num; i++) {
\r
115 // 検査対象内のラベルサイズになるまで無視
\r
116 if (labels[i].area <= AR_AREA_MAX) {
\r
120 final int xsize = this._width;
\r
121 final int ysize = this._height;
\r
122 final int[] xcoord = this._xcoord;
\r
123 final int[] ycoord = this._ycoord;
\r
124 final int coord_max = this._max_coord;
\r
125 final int[] mkvertex =this.__detectMarker_mkvertex;
\r
127 final LabelOverlapChecker<NyARLabelingLabel> overlap = this._overlap_checker;
\r
130 overlap.setMaxLabels(label_num);
\r
131 for (; i < label_num; i++) {
\r
132 final NyARLabelingLabel label_pt = labels[i];
\r
133 final int label_area = label_pt.area;
\r
134 // 検査対象サイズよりも小さくなったら終了
\r
135 if (label_area < AR_AREA_MIN) {
\r
138 // クリップ領域が画面の枠に接していれば除外
\r
139 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
142 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
145 // 既に検出された矩形との重なりを確認
\r
146 if (!overlap.check(label_pt)) {
\r
151 final int coord_num = _cpickup.getContour(limage,limage.getTopClipTangentX(label_pt),label_pt.clip_t, coord_max, xcoord, ycoord);
\r
152 if (coord_num == coord_max) {
\r
156 //輪郭線をチェックして、矩形かどうかを判定。矩形ならばmkvertexに取得
\r
157 if (!this._coord2vertex.getVertexIndexes(xcoord, ycoord,coord_num,label_area, mkvertex)) {
\r
161 //矩形を発見したことをコールバック関数で通知
\r
162 i_callback.onSquareDetect(this,xcoord,ycoord,coord_num,mkvertex);
\r
164 // 検出済の矩形の属したラベルを重なりチェックに追加する。
\r
165 overlap.push(label_pt);
\r