OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.1 / test / jp / nyatla / nyartoolkit / dev / LabelingCamera.java
1 /* このソースは実験用のソースです。
2  * 動いたり動かなかったりします。
3  * 
4  */
5 package jp.nyatla.nyartoolkit.dev;
6
7 import javax.media.*;
8
9 import javax.media.util.BufferToImage;
10 import javax.media.format.*;
11
12 import jp.nyatla.nyartoolkit.NyARException;
13 import jp.nyatla.nyartoolkit.jmf.utils.*;
14
15 import jp.nyatla.nyartoolkit.core.*;
16
17 import java.awt.*;
18
19 import jp.nyatla.nyartoolkit.core.labeling.*;
20 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingImage;
21 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingLabel;
22 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabelingLabelStack;
23 import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabeling_ARToolKit;
24 import jp.nyatla.nyartoolkit.core.match.*;
25 import jp.nyatla.nyartoolkit.core.param.*;
26 import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d;
27 import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2;
28 import jp.nyatla.nyartoolkit.core.pickup.*;
29 import jp.nyatla.nyartoolkit.core.raster.*;
30 import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster;
31 import jp.nyatla.nyartoolkit.core.rasterfilter.*;
32 import jp.nyatla.nyartoolkit.core2.rasteranalyzer.*;
33 import jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold.*;
34 import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*;
35 import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.NyARRasterFilter_RgbAve;
36 import jp.nyatla.utils.NyObjectStack;
37 import jp.nyatla.utils.j2se.LabelingBufferdImage;
38 import jp.nyatla.nyartoolkit.core.rasterfilter.bin.INyARRasterFilter_RgbToGs;
39 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;
40 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector;
41 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
42 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack;
43 import jp.nyatla.nyartoolkit.core.squaredetect.NyARVertexCounter;
44 import jp.nyatla.nyartoolkit.core.transmat.*;
45 import jp.nyatla.nyartoolkit.core.types.*;
46 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;
47
48
49 ///**
50 // * QRコードのシンボルを結びつける偉いクラス
51 // *
52 // */
53 //class NyQrCodeSymbolBinder
54 //{
55 //      LabelingBufferdImage bimg;
56 //      
57 //      NyARIntPoint[][] _sqare;
58 //      /**
59 //       * 最小の三角形を構成する頂点セットを得る
60 //       * @param i_s0
61 //       * @param i_s1
62 //       * @param i_s2
63 //       * @param o_vertex
64 //       */
65 //      public static void getMinimumTriangleVertex(NyARSquare[] i_sqare,int[] o_vertex_id)
66 //      {
67 //              //辺の長さが最小になる頂点の組合せを探す
68 //              int d;
69 //              int x,y;
70 //              int dmax=0x7fffffff;
71 //              final NyARIntPoint[] vertex0=i_sqare[0].imvertex;
72 //              final NyARIntPoint[] vertex1=i_sqare[1].imvertex;
73 //              final NyARIntPoint[] vertex2=i_sqare[2].imvertex;
74 //              for(int i=0;i<4;i++)
75 //              {
76 //                      for(int i2=0;i2<4;i2++)
77 //                      {
78 //                              for(int i3=0;i3<4;i3++){
79 //                                      x=vertex0[i].x-vertex2[i3].x;
80 //                                      y=vertex0[i].y-vertex2[i3].y;
81 //                                      d=x*x+y*y;
82 //                                      x=vertex1[i2].x-vertex2[i3].x;
83 //                                      y=vertex1[i2].y-vertex2[i3].y;
84 //                                      d+=x*x+y*y;
85 //                                      x=vertex1[i2].x-vertex0[i].x;
86 //                                      y=vertex1[i2].y-vertex0[i].y;
87 //                                      d+=x*x+y*y;
88 //                                      if(d<dmax){
89 //                                              dmax=d;
90 //                                              o_vertex_id[0]=i;                                       
91 //                                              o_vertex_id[1]=i2;
92 //                                              o_vertex_id[2]=i3;
93 //                                      }
94 //                              }
95 //                      }
96 //              }
97 //              return;
98 //      }
99 //      /**
100 //       * 2矩形の頂点距離が最低の組合せを探す
101 //       * @param i_sqare
102 //       * @param o_vertex_id
103 //       */
104 //      public static void getMinimumLineVertex(NyARIntPoint[] i_sqare0,NyARIntPoint[] i_sqare1,int[] o_vertex_id)
105 //      {
106 //              //辺の長さが最小になる頂点の組合せを探す
107 //              int d;
108 //              int x,y;
109 //              int dmax=0x7fffffff;
110 //              for(int i=0;i<4;i++)
111 //              {
112 //                      for(int i2=0;i2<4;i2++)
113 //                      {
114 //                              x=i_sqare1[i2].x-i_sqare0[i].x;
115 //                              y=i_sqare1[i2].y-i_sqare0[i].y;
116 //                              d=x*x+y*y;
117 //                              if(d<dmax){
118 //                                      dmax=d;
119 //                                      o_vertex_id[0]=i;                                       
120 //                                      o_vertex_id[1]=i2;
121 //                              }
122 //                      }
123 //              }
124 //              return;
125 //      }       
126 //      
127 //      /**
128 //       * キーシンボルのインデックスを得る
129 //       * @param i_sqare
130 //       * @param i_vertex_id
131 //       * 最小三角形の頂点IDセット
132 //       * @return
133 //       */
134 //      public static int getKeySymble(NyARSquare[] i_sqare,int[] i_vertex_id)
135 //      {
136 //              //シンボルグループの重心を計算
137 //              int cx,cy;
138 //              cx=cy=0;
139 //              for(int i=0;i<3;i++)
140 //              {
141 //                      final NyARIntPoint[] sq_ptr=i_sqare[i].imvertex;
142 //                      cx+=sq_ptr[0].x;                        
143 //                      cx+=sq_ptr[1].x;                        
144 //                      cx+=sq_ptr[2].x;                        
145 //                      cx+=sq_ptr[3].x;                        
146 //                      cy+=sq_ptr[0].y;                        
147 //                      cy+=sq_ptr[1].y;                        
148 //                      cy+=sq_ptr[2].y;                        
149 //                      cy+=sq_ptr[3].y;                        
150 //              }
151 //              cx/=12;
152 //              cy/=12; 
153 //              //前段で探した頂点候補のうち、最も重心に近いものが中心シンボルの内対角点
154 //              int key_symble_idx=0;
155 //              int x=i_sqare[0].imvertex[i_vertex_id[0]].x-cx;
156 //              int y=i_sqare[0].imvertex[i_vertex_id[0]].y-cy;
157 //              int dmax=x*x+y*y;
158 //              for(int i=1;i<3;i++){
159 //                      x=i_sqare[i].imvertex[i_vertex_id[i]].x-cx;
160 //                      y=i_sqare[i].imvertex[i_vertex_id[i]].y-cy;
161 //                      final int d=x*x+y*y;
162 //                      if(d<dmax){
163 //                              dmax=d;
164 //                              key_symble_idx=i;
165 //                      }
166 //              }
167 //              return key_symble_idx;
168 //      }
169 //      public void bindSquare(NyARSquare i_sq1,int i_lv1,NyARSquare i_sq2,int i_lv2)
170 //      {
171 //              NyARSquare new_square=new NyARSquare();
172 //              //4辺の式を計算
173 //              new_square.line[0].copyFrom(i_sq1.line[(i_lv1)%4]);
174 //              new_square.line[1].copyFrom(i_sq1.line[(i_lv1+3)%4]);
175 //              new_square.line[2].copyFrom(i_sq2.line[(i_lv2)%4]);
176 //              new_square.line[3].copyFrom(i_sq2.line[(i_lv2+3)%4]);
177 //              //歪み無しの座標系を計算
178 //              final NyARDoublePoint2d[] l_sqvertex = new_square.sqvertex;
179 //              final NyARLinear[] l_line = new_square.line;            
180 //              for (int i = 0; i < 4; i++) {
181 //                      final NyARLinear l_line_i = l_line[i];
182 //                      final NyARLinear l_line_2 = l_line[(i + 3) % 4];
183 //                      final double w1 = l_line_2.run * l_line_i.rise - l_line_i.run * l_line_2.rise;
184 //                      if (w1 == 0.0) {
185 //                              return;
186 //                      }
187 //                      l_sqvertex[i].x = (l_line_2.rise * l_line_i.intercept - l_line_i.rise * l_line_2.intercept) / w1;
188 //                      l_sqvertex[i].y = (l_line_i.run * l_line_2.intercept - l_line_2.run * l_line_i.intercept) / w1;
189 ////                    // 頂点インデクスから頂点座標を得て保存
190 ////                    l_imvertex[i].x = i_xcoord[i_mkvertex[i]];
191 ////                    l_imvertex[i].y = i_ycoord[i_mkvertex[i]];
192 //              }
193 //              Graphics g=this.bimg.getGraphics();
194 //              g.setColor(Color.red);
195 //              int[] x=new int[4];
196 //              int[] y=new int[4];
197 //              for(int i=0;i<4;i++){
198 //                      x[i]=(int)l_sqvertex[i].x;
199 //                      y[i]=(int)l_sqvertex[i].y;
200 //              }
201 //              g.drawPolygon(x,y,4);
202 //              //基準点はVertexをそのまま採用
203 //              //2個の想定点は座標を逆変換して設定
204 //      }
205 //      /**
206 //       *
207 //       * @param i_sq
208 //       * @param o_sq
209 //       * @return
210 //       */
211 //      public boolean margeEdge(NyARSquare[] i_sq,NyARSquare o_sq)
212 //      {
213 //              int[] minimum_triangle_vertex=new int[3];
214 //              int[] minimum_line_vertex=new int[2];
215 //
216 //              //辺の長さが最小になる頂点の組合せを探す
217 //              getMinimumTriangleVertex(i_sq,minimum_triangle_vertex);
218 //              
219 //              //キーシンボルのインデクス番号を得る
220 //              int key_simble_idx=getKeySymble(i_sq,minimum_triangle_vertex);
221 //              
222 //              //エッジシンボルのインデックス番号を決める
223 //              int symbol_e1_idx=(key_simble_idx+1)%3;
224 //              int symbol_e2_idx=(key_simble_idx+2)%3;
225 //              
226 //              //エッジシンボル間で最短距離を取る頂点ペアを取る
227 //              //(角度を低くするとエラーが出やすい。対角線との類似性を確認する方法のほうがいい。多分)
228 //              getMinimumLineVertex(i_sq[symbol_e1_idx].imvertex,i_sq[symbol_e2_idx].imvertex,minimum_line_vertex);
229 //              
230 //              //内対角を外対角に変換
231 //              int lv1=(minimum_line_vertex[0]+2)%4;
232 //              int lv2=(minimum_line_vertex[1]+2)%4;
233 //              int kv =(minimum_triangle_vertex[key_simble_idx]+2)%4;
234 //              //矩形のバインド
235 //              bindSquare(i_sq[symbol_e1_idx],lv1,i_sq[symbol_e2_idx],lv2);
236 //                              
237 //              
238 //              Graphics g=this.bimg.getGraphics();
239 //              //内対角に緑の点を打つ
240 //              g.setColor(Color.green);
241 //              g.fillRect(i_sq[symbol_e1_idx].imvertex[lv1].x-2,i_sq[symbol_e1_idx].imvertex[lv1].y-2,4,4);
242 //              g.fillRect(i_sq[symbol_e2_idx].imvertex[lv2].x-2,i_sq[symbol_e2_idx].imvertex[lv2].y-2,4,4);
243 ////            g.fillRect(i_sq[symbol_e2_idx][minimum_line_vertex[1]].x-2,i_sq[symbol_e2_idx][minimum_line_vertex[1]].y-2,4,4);
244 //              
245 //              
246 //              //中央の中心エッジから最も遠い点が
247 //              //両端のエッジも探す
248 //              
249 //              
250 //              
251 //
252 ////            this.bimg.getGraphics().fillRect(i_sq[edge1_id][vid1_id].x,i_sq[edge1_id][vid1_id].y,5,5);
253 //              
254 //              for (int i = 0; i <3; i++) {
255 //                      int[] xp=new int[4]; 
256 //                      int[] yp=new int[4]; 
257 //                      for(int i2=0;i2<4;i2++){
258 //                              xp[i2]=i_sq[i].imvertex[i2].x;
259 //                              yp[i2]=i_sq[i].imvertex[i2].y;
260 //                      }
261 //                      this.bimg.getGraphics().setColor(Color.RED);
262 //                      this.bimg.getGraphics().drawPolygon(xp, yp,4);
263 //              }               
264 //              
265 //              
266 //              return false;
267 //              
268 //              
269 //              
270 //              
271 //
272 //              
273 //      }       
274 //      
275 //      
276 //      
277 //      
278 //}
279
280
281 /**
282  * 矩形座標をPCAではなく、頂点座標そのものからSquare位置を計算するクラス
283  * 
284  */
285 class NyARQRCodeDetector implements INyARSquareDetector
286 {
287         LabelingBufferdImage bimg;
288         private static final double VERTEX_FACTOR = 2.0;// 線検出のファクタ
289
290         private static final int AR_AREA_MAX = 100000;// #define AR_AREA_MAX 100000
291
292         private static final int AR_AREA_MIN = 70;// #define AR_AREA_MIN 70
293
294         private final int _width;
295
296         private final int _height;
297
298         private final NyARLabeling_ARToolKit _labeling;
299
300         private final NyARLabelingImage _limage;
301
302         private final NyARCameraDistortionFactor _dist_factor_ref;
303
304         /**
305          * 最大i_squre_max個のマーカーを検出するクラスを作成する。
306          * 
307          * @param i_param
308          */
309         public NyARQRCodeDetector(NyARCameraDistortionFactor i_dist_factor_ref, NyARIntSize i_size) throws NyARException
310         {
311                 this._width = i_size.w;
312                 this._height = i_size.h;
313                 this._dist_factor_ref = i_dist_factor_ref;
314                 this._labeling = new NyARLabeling_ARToolKit();
315                 this._limage = new NyARLabelingImage(this._width, this._height);
316                 this._labeling.attachDestination(this._limage);
317
318                 // 輪郭の最大長は画面に映りうる最大の長方形サイズ。
319                 int number_of_coord = (this._width + this._height) * 2;
320
321                 // 輪郭バッファは頂点変換をするので、輪郭バッファの2倍取る。
322                 this._max_coord = number_of_coord;
323                 this._xcoord = new int[number_of_coord * 2];
324                 this._ycoord = new int[number_of_coord * 2];
325         }
326
327         private final int _max_coord;
328
329         private final int[] _xcoord;
330
331         private final int[] _ycoord;
332
333         private void normalizeCoord(int[] i_coord_x, int[] i_coord_y, int i_index, int i_coord_num)
334         {
335                 // vertex1を境界にして、後方に配列を連結
336                 System.arraycopy(i_coord_x, 1, i_coord_x, i_coord_num, i_index);
337                 System.arraycopy(i_coord_y, 1, i_coord_y, i_coord_num, i_index);
338         }
339
340         private final int[] __detectMarker_mkvertex = new int[5];
341
342         /**
343          * ARMarkerInfo2 *arDetectMarker2( ARInt16 *limage, int label_num, int *label_ref,int *warea, double *wpos, int *wclip,int area_max, int area_min, double
344          * factor, int *marker_num ) 関数の代替品 ラベリング情報からマーカー一覧を作成してo_marker_listを更新します。 関数はo_marker_listに重なりを除外したマーカーリストを作成します。
345          * 
346          * @param i_raster
347          * 解析する2値ラスタイメージを指定します。
348          * @param o_square_stack
349          * 抽出した正方形候補を格納するリスト
350          * @throws NyARException
351          */
352         public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException
353         {
354                 final INyARLabeling labeling_proc = this._labeling;
355                 final NyARLabelingImage limage = this._limage;
356
357                 // 初期化
358
359                 // マーカーホルダをリセット
360                 o_square_stack.clear();
361
362                 // ラベリング
363                 labeling_proc.labeling(i_raster);
364
365                 // ラベル数が0ならここまで
366                 final int label_num = limage.getLabelStack().getLength();
367                 if (label_num < 1) {
368                         return;
369                 }
370
371                 final NyARLabelingLabelStack stack = limage.getLabelStack();
372                 // ラベルを大きい順に整列
373                 stack.sortByArea();
374                 final NyARLabelingLabel[] labels = stack.getArray();
375
376
377                 // デカいラベルを読み飛ばし
378                 int i;
379                 for (i = 0; i < label_num; i++) {
380                         // 検査対象内のラベルサイズになるまで無視
381                         if (labels[i].area <= AR_AREA_MAX) {
382                                 break;
383                         }
384                 }
385                 
386                 final int xsize = this._width;
387                 final int ysize = this._height;
388                 final int[] xcoord = this._xcoord;
389                 final int[] ycoord = this._ycoord;
390                 final int coord_max = this._max_coord;
391                 final int[] mkvertex = this.__detectMarker_mkvertex;
392                 final int[][] buf = (int[][]) limage.getBufferReader().getBuffer();
393                 final int[] indextable = limage.getIndexArray();
394                 int coord_num;
395                 int label_area;
396                 NyARLabelingLabel label_pt;
397                 NyARSquareStack wk_stack=new NyARSquareStack(100);
398
399                 for (; i < label_num; i++) {
400                         label_pt = labels[i];
401                         label_area = label_pt.area;
402                         // 検査対象サイズよりも小さくなったら終了
403                         if (label_area < AR_AREA_MIN) {
404                                 break;
405                         }
406                         // クリップ領域が画面の枠に接していれば除外
407                         if (label_pt.clip_l == 1 || label_pt.clip_r == xsize - 2) {// if(wclip[i*4+0] == 1 || wclip[i*4+1] ==xsize-2){
408                                 continue;
409                         }
410                         if (label_pt.clip_t == 1 || label_pt.clip_b == ysize - 2) {// if( wclip[i*4+2] == 1 || wclip[i*4+3] ==ysize-2){
411                                 continue;
412                         }
413                         // 特徴点候補であるかを確認する。
414                         if (!hasQrEdgeFeature(buf, indextable, label_pt)) {
415                                 continue;
416                         }
417
418                         // 輪郭を取得
419                         coord_num = limage.getContour(i, coord_max, xcoord, ycoord);
420                         if (coord_num == coord_max) {
421                                 // 輪郭が大きすぎる。
422                                 continue;
423                         }
424                         // 頂点候補のインデクスを取得
425                         final int vertex1 = scanVertex(xcoord, ycoord, coord_num);
426
427                         // 頂点候補(vertex1)を先頭に並べなおした配列を作成する。
428                         normalizeCoord(xcoord, ycoord, vertex1, coord_num);
429
430                         // 頂点情報を取得
431                         if (!getSquareVertex(xcoord, ycoord, vertex1, coord_num, label_area, mkvertex)) {
432                                 continue;
433                         }
434                         NyARSquare square=(NyARSquare)wk_stack.prePush();
435                         if(!getSquareLine(mkvertex,xcoord,ycoord,square)){
436                                 wk_stack.pop();
437                                 continue;
438                         }
439                 }
440                 bindQrcodeEdge(wk_stack);
441                 //エッジ同士の相関関係をしらべる。
442
443                 return;
444         }
445         /**
446          *
447          * @param i_sq
448          * @param o_sq
449          * @return
450          */
451         public boolean margeEdge(NyARSquare[] i_sq,NyARSquare o_sq)
452         {
453                 NyQrCodeSymbolBinder binder=new NyQrCodeSymbolBinder();
454                 binder.bimg=this.bimg;
455                 binder.(i_sq, o_sq);
456
457                 return false;
458                 
459                 
460                 
461                 
462         }
463         /**
464          * QRコードのエッジペアを作る
465          * @param i_square_stack
466          */
467         public void bindQrcodeEdge(NyARSquareStack i_square_stack)
468         {
469                 
470                 NyARSquare sq_ptr1,sq_ptr2,sq_ptr3;
471                 int number_of_edge=i_square_stack.getLength();
472                 if(number_of_edge<3){
473                         return;
474                 }
475                 NyARSquare[] sa=i_square_stack.getArray();
476                 for(int i=0;i<number_of_edge;i++)
477                 {       
478                         for(int i2=i+1;i2<number_of_edge;i2++)
479                         {
480                                 sq_ptr2=sa[i2];
481                                 for(int i3=i2+1;i3<number_of_edge;i3++){
482                                         sq_ptr3=sa[i3];
483                                         //3個のエッジの関連性を確認する。
484                                         margeEdge(sa,null);
485                                 }
486                                 //
487                         }
488                 }
489         }
490         /**
491          * 2つの頂点座標を結ぶ直線から、NyARLinearを計算する。
492          * 
493          * @param i_v1
494          * @param i_v2
495          * @param o_line
496          */
497         final private void getLine(NyARDoublePoint2d i_v1, NyARDoublePoint2d i_v2, NyARLinear o_line)
498         {
499                 final double x = i_v1.x - i_v2.x;
500                 final double y = i_v1.y - i_v2.y;
501                 final double x2 = x * x;
502                 final double y2 = y * y;
503                 final double rise_ = Math.sqrt(x2 / (x2 + y2));
504                 o_line.rise = rise_;
505                 o_line.run = Math.sqrt(y2 / (x2 + y2));
506                 if (x < 0) {
507                         if (y < 0) {
508                                 o_line.rise = -o_line.rise;
509                         } else {
510                                 o_line.rise = -o_line.rise;
511                                 o_line.run = -o_line.run;
512                         }
513                 } else {
514                         if (y < 0) {
515                                 o_line.rise = -o_line.rise;
516                                 o_line.run = -o_line.run;
517                         } else {
518                                 o_line.rise = -o_line.rise;
519                         }
520                 }
521                 o_line.intercept = (i_v1.y + (o_line.run / o_line.rise) * (i_v1.x)) * rise_;
522
523         }
524         private final INyARPca2d _pca=new NyARPca2d_MatrixPCA_O2(100);
525         private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22();
526         private final NyARDoublePoint2d __getSquareLine_mean=new NyARDoublePoint2d();
527         private final NyARDoublePoint2d __getSquareLine_ev=new NyARDoublePoint2d();
528         /**
529          * 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
530          * coord_num,int vertex[], double line[4][3], double v[4][2], double *dist_factor) の2関数の合成品です。 マーカーのvertex,lineを計算して、結果をo_squareに保管します。
531          * Optimize:STEP[424->391]
532          * 
533          * @param i_cparam
534          * @return
535          * @throws NyARException
536          */
537         private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord, NyARSquare o_square) throws NyARException
538         {
539                 final NyARLinear[] l_line = o_square.line;
540                 final NyARCameraDistortionFactor dist_factor=this._dist_factor_ref;  
541                 final NyARDoubleMatrix22 evec=this.__getSquareLine_evec;
542                 final NyARDoublePoint2d mean=this.__getSquareLine_mean;
543                 final NyARDoublePoint2d ev=this.__getSquareLine_ev;
544         
545                 
546                 for (int i = 0; i < 4; i++) {
547                         final double w1 = (double) (i_mkvertex[i + 1] - i_mkvertex[i] + 1) * 0.05 + 0.5;
548                         final int st = (int) (i_mkvertex[i] + w1);
549                         final int ed = (int) (i_mkvertex[i + 1] - w1);
550                         final int n = ed - st + 1;
551                         if (n < 2) {
552                                 // nが2以下でmatrix.PCAを計算することはできないので、エラー
553                                 return false;
554                         }
555                         //主成分分析する。
556                         this._pca.pcaWithDistortionFactor(i_xcoord, i_ycoord, st, n,dist_factor, evec, ev,mean);
557                         final NyARLinear l_line_i = l_line[i];
558                         l_line_i.run = evec.m01;// line[i][0] = evec->m[1];
559                         l_line_i.rise = -evec.m00;// line[i][1] = -evec->m[0];
560                         l_line_i.intercept = -(l_line_i.run * mean.x + l_line_i.rise * mean.y);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);
561                 }
562
563                 final NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex;
564                 final NyARIntPoint2d[] l_imvertex = o_square.imvertex;
565                 for (int i = 0; i < 4; i++) {
566                         final NyARLinear l_line_i = l_line[i];
567                         final NyARLinear l_line_2 = l_line[(i + 3) % 4];
568                         final double w1 = l_line_2.run * l_line_i.rise - l_line_i.run * l_line_2.rise;
569                         if (w1 == 0.0) {
570                                 return false;
571                         }
572                         l_sqvertex[i].x = (l_line_2.rise * l_line_i.intercept - l_line_i.rise * l_line_2.intercept) / w1;
573                         l_sqvertex[i].y = (l_line_i.run * l_line_2.intercept - l_line_2.run * l_line_i.intercept) / w1;
574                         // 頂点インデクスから頂点座標を得て保存
575                         l_imvertex[i].x = i_xcoord[i_mkvertex[i]];
576                         l_imvertex[i].y = i_ycoord[i_mkvertex[i]];
577                 }
578                 return true;
579         }
580         /**
581          * 辺からの対角線が最長になる点を対角線候補として返す。
582          * 
583          * @param i_xcoord
584          * @param i_ycoord
585          * @param i_coord_num
586          * @return
587          */
588         private int scanVertex(int[] i_xcoord, int[] i_ycoord, int i_coord_num)
589         {
590                 final int sx = i_xcoord[0];
591                 final int sy = i_ycoord[0];
592                 int d = 0;
593                 int w, x, y;
594                 int ret = 0;
595                 for (int i = 1; i < i_coord_num; i++) {
596                         x = i_xcoord[i] - sx;
597                         y = i_ycoord[i] - sy;
598                         w = x * x + y * y;
599                         if (w > d) {
600                                 d = w;
601                                 ret = i;
602                         }
603                         // ここでうまく終了条件入れられないかな。
604                 }
605                 return ret;
606         }
607
608         private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter();
609
610         private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter();
611
612         /**
613          * static int arDetectMarker2_check_square( int area, ARMarkerInfo2 *marker_info2, double factor ) 関数の代替関数 OPTIMIZED STEP [450->415] o_squareに頂点情報をセットします。
614          * 
615          * @param i_x_coord
616          * @param i_y_coord
617          * @param i_vertex1_index
618          * @param i_coord_num
619          * @param i_area
620          * @param o_vertex
621          * 要素数はint[4]である事
622          * @return
623          */
624         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)
625         {
626                 final NyARVertexCounter wv1 = this.__getSquareVertex_wv1;
627                 final NyARVertexCounter wv2 = this.__getSquareVertex_wv2;
628                 final int end_of_coord = i_vertex1_index + i_coord_num - 1;
629                 final int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0];
630                 final int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0];
631                 int dmax = 0;
632                 int v1 = i_vertex1_index;
633                 for (int i = 1 + i_vertex1_index; i < end_of_coord; i++) {// for(i=1;i<marker_info2->coord_num-1;i++)
634                         // {
635                         final int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy);
636                         if (d > dmax) {
637                                 dmax = d;
638                                 v1 = i;
639                         }
640                 }
641                 final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR;
642
643                 o_vertex[0] = i_vertex1_index;
644
645                 if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v1, thresh)) { // if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,0,v1,thresh,wv1,&wvnum1)<
646                         // 0 ) {
647                         return false;
648                 }
649                 if (!wv2.getVertex(i_x_coord, i_y_coord, v1, end_of_coord, thresh)) {// if(get_vertex(marker_info2->x_coord,marker_info2->y_coord,v1,marker_info2->coord_num-1,thresh,wv2,&wvnum2)
650                         // < 0) {
651                         return false;
652                 }
653
654                 int v2;
655                 if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {// if(wvnum1 == 1 && wvnum2== 1) {
656                         o_vertex[1] = wv1.vertex[0];
657                         o_vertex[2] = v1;
658                         o_vertex[3] = wv2.vertex[0];
659                 } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {// }else if( wvnum1 > 1 && wvnum2== 0) {
660                         // 頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。
661                         v2 = (v1 - i_vertex1_index) / 2 + i_vertex1_index;
662                         if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh)) {
663                                 return false;
664                         }
665                         if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh)) {
666                                 return false;
667                         }
668                         if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {
669                                 o_vertex[1] = wv1.vertex[0];
670                                 o_vertex[2] = wv2.vertex[0];
671                                 o_vertex[3] = v1;
672                         } else {
673                                 return false;
674                         }
675                 } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) {
676                         // v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index;
677                         v2 = (v1 + end_of_coord) / 2;
678
679                         if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh)) {
680                                 return false;
681                         }
682                         if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh)) {
683                                 return false;
684                         }
685                         if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {
686                                 o_vertex[1] = v1;
687                                 o_vertex[2] = wv1.vertex[0];
688                                 o_vertex[3] = wv2.vertex[0];
689                         } else {
690                                 return false;
691                         }
692                 } else {
693                         return false;
694                 }
695                 o_vertex[4] = end_of_coord;
696                 return true;
697         }
698         /**
699          * QRコードのエッジ特徴を持つラベルであるかを調べる
700          * @param buf
701          * @param index_table
702          * @param i_label
703          * @return
704          */
705         private boolean hasQrEdgeFeature(int buf[][], int[] index_table, NyARLabelingLabel i_label)
706         {
707                 int tx, bx;
708                 int w;
709                 int i_label_id = i_label.id;
710                 int[] limage_j;
711                 final int clip_l = i_label.clip_l;
712                 final int clip_b = i_label.clip_b;
713                 final int clip_r = i_label.clip_r;
714                 final int clip_t = i_label.clip_t;
715
716                 tx = bx = 0;
717                 // 上接点(→)
718                 limage_j = buf[clip_t];
719                 for (int i = clip_l; i <= clip_r; i++) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) {
720                         w = limage_j[i];
721                         if (w > 0 && index_table[w - 1] == i_label_id) {
722                                 tx = i;
723                                 break;
724                         }
725                 }
726                 // 下接点(←)
727                 limage_j = buf[clip_b];
728                 for (int i = clip_r; i >= clip_l; i--) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) {
729                         w = limage_j[i];
730                         if (w > 0 && index_table[w - 1] == i_label_id) {
731                                 bx = i;
732                                 break;
733                         }
734                 }
735                 final int cx = (clip_l + clip_r) / 2;
736                 final int cy = (clip_t + clip_b) / 2;
737                 // 横断チェック(中心から線を引いて、101になるかしらべる)
738                 if (!checkDiagonalLine(buf, cx, cy, bx, clip_b)) {
739                         return false;
740                 }
741                 if (!checkDiagonalLine(buf, tx, clip_t, cx, cy)) {
742                         return false;
743                 }
744                 return true;
745         }
746
747         /**
748          * 対角線のパターンを調べる。
749          * 
750          * @param buf
751          * @param i_px1
752          * @param i_py1
753          * @param i_px2
754          * @param i_py2
755          * @return
756          */
757         private boolean checkDiagonalLine(int[][] buf, int i_px1, int i_py1, int i_px2, int i_py2)
758         {
759                 int sub_y = i_py2 - i_py1;
760                 int sub_x = i_px2 - i_px1;
761                 // 黒
762                 int i = 0;
763                 for (; i < sub_y; i++) {
764                         int yp = i_py1 + i;
765                         int xp = i_px1 + i * sub_x / sub_y;
766                         if (buf[yp][xp] == 0 && buf[yp][xp-1] == 0 && buf[yp][xp+1] == 0) {
767                                 break;
768                         }
769
770                 }
771                 if (i == sub_y) {
772                         return false;
773                 }
774                 // 白
775                 for (; i < sub_y; i++) {
776                         int yp = i_py1 + i;
777                         int xp = i_px1 + i * sub_x / sub_y;
778                         if (buf[yp][xp] != 0 && buf[yp][xp-1] != 0 && buf[yp][xp+1] != 0) {
779                                 break;
780                         }
781
782                 }
783                 if (i == sub_y) {
784                         return false;
785                 }
786                 // 黒
787                 for (; i < sub_y; i++) {
788                         int yp = i_py1 + i;
789                         int xp = i_px1 + i * sub_x / sub_y;
790                         if (buf[yp][xp] == 0 && buf[yp][xp-1] == 0 && buf[yp][xp+1] == 0) {
791                                 break;
792                         }
793
794                 }
795                 if (i != sub_y) {
796                         return false;
797                 }
798                 // 端まで到達したらOK
799                 return true;
800         }
801
802 }
803
804
805 public class LabelingCamera extends Frame implements JmfCaptureListener
806 {
807         private final String camera_file = "../Data/camera_para.dat";
808
809         private JmfNyARRaster_RGB _raster;
810
811         private JmfCameraCapture capture;
812         private NyARParam ap;
813         public LabelingCamera() throws NyARException, NyARException
814         {
815                 setBounds(0, 0, 640 + 64, 720 + 64);
816                 // キャプチャの準備
817                 capture = new JmfCameraCapture(320, 240, 30f, JmfCameraCapture.PIXEL_FORMAT_RGB);
818                 capture.setCaptureListener(this);
819
820                 // キャプチャイメージ用のラスタを準備
821                 this._raster = new JmfNyARRaster_RGB(320, 240);
822                 
823                 // AR用カメラパラメタファイルをロード
824                 ap = new NyARParam();
825                 ap.loadARParamFromFile(camera_file);
826                 ap.changeScreenSize(320, 240);          
827                 
828                 
829         }
830
831         // そのラベルが特徴点候補か返す。
832
833         private NyARBinRaster _binraster1 = new NyARBinRaster(320, 240);
834
835         private NyARGrayscaleRaster _gsraster1 = new NyARGrayscaleRaster(320, 240);
836
837         private NyARLabelingImage _limage = new NyARLabelingImage(320, 240);
838
839         private LabelingBufferdImage _bimg = new LabelingBufferdImage(320, 240);
840
841         private NyARRasterFilter_ARToolkitThreshold filter_gs2bin;
842
843         public void onUpdateBuffer(Buffer i_buffer)
844         {
845                 NyARRasterFilter_AreaAverage gs2bin=new NyARRasterFilter_AreaAverage();
846
847                 try {
848                         // キャプチャしたバッファをラスタにセット
849                         _raster.setBuffer(i_buffer);
850
851                         Graphics g = getGraphics();
852                         // キャプチャ画像
853                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
854                         Image img = b2i.createImage(i_buffer);
855                         this.getGraphics().drawImage(img, 32, 32, this);
856
857                         // 画像1
858                         INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbAve();
859                         filter_rgb2gs.doFilter(_raster, _gsraster1);
860                         this._bimg.drawImage(this._gsraster1);
861                         this.getGraphics().drawImage(this._bimg, 32 + 320, 32, 320 + 320 + 32, 240 + 32, 0, 240, 320, 0, this);
862                         
863
864                         // 画像2
865                         gs2bin.doFilter(_gsraster1, _binraster1);
866                         this._bimg.drawImage(_binraster1);
867                         this.getGraphics().drawImage(this._bimg, 32, 32 + 240, 320 + 32, 240 + 32 + 240, 0, 240, 320, 0, this);
868
869                         // 画像3
870                         NyARLabelingImage limage = new NyARLabelingImage(320, 240);
871                         NyARLabeling_ARToolKit labeling = new NyARLabeling_ARToolKit();
872                         labeling.attachDestination(limage);
873                         labeling.labeling(_binraster1);
874                         this._bimg.drawImage(this._gsraster1);
875                         NyARLabelingLabel[] labels =  limage.getLabelStack().getArray();
876
877                         NyARSquareStack stack = new NyARSquareStack(100);
878                         NyARQRCodeDetector detect = new NyARQRCodeDetector(ap.getDistortionFactor(), new NyARIntSize(320,240));
879                         detect.bimg=this._bimg;
880
881                         detect.detectMarker(_binraster1, stack);
882                         for (int i = 0; i < stack.getLength(); i++) {
883                                 NyARSquare[] square_ptr = (NyARSquare[]) stack.getArray();
884                                 int[] xp=new int[4]; 
885                                 int[] yp=new int[4]; 
886                                 for(int i2=0;i2<4;i2++){
887                                         xp[i2]=square_ptr[i].imvertex[i2].x;
888                                         yp[i2]=square_ptr[i].imvertex[i2].y;
889                                 }
890                                 this._bimg.getGraphics().setColor(Color.RED);
891                                 this._bimg.getGraphics().drawPolygon(xp, yp,2);
892                         }
893                         this.getGraphics().drawImage(this._bimg, 32 + 320, 32 + 240, 320 + 32 + 320, 240 + 32 + 240, 0, 240, 320, 0, this);
894
895                         // 画像3
896                         // threshold.debugDrawHistgramMap(_workraster, _workraster2);
897                         // this._bimg2.setImage(this._workraster2);
898                         // this.getGraphics().drawImage(this._bimg2, 32+320, 32+240,320+32+320,240+32+240,0,240,320,0, this);
899
900                         // 画像4
901                         // NyARRasterThresholdAnalyzer_SlidePTile threshold=new NyARRasterThresholdAnalyzer_SlidePTile(15);
902                         // threshold.analyzeRaster(_gsraster1);
903                         // filter_gs2bin=new NyARRasterFilter_AreaAverage();
904                         // filter_gs2bin.doFilter(_gsraster1, _binraster1);
905                         // this._bimg.drawImage(_binraster1);
906
907                         // NyARRasterDetector_QrCodeEdge detector=new NyARRasterDetector_QrCodeEdge(10000);
908                         // detector.analyzeRaster(_binraster1);
909
910                         // this._bimg.overlayData(detector.geResult());
911
912                         // this.getGraphics().drawImage(this._bimg, 32, 32+480,320+32,480+32+240,0,240,320,0, this);
913                         // 画像5
914
915                         /*
916                          * threshold2.debugDrawHistgramMap(_workraster, _workraster2); this._bimg2.drawImage(this._workraster2); this.getGraphics().drawImage(this._bimg2,
917                          * 32+320, 32+480,320+32+320,480+32+240,0,240,320,0, this);
918                          */
919
920                         // this.getGraphics().drawImage(this._bimg, 32, 32, this);
921
922                 } catch (Exception e) {
923                         e.printStackTrace();
924                 }
925
926         }
927
928         private INyARLabeling labelingFactory(int i_idx)
929         {
930                 // switch(i_idx){
931                 // case 0:{NyARLabeling_ARToolKit l=new NyARLabeling_ARToolKit();l.setThresh(4);return l;}
932                 // case 1:{return new NyLineLabeling();}
933                 // }
934                 return null;
935
936         }
937
938         private void startCapture()
939         {
940                 try {
941                         capture.start();
942                 } catch (Exception e) {
943                         e.printStackTrace();
944                 }
945         }
946
947         public static void main(String[] args)
948         {
949                 try {
950                         LabelingCamera mainwin = new LabelingCamera();
951                         mainwin.setVisible(true);
952                         mainwin.startCapture();
953                 } catch (Exception e) {
954                         e.printStackTrace();
955                 }
956
957         }
958
959 }