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 version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\r
16 * of the License, or (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 framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.core;
\r
37 import jp.nyatla.nyartoolkit.NyARException;
\r
43 * イメージからマーカー情報を検出するクラス。
\r
44 * このクラスは、arDetectMarker2.cとの置き換えになります。
\r
45 * ラベリング済みのラスタデータからマーカー位置を検出して、結果を保持します。
\r
48 public class NyARDetectMarker {
\r
49 private static final int AR_AREA_MAX=100000;//#define AR_AREA_MAX 100000
\r
50 private static final int AR_AREA_MIN=70;//#define AR_AREA_MIN 70
\r
52 // private final NyARMarker[] marker_holder; //マーカーデータの保持配列
\r
53 // private final NyARMarker[] marker_info2_array;//マーカーデータのインデックス配列
\r
54 // private int marker_num;
\r
55 private int width,height;
\r
57 * 最大i_squre_max個のマーカーを検出するクラスを作成する。
\r
61 public NyARDetectMarker(int i_width,int i_height)
\r
63 this.width =i_width;
\r
64 this.height=i_height;
\r
66 // this.marker_holder=new NyARMarker[i_squre_max];
\r
67 // this.marker_info2_array=new NyARMarker[i_squre_max];
\r
68 // //先にマーカーホルダにオブジェクトを作っておく
\r
69 // for(int i=0;i<i_squre_max;i++){
\r
70 // this.marker_holder[i]=new NyARMarker();
\r
73 private static final int AR_CHAIN_MAX=10000;
\r
74 private final int[] wk_arGetContour_xdir={0, 1, 1, 1, 0,-1,-1,-1};
\r
75 private final int[] wk_arGetContour_ydir={-1,-1, 0, 1, 1, 1, 0,-1};
\r
76 private final int[] wk_arGetContour_xcoord=new int[AR_CHAIN_MAX];
\r
77 private final int[] wk_arGetContour_ycoord=new int[AR_CHAIN_MAX];
\r
79 * int arGetContour( ARInt16 *limage, int *label_ref,int label, int clip[4], ARMarkerInfo2 *marker_info2 )
\r
81 * detectMarker関数から使う関数です。o_markerにlabelとclipで示される1個のマーカーを格納します。
\r
82 * marker_holder[i_holder_num]にオブジェクトが無ければまず新規に作成し、もし
\r
83 * 既に存在すればそこにマーカー情報を上書きして記録します。
\r
84 * Optimize:STEP[369->336]
\r
90 * @throws NyARException
\r
92 private final void arGetContour(NyARMarker o_marker,int[][] limage, int[] label_ref,int i_labelnum, NyARLabel i_label) throws NyARException
\r
94 final int[] xcoord=wk_arGetContour_xcoord;
\r
95 final int[] ycoord=wk_arGetContour_ycoord;
\r
96 final int[] xdir=wk_arGetContour_xdir; //static int xdir[8] = { 0, 1, 1, 1, 0,-1,-1,-1};
\r
97 final int[] ydir=wk_arGetContour_ydir;//static int ydir[8] = {-1,-1, 0, 1, 1, 1, 0,-1};
\r
98 //ShortPointer p1;//ARInt16 *p1;
\r
100 int sx=0, sy=0, dir;
\r
106 limage_j=limage[j];
\r
107 final int clip1=i_label.clip1;
\r
108 //p1=ShortPointer.wrap(limage,j*xsize+clip.get());//p1 = &(limage[j*xsize+clip[0]]);
\r
109 for( i = i_label.clip0; i <= clip1; i++){//for( i = clip[0]; i <= clip[1]; i++, p1++ ) {
\r
111 if(w > 0 && label_ref[w-1] == i_labelnum ) {//if( *p1 > 0 && label_ref[(*p1)-1] == label ) {
\r
117 if(i>clip1){//if( i > clip[1] ) {
\r
118 System.out.println("??? 1");//printf();
\r
119 throw new NyARException();//return(-1);
\r
122 // //マーカーホルダが既に確保済みかを調べる
\r
123 // if(marker_holder[i_holder_num]==null){
\r
125 // marker_holder[i_holder_num]=new NyARMarker();
\r
129 coord_num=1;//marker_info2->coord_num = 1;
\r
130 xcoord[0]=sx;//marker_info2->x_coord[0] = sx;
\r
131 ycoord[0]=sy;//marker_info2->y_coord[0] = sy;
\r
138 //本家はdmaxの作成とxcoordの作成を別のループでやってるけど、非効率なので統合
\r
140 //xcoord[1]-xcoord[n]までのデータを作る。
\r
142 // 1個前のxcoordとycoordはループ後半で格納される。
\r
143 // c=xcoord[coord_num-1];
\r
144 // r=ycoord[coord_num-1];
\r
145 //p1 = &(limage[marker_info2->y_coord[marker_info2->coord_num-1] * xsize+ marker_info2->x_coord[marker_info2->coord_num-1]]);
\r
148 if(limage[r+ydir[dir]][c+xdir[dir]]>0){//if( p1[ydir[dir]*xsize+xdir[dir]] > 0 ){
\r
154 System.out.println("??? 2");//printf("??? 2\n");
\r
155 throw new NyARException();//return(-1);
\r
157 // xcoordとycoordをc,rにも保存
\r
158 c= c + xdir[dir];//marker_info2->x_coord[marker_info2->coord_num]= marker_info2->x_coord[marker_info2->coord_num-1] + xdir[dir];
\r
159 r= r + ydir[dir];//marker_info2->y_coord[marker_info2->coord_num]= marker_info2->y_coord[marker_info2->coord_num-1] + ydir[dir];
\r
160 xcoord[coord_num]=c;//marker_info2->x_coord[marker_info2->coord_num]= marker_info2->x_coord[marker_info2->coord_num-1] + xdir[dir];
\r
161 ycoord[coord_num]=r;//marker_info2->y_coord[marker_info2->coord_num]= marker_info2->y_coord[marker_info2->coord_num-1] + ydir[dir];
\r
162 if(c == sx && r == sy ){
\r
166 d=(c-sx)*(c-sx)+(r-sy)*(r-sy);
\r
173 if(coord_num == AR_CHAIN_MAX-1){//if( marker_info2.coord_num == Config.AR_CHAIN_MAX-1 ){
\r
174 System.out.println("??? 3");//printf("??? 3\n");
\r
175 throw new NyARException();//return(-1);
\r
180 // for(i=1;i<coord_num;i++) {// for(i=1;i<marker_info2->coord_num;i++) {
\r
181 // d = (xcoord[i]-sx)*(xcoord[i]-sx)+ (ycoord[i]-sy)*(ycoord[i]-sy);// d = (marker_info2->x_coord[i]-sx)*(marker_info2->x_coord[i]-sx)+ (marker_info2->y_coord[i]-sy)*(marker_info2->y_coord[i]-sy);
\r
182 // if( d > dmax ) {
\r
187 //NyARMarkerへcoord情報をセット
\r
188 //coordの並び替えと保存はNyARMarkerへ移動
\r
189 o_marker.setCoordXY(v1,coord_num,xcoord,ycoord);
\r
194 * ARMarkerInfo2 *arDetectMarker2( ARInt16 *limage, int label_num, int *label_ref,int *warea, double *wpos, int *wclip,int area_max, int area_min, double factor, int *marker_num )
\r
196 * ラベリング情報からマーカー一覧を作成してo_marker_listを更新します。
\r
197 * 関数はo_marker_listに重なりを除外したマーカーリストを作成します。
\r
199 * @param i_labeling
\r
200 * ラベリング済みの情報を持つラベリングオブジェクト
\r
203 * @param o_marker_list
\r
205 * @throws NyARException
\r
207 public final void detectMarker(NyARLabeling i_labeling,double i_factor,NyARMarkerList o_marker_list) throws NyARException
\r
212 NyARLabel[] labels=i_labeling.getLabel();
\r
213 // int[] warea =i_labeling.getArea();
\r
214 int label_num =i_labeling.getLabelNum();
\r
215 // int[][] wclip =i_labeling.getClip();
\r
216 // double[] wpos =i_labeling.getPos();
\r
217 int[][] limage=i_labeling.getLabelImg();
\r
218 int[] label_ref =i_labeling.getLabelRef();
\r
221 o_marker_list.reset();
\r
225 // マーカーをmarker_holderに蓄積する。
\r
226 NyARMarker current_marker=o_marker_list.getCurrentHolder();
\r
227 NyARLabel label_pt;
\r
228 for(i=0; i<label_num; i++ ){
\r
229 label_pt=labels[i];
\r
230 label_area=label_pt.area;
\r
231 if(label_area < AR_AREA_MIN || label_area > AR_AREA_MAX ){
\r
234 if( label_pt.clip0 == 1 || label_pt.clip1 == xsize-2 ){//if( wclip[i*4+0] == 1 || wclip[i*4+1] == xsize-2 ){
\r
237 if( label_pt.clip2 == 1 || label_pt.clip3 == ysize-2 ){//if( wclip[i*4+2] == 1 || wclip[i*4+3] == ysize-2 ){
\r
240 //ret = arGetContour( limage, label_ref, i+1,&(wclip[i*4]), &(marker_info2[marker_num2]));
\r
241 arGetContour(current_marker,limage, label_ref, i+1,label_pt);
\r
243 if(!current_marker.checkSquare(label_area,i_factor,label_pt.pos_x,label_pt.pos_y)){
\r
244 //後半で整理するからここはいらない。// marker_holder[marker_num2]=null;
\r
247 // この3行はcheckSquareの最終段に含める。
\r
248 // marker_holder[marker_num2].area = warea[i];
\r
249 // marker_holder[marker_num2].pos[0] = wpos[i*2+0];
\r
250 // marker_holder[marker_num2].pos[1] = wpos[i*2+1];
\r
252 current_marker=o_marker_list.getNextHolder();
\r
253 //マーカーリストが上限に達したか確認
\r
254 if(current_marker==null){
\r
258 //マーカーリストを整理(重なり処理とかはマーカーリストに責務押し付け)
\r
259 o_marker_list.updateMarkerArray();
\r
261 // double[] pos_j,pos_i;
\r
262 // for(i=0; i < marker_num2; i++ ){
\r
263 // pos_i=marker_holder[i].pos;
\r
264 // for(j=i+1; j < marker_num2; j++ ) {
\r
265 // pos_j=marker_holder[j].pos;
\r
266 // d = (pos_i[0] - pos_j[0])*(pos_i[0] - pos_j[0])+
\r
267 // (pos_i[1] - pos_j[1])*(pos_i[1] - pos_j[1]);
\r
268 // if(marker_holder[i].area >marker_holder[j].area ) {
\r
269 // if( d <marker_holder[i].area / 4 ) {
\r
270 // marker_holder[j].area = 0;
\r
273 // if( d < marker_holder[j].area / 4 ) {
\r
274 // marker_holder[i].area = 0;
\r
280 // エリアが0のマーカーを外した配列を作って、その数もついでに計算
\r
281 // for(i=0;i<marker_num2;i++){
\r
282 // if(marker_holder[i].area==0.0){
\r
285 // marker_info2_array[marker_num]=marker_holder[i];
\r
288 // for( i=0; i < marker_num2; i++ ) {
\r
289 // if( marker_info2_array[i].area == 0.0 ) {
\r
290 // for( j=i+1; j < marker_num2; j++ ){
\r
291 // marker_info2_array[j-1] = marker_info2_array[j];
\r
297 // marker_num=marker_num2;//*marker_num = marker_num2;
\r
298 // return( &(marker_info2[0]) );
\r