From f62748087666f234934344899305bfa525d314cd Mon Sep 17 00:00:00 2001 From: nyatla Date: Sat, 27 Sep 2008 07:14:12 +0000 Subject: [PATCH] =?utf8?q?[=E3=83=90=E3=83=83=E3=82=AF=E3=82=A2=E3=83=83?= =?utf8?q?=E3=83=97]NyARToolkit?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nyartoolkit/jogl/sample/JavaSimpleLite.java | 57 +- .../nyartoolkit/jogl/sample/JavaSimpleLite2.java | 53 +- .../toys/qrcode/NyARQrCodeDetector.java | 489 +++++++++++ .../toys/qrcode/NyARQrCodeSymbolBinder.java | 298 +++++++ .../qrcode/NyARRasterFilter_QrAreaAverage.java | 89 ++ .../toys/qrcode/NyARSingleQrDetector.java | 159 ++++ .../nyartoolkit/toys/qrcode/SingleQrSample.java | 242 ++++++ .../nyartoolkit/toys/qrcode/SingleQrTest.java | 189 +++++ ...QrCodeDetector.java => NyARVertexDetector.java} | 5 +- .../toys/x2/GLNyARSingleDetectMarker_X2.java | 1 - .../nyartoolkit/toys/x2/JavaSimpleLite_X2.java | 2 +- .../nyatla/nyartoolkit/toys/x2/RawFileTest_X2.java | 1 - .../nyartoolkit/jogl/utils/GLNyARDetectMarker.java | 95 --- .../nyatla/nyartoolkit/jogl/utils/GLNyARParam.java | 127 --- .../jogl/utils/GLNyARSingleDetectMarker.java | 103 --- .../nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java | 121 +++ src/jp/nyatla/nyartoolkit/core/NyARSquare.java | 6 + .../nyartoolkit/core/NyARSquareDetector.java | 9 +- ...ayscaleRaster.java => NyARGrayscaleRaster.java} | 4 +- .../rasterfilter/INyARRasterFilter_GsToBin.java | 2 +- .../rasterfilter/INyARRasterFilter_RgbToGs.java | 2 +- .../gs2bin/NyARRasterFilter_AreaAverage.java | 2 +- .../gs2bin/NyARRasterFilter_Threshold.java | 2 +- .../rgb2gs/NyARRasterFilter_RgbAve.java | 2 +- .../rgb2gs/NyARRasterFilter_RgbMul.java | 6 +- .../rgb2gs/NyARRasterFilter_RgbOr.java | 2 +- src/jp/nyatla/utils/j2se/LabelingBufferdImage.java | 2 +- .../nyartoolkit/dev/CopyOfLabelingCamera.java | 920 --------------------- test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java | 487 ++++++----- 29 files changed, 1904 insertions(+), 1573 deletions(-) create mode 100644 sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeDetector.java create mode 100644 sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeSymbolBinder.java create mode 100644 sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARRasterFilter_QrAreaAverage.java create mode 100644 sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARSingleQrDetector.java create mode 100644 sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrSample.java create mode 100644 sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrTest.java rename sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/{NyARQrCodeDetector.java => NyARVertexDetector.java} (96%) delete mode 100644 src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java delete mode 100644 src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java delete mode 100644 src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java rename src/jp/nyatla/nyartoolkit/core/raster/{NyARGlayscaleRaster.java => NyARGrayscaleRaster.java} (91%) delete mode 100644 test/jp/nyatla/nyartoolkit/dev/CopyOfLabelingCamera.java diff --git a/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java b/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java index 970c0ec..c56c5f7 100644 --- a/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java +++ b/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite.java @@ -26,23 +26,16 @@ */ package jp.nyatla.nyartoolkit.jogl.sample; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; +import java.awt.event.*; import java.awt.*; - import javax.media.Buffer; - -import javax.media.opengl.GL; -import javax.media.opengl.GLAutoDrawable; -import javax.media.opengl.GLEventListener; -import javax.media.opengl.GLCanvas; - -import com.sun.opengl.util.Animator; - -import jp.nyatla.nyartoolkit.core.NyARCode; - -import jp.nyatla.nyartoolkit.jmf.utils.JmfCameraCapture; -import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener; +import javax.media.opengl.*; +import com.sun.opengl.util.*; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.*; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.detector.*; +import jp.nyatla.nyartoolkit.jmf.utils.*; import jp.nyatla.nyartoolkit.jogl.utils.*; /** * simpleLiteと同じようなテストプログラム @@ -67,14 +60,14 @@ public class JavaSimpleLite implements GLEventListener, JmfCaptureListener private JmfCameraCapture _capture; private GL _gl; - private NyARGLUtil _glnya; //NyARToolkit関係 - private GLNyARSingleDetectMarker _nya; - - private GLNyARParam _ar_param; + private NyARSingleDetectMarker _nya; + private NyARParam _ar_param; + private double[] _camera_projection=new double[16]; + /** * 立方体を書く * @@ -150,11 +143,11 @@ public class JavaSimpleLite implements GLEventListener, JmfCaptureListener _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB); _capture.setCaptureListener(this); //NyARToolkitの準備 - _ar_param = new GLNyARParam(); + _ar_param = new NyARParam(); NyARCode ar_code = new NyARCode(16, 16); _ar_param.loadARParamFromFile(PARAM_FILE); _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y); - _nya = new GLNyARSingleDetectMarker(_ar_param, ar_code, 80.0); + _nya = new NyARSingleDetectMarker(_ar_param, ar_code, 80.0); _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。 ar_code.loadARPattFromFile(CARCODE_FILE); //NyARToolkit用の支援クラス @@ -166,10 +159,12 @@ public class JavaSimpleLite implements GLEventListener, JmfCaptureListener } catch (Exception e) { e.printStackTrace(); } - _animator = new Animator(drawable); + //カメラパラメータの計算 + _glnya.toCameraFrustumRH(_ar_param,_camera_projection); + _animator = new Animator(drawable); _animator.start(); - + return; } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) @@ -184,10 +179,12 @@ public class JavaSimpleLite implements GLEventListener, JmfCaptureListener _gl.glMatrixMode(GL.GL_MODELVIEW); _gl.glLoadIdentity(); } - + private NyARTransMatResult __display_transmat_result=new NyARTransMatResult(); + private double[] __display_wk=new double[16]; + public void display(GLAutoDrawable drawable) { - + NyARTransMatResult transmat_result=__display_transmat_result; try { if (!_cap_image.hasData()) { return; @@ -200,16 +197,20 @@ public class JavaSimpleLite implements GLEventListener, JmfCaptureListener //背景を書く _glnya.drawBackGround(_cap_image, 1.0); } - //あったら立方体を書く + //マーカーがあれば、立方体を描画 if (is_marker_exist) { //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。 // Projection transformation. _gl.glMatrixMode(GL.GL_PROJECTION); - _gl.glLoadMatrixd(_ar_param.getCameraFrustumRH(), 0); + _gl.glLoadMatrixd(_camera_projection, 0); _gl.glMatrixMode(GL.GL_MODELVIEW); // Viewing transformation. _gl.glLoadIdentity(); - _gl.glLoadMatrixd(_nya.getCameraViewRH(), 0); + //変換行列を取得 + _nya.getTransmationMatrix(transmat_result); + //変換行列をOpenGL形式に変換 + _glnya.toCameraViewRH(transmat_result, __display_wk); + _gl.glLoadMatrixd(__display_wk, 0); // All other lighting and geometry goes here. drawCube(); diff --git a/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java b/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java index 8748b45..9f65915 100644 --- a/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java +++ b/sample/jogl/jp/nyatla/nyartoolkit/jogl/sample/JavaSimpleLite2.java @@ -27,23 +27,16 @@ package jp.nyatla.nyartoolkit.jogl.sample; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; +import java.awt.event.*; import java.awt.*; - -import javax.media.Buffer; - -import javax.media.opengl.GL; -import javax.media.opengl.GLAutoDrawable; -import javax.media.opengl.GLEventListener; -import javax.media.opengl.GLCanvas; - +import javax.media.*; +import javax.media.opengl.*; import com.sun.opengl.util.Animator; - -import jp.nyatla.nyartoolkit.core.NyARCode; - -import jp.nyatla.nyartoolkit.jmf.utils.JmfCameraCapture; -import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener; +import jp.nyatla.nyartoolkit.core.param.*; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.detector.*; +import jp.nyatla.nyartoolkit.jmf.utils.*; import jp.nyatla.nyartoolkit.jogl.utils.*; /** * simpleLiteの複数マーカー同時認識バージョン @@ -73,10 +66,10 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener private NyARGLUtil _glnya; //NyARToolkit関係 - private GLNyARDetectMarker _nya; - - private GLNyARParam _ar_param; + private NyARDetectMarker _nya; + private NyARParam _ar_param; + private double[] _camera_projection=new double[16]; /** * 立方体を書く * @@ -152,7 +145,7 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB); _capture.setCaptureListener(this); //NyARToolkitの準備 - _ar_param = new GLNyARParam(); + _ar_param = new NyARParam(); _ar_param.loadARParamFromFile(PARAM_FILE); _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y); @@ -163,7 +156,7 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener ar_codes[0].loadARPattFromFile(CARCODE_FILE1); ar_codes[1] = new NyARCode(16, 16); ar_codes[1].loadARPattFromFile(CARCODE_FILE2); - _nya = new GLNyARDetectMarker(_ar_param, ar_codes, width, 2); + _nya = new NyARDetectMarker(_ar_param, ar_codes, width, 2); _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。 //NyARToolkit用の支援クラス _glnya = new NyARGLUtil(_gl); @@ -174,8 +167,10 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener } catch (Exception e) { e.printStackTrace(); } + //カメラパラメータの計算 + _glnya.toCameraFrustumRH(_ar_param,_camera_projection); + _animator = new Animator(drawable); - _animator.start(); } @@ -192,9 +187,12 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener _gl.glMatrixMode(GL.GL_MODELVIEW); _gl.glLoadIdentity(); } - + private NyARTransMatResult __display_transmat_result=new NyARTransMatResult(); + private double[] __display_wk=new double[16]; + public void display(GLAutoDrawable drawable) { + NyARTransMatResult transmat_result=__display_transmat_result; try { if (!_cap_image.hasData()) { @@ -209,7 +207,6 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener _glnya.drawBackGround(_cap_image, 1.0); } //あったら立方体を書く - double[] matrix = new double[16]; for (int i = 0; i < found_markers; i++) { //1番のマーカーでなければ表示しない。 if (_nya.getARCodeIndex(i) != 0) { @@ -218,12 +215,15 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。 // Projection transformation. _gl.glMatrixMode(GL.GL_PROJECTION); - _gl.glLoadMatrixd(_ar_param.getCameraFrustumRH(), 0); + _gl.glLoadMatrixd(_camera_projection, 0); _gl.glMatrixMode(GL.GL_MODELVIEW); // Viewing transformation. _gl.glLoadIdentity(); - _nya.getCameraViewRH(i, matrix); - _gl.glLoadMatrixd(matrix, 0); + //変換行列を取得 + _nya.getTransmationMatrix(i,transmat_result); + //変換行列をOpenGL形式に変換 + _glnya.toCameraViewRH(transmat_result, __display_wk); + _gl.glLoadMatrixd(__display_wk, 0); // All other lighting and geometry goes here. drawCube(); @@ -232,6 +232,7 @@ public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener } catch (Exception e) { e.printStackTrace(); } + return; } public void onUpdateBuffer(Buffer i_buffer) diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeDetector.java b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeDetector.java new file mode 100644 index 0000000..694a35c --- /dev/null +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeDetector.java @@ -0,0 +1,489 @@ +package jp.nyatla.nyartoolkit.toys.qrcode; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.NyARSquare; +import jp.nyatla.nyartoolkit.core.NyARSquareStack; +import jp.nyatla.nyartoolkit.core.NyARVertexCounter; +import jp.nyatla.nyartoolkit.core.labeling.INyARLabeling; +import jp.nyatla.nyartoolkit.core.labeling.NyARLabelingImage; +import jp.nyatla.nyartoolkit.core.labeling.NyARLabelingLabel; +import jp.nyatla.nyartoolkit.core.labeling.NyARLabelingLabelStack; +import jp.nyatla.nyartoolkit.core.labeling.NyARLabeling_ARToolKit; +import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor; +import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d; +import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2; +import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARIntPoint; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; +import jp.nyatla.nyartoolkit.core.types.NyARLinear; +import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22; +import jp.nyatla.utils.j2se.LabelingBufferdImage; + +public class NyARQrCodeDetector implements INyARSquareDetector +{ + private NyARQrCodeSymbolBinder _binder; + private static final double VERTEX_FACTOR = 2.0;// 線検出のファクタ + + private static final int AR_AREA_MAX = 10000; + + private static final int AR_AREA_MIN = 50; + + private final int _width; + + private final int _height; + + private final NyARLabeling_ARToolKit _labeling; + + private final NyARLabelingImage _limage; + + private final NyARCameraDistortionFactor _dist_factor_ref; + + /** + * 最大i_squre_max個のマーカーを検出するクラスを作成する。 + * + * @param i_param + */ + public NyARQrCodeDetector(NyARCameraDistortionFactor i_dist_factor_ref, NyARIntSize i_size) throws NyARException + { + this._width = i_size.w; + this._height = i_size.h; + this._dist_factor_ref = i_dist_factor_ref; + this._labeling = new NyARLabeling_ARToolKit(); + this._limage = new NyARLabelingImage(this._width, this._height); + this._labeling.attachDestination(this._limage); + this._binder=new NyARQrCodeSymbolBinder(i_dist_factor_ref); + + // 輪郭の最大長はMAX_COORD_NUMの2倍に制限 + int number_of_coord = MAX_COORD_NUM* 2; + + // 輪郭バッファはnumber_of_coordの2倍 + this._max_coord = number_of_coord; + this._xcoord = new int[number_of_coord * 2]; + this._ycoord = new int[number_of_coord * 2]; + } + + private final int _max_coord; + + private final int[] _xcoord; + + private final int[] _ycoord; + + private void normalizeCoord(int[] i_coord_x, int[] i_coord_y, int i_index, int i_coord_num) + { + // vertex1を境界にして、後方に配列を連結 + System.arraycopy(i_coord_x, 1, i_coord_x, i_coord_num, i_index); + System.arraycopy(i_coord_y, 1, i_coord_y, i_coord_num, i_index); + } + + private final int[] __detectMarker_mkvertex = new int[5]; + + /** + * 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 ) 関数の代替品 ラベリング情報からマーカー一覧を作成してo_marker_listを更新します。 関数はo_marker_listに重なりを除外したマーカーリストを作成します。 + * + * @param i_raster + * 解析する2値ラスタイメージを指定します。 + * @param o_square_stack + * 抽出した正方形候補を格納するリスト + * @throws NyARException + */ + public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException + { + final INyARLabeling labeling_proc = this._labeling; + final NyARLabelingImage limage = this._limage; + + // 初期化 + + // マーカーホルダをリセット + o_square_stack.clear(); + + // ラベリング + labeling_proc.labeling(i_raster); + + // ラベル数が0ならここまで + final int label_num = limage.getLabelStack().getLength(); + if (label_num < 1) { + return; + } + + final NyARLabelingLabelStack stack = limage.getLabelStack(); + final NyARLabelingLabel[] labels = (NyARLabelingLabel[]) stack.getArray(); + + // ラベルを大きい順に整列 + stack.sortByArea(); + + // デカいラベルを読み飛ばし + int i; + for (i = 0; i < label_num; i++) { + // 検査対象内のラベルサイズになるまで無視 + if (labels[i].area <= AR_AREA_MAX) { + break; + } + } + + final int xsize = this._width; + final int ysize = this._height; + final int[] xcoord = this._xcoord; + final int[] ycoord = this._ycoord; + final int coord_max = this._max_coord; + final int[] mkvertex = this.__detectMarker_mkvertex; + final int[][] buf = (int[][]) limage.getBufferReader().getBuffer(); + final int[] indextable = limage.getIndexArray(); + int coord_num; + int label_area; + NyARLabelingLabel label_pt; + NyARSquareStack wk_stack=new NyARSquareStack(100); + wk_stack.clear(); + + for (; i < label_num; i++) { + label_pt = labels[i]; + label_area = label_pt.area; + // 検査対象サイズよりも小さくなったら終了 + if (label_area < AR_AREA_MIN) { + break; + } + // クリップ領域が画面の枠に接していれば除外 + if (label_pt.clip_l == 1 || label_pt.clip_r == xsize - 2) {// if(wclip[i*4+0] == 1 || wclip[i*4+1] ==xsize-2){ + continue; + } + if (label_pt.clip_t == 1 || label_pt.clip_b == ysize - 2) {// if( wclip[i*4+2] == 1 || wclip[i*4+3] ==ysize-2){ + continue; + } + // 特徴点候補であるかを確認する。 + if (!hasQrEdgeFeature(buf, indextable, label_pt)) { + continue; + } + + // 輪郭を取得 + coord_num = limage.getContour(i, coord_max, xcoord, ycoord); + if (coord_num == coord_max) { + // 輪郭が大きすぎる。 + continue; + } + // 頂点候補のインデクスを取得 + final int vertex1 = scanVertex(xcoord, ycoord, coord_num); + + // 頂点候補(vertex1)を先頭に並べなおした配列を作成する。 + normalizeCoord(xcoord, ycoord, vertex1, coord_num); + + // 頂点情報を取得 + if (!getSquareVertex(xcoord, ycoord, vertex1, coord_num, label_area, mkvertex)) { + continue; + } + NyARSquare square=(NyARSquare)wk_stack.prePush(); + //矩形からラインと観察座標を取得 + if(!getSquareLine(mkvertex,xcoord,ycoord,square.line,square.imvertex)){ + wk_stack.pop(); + continue; + } + } + //シンボルの関連付け + bindQrcodeEdge(wk_stack,o_square_stack); + //エッジ同士の相関関係をしらべる。 + + return; + } + + /** + * QRコードのエッジペアを作る + * @param i_square_stack + */ + public void bindQrcodeEdge(NyARSquareStack i_square_stack,NyARSquareStack o_square_stack) throws NyARException + { + NyARSquare[] group=new NyARSquare[3]; + int number_of_edge=i_square_stack.getLength(); + if(number_of_edge<3){ + return; + } + NyARSquare[] sa=(NyARSquare[])i_square_stack.getArray(); + for(int i=0;iMAX_COORD_NUM) { + // nが2以下、又はMAX_COORD_NUM以上なら主成分分析をしない。 + return false; + } + //主成分分析する。 + this._pca.pcaWithDistortionFactor(i_xcoord, i_ycoord, st, n,dist_factor, evec, ev,mean); + final NyARLinear l_line_i = o_line[i]; + l_line_i.run = evec.m01;// line[i][0] = evec->m[1]; + l_line_i.rise = -evec.m00;// line[i][1] = -evec->m[0]; + 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]); + } + for (int i = 0; i < 4; i++) { + final NyARLinear l_line_i = o_line[i]; + final NyARLinear l_line_2 = o_line[(i + 3) % 4]; + final double w1 = l_line_2.run * l_line_i.rise - l_line_i.run * l_line_2.rise; + if (w1 == 0.0) { + return false; + } + // 頂点インデクスから頂点座標を得て保存 + o_imvertex[i].x = i_xcoord[i_mkvertex[i]]; + o_imvertex[i].y = i_ycoord[i_mkvertex[i]]; + } + return true; + } + /** + * 辺からの対角線が最長になる点を対角線候補として返す。 + * + * @param i_xcoord + * @param i_ycoord + * @param i_coord_num + * @return + */ + private int scanVertex(int[] i_xcoord, int[] i_ycoord, int i_coord_num) + { + final int sx = i_xcoord[0]; + final int sy = i_ycoord[0]; + int d = 0; + int w, x, y; + int ret = 0; + for (int i = 1; i < i_coord_num; i++) { + x = i_xcoord[i] - sx; + y = i_ycoord[i] - sy; + w = x * x + y * y; + if (w > d) { + d = w; + ret = i; + } + // ここでうまく終了条件入れられないかな。 + } + return ret; + } + + private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter(); + private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter(); + + /** + * static int arDetectMarker2_check_square( int area, ARMarkerInfo2 *marker_info2, double factor ) 関数の代替関数 OPTIMIZED STEP [450->415] o_squareに頂点情報をセットします。 + * + * @param i_x_coord + * @param i_y_coord + * @param i_vertex1_index + * @param i_coord_num + * @param i_area + * @param o_vertex + * 要素数はint[4]である事 + * @return + */ + 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) + { + final NyARVertexCounter wv1 = this.__getSquareVertex_wv1; + final NyARVertexCounter wv2 = this.__getSquareVertex_wv2; + final int end_of_coord = i_vertex1_index + i_coord_num - 1; + final int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0]; + final int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0]; + int dmax = 0; + int v1 = i_vertex1_index; + for (int i = 1 + i_vertex1_index; i < end_of_coord; i++) {// for(i=1;icoord_num-1;i++) + // { + final int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy); + if (d > dmax) { + dmax = d; + v1 = i; + } + } + final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR; + + o_vertex[0] = i_vertex1_index; + + 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)< + // 0 ) { + return false; + } + 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) + // < 0) { + return false; + } + + int v2; + if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {// if(wvnum1 == 1 && wvnum2== 1) { + o_vertex[1] = wv1.vertex[0]; + o_vertex[2] = v1; + o_vertex[3] = wv2.vertex[0]; + } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {// }else if( wvnum1 > 1 && wvnum2== 0) { + // 頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。 + v2 = (v1 - i_vertex1_index) / 2 + i_vertex1_index; + if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh)) { + return false; + } + if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh)) { + return false; + } + if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) { + o_vertex[1] = wv1.vertex[0]; + o_vertex[2] = wv2.vertex[0]; + o_vertex[3] = v1; + } else { + return false; + } + } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) { + // v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index; + v2 = (v1 + end_of_coord) / 2; + + if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh)) { + return false; + } + if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh)) { + return false; + } + if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) { + o_vertex[1] = v1; + o_vertex[2] = wv1.vertex[0]; + o_vertex[3] = wv2.vertex[0]; + } else { + return false; + } + } else { + return false; + } + o_vertex[4] = end_of_coord; + return true; + } + /** + * QRコードのシンボル特徴を持つラベルであるかを調べる + * @param buf + * @param index_table + * @param i_label + * @return + */ + private boolean hasQrEdgeFeature(int buf[][], int[] index_table, NyARLabelingLabel i_label) + { + int tx, bx; + int w; + int i_label_id = i_label.id; + int[] limage_j; + final int clip_l = i_label.clip_l; + final int clip_b = i_label.clip_b; + final int clip_r = i_label.clip_r; + final int clip_t = i_label.clip_t; + + tx = bx = 0; + // 上接点(→) + limage_j = buf[clip_t]; + for (int i = clip_l; i <= clip_r; i++) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) { + w = limage_j[i]; + if (w > 0 && index_table[w - 1] == i_label_id) { + tx = i; + break; + } + } + // 下接点(←) + limage_j = buf[clip_b]; + for (int i = clip_r; i >= clip_l; i--) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) { + w = limage_j[i]; + if (w > 0 && index_table[w - 1] == i_label_id) { + bx = i; + break; + } + } + final int cx = (clip_l + clip_r) / 2; + final int cy = (clip_t + clip_b) / 2; + // 横断チェック(中心から線を引いて、010になるかしらべる) + if (!checkDiagonalLine(buf, cx, cy, bx, clip_b)) { + return false; + } + if (!checkDiagonalLine(buf, tx, clip_t, cx, cy)) { + return false; + } + return true; + } + + /** + * シンボルのパターン特徴を調べる関数 + * 対角線の一部が010になってるか調べる。 + * + * @param buf + * @param i_px1 + * @param i_py1 + * @param i_px2 + * @param i_py2 + * @return + */ + private boolean checkDiagonalLine(int[][] buf, int i_px1, int i_py1, int i_px2, int i_py2) + { + int sub_y = i_py2 - i_py1; + int sub_x = i_px2 - i_px1; + // 黒 + int i = 0; + for (; i < sub_y; i++) { + int yp = i_py1 + i; + int xp = i_px1 + i * sub_x / sub_y; + if (buf[yp][xp] == 0 && buf[yp][xp-1] == 0 && buf[yp][xp+1] == 0) { + break; + } + + } + if (i == sub_y) { + return false; + } + // 白 + for (; i < sub_y; i++) { + int yp = i_py1 + i; + int xp = i_px1 + i * sub_x / sub_y; + if (buf[yp][xp] != 0 && buf[yp][xp-1] != 0 && buf[yp][xp+1] != 0) { + break; + } + + } + if (i == sub_y) { + return false; + } + // 黒 + for (; i < sub_y; i++) { + int yp = i_py1 + i; + int xp = i_px1 + i * sub_x / sub_y; + if (buf[yp][xp] == 0 && buf[yp][xp-1] == 0 && buf[yp][xp+1] == 0) { + break; + } + + } + if (i != sub_y) { + return false; + } + // 端まで到達したらOK + return true; + } + +} diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeSymbolBinder.java b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeSymbolBinder.java new file mode 100644 index 0000000..6102210 --- /dev/null +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARQrCodeSymbolBinder.java @@ -0,0 +1,298 @@ +package jp.nyatla.nyartoolkit.toys.qrcode; + +import java.awt.Color; +import java.awt.Graphics; + +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.utils.j2se.*; +import jp.nyatla.nyartoolkit.core.param.*; +/** + * QRコードのシンボルを結びつける偉いクラス + * アルゴリズムはこんな感じ。 + * 1.3シンボルの位置関係から中間のシンボルを探す。 + * 2.中間シンボルの内角点を探す + * 3.残りの2シンボル間の最短距離の頂点セットを見つけて、それぞれの内角点を探す + * 4.3個の内角点が決まったら、各シンボルごとに外角点(反対側の頂点)を特定する。 + * 5.対角のシンボルの外角頂点から伸びる線分を合成して、矩形を決める。 + * 6.矩形が決まったら、方程式を解いて交点を出して、頂点にする。 + * 7.交点と中央のシンボルの位置関係から、正しい計算が行われたかを判定(まだ実装してない) + * + * + * この方法は浅い角度でシンボル集合を見たときに、1や3の手順が高い確率で失敗する。 + * その場合計算が途中で破綻するのでわかる(はず) + * 他の方法もあるけど、それはまた今度。 + */ +public class NyARQrCodeSymbolBinder +{ + private NyARCameraDistortionFactor _distfactor; + + public NyARQrCodeSymbolBinder(NyARCameraDistortionFactor i_ref_distortion) + { + this._distfactor=i_ref_distortion; + return; + } + /** + * 最小の三角形を構成する頂点セットを得る + * @param i_s0 + * @param i_s1 + * @param i_s2 + * @param o_vertex + */ + private static void getMinimumTriangleVertex(NyARSquare[] i_sqare,int[] o_vertex_id) + { + //辺の長さが最小になる頂点の組合せを探す + int d; + int x,y; + int dmax=0x7fffffff; + final NyARIntPoint[] vertex0=i_sqare[0].imvertex; + final NyARIntPoint[] vertex1=i_sqare[1].imvertex; + final NyARIntPoint[] vertex2=i_sqare[2].imvertex; + for(int i=0;i<4;i++) + { + for(int i2=0;i2<4;i2++) + { + for(int i3=0;i3<4;i3++){ + x=vertex0[i].x-vertex2[i3].x; + y=vertex0[i].y-vertex2[i3].y; + d=x*x+y*y; + x=vertex1[i2].x-vertex2[i3].x; + y=vertex1[i2].y-vertex2[i3].y; + d+=x*x+y*y; + x=vertex1[i2].x-vertex0[i].x; + y=vertex1[i2].y-vertex0[i].y; + d+=x*x+y*y; + if(d64){ + return -1; + } + //シンボルがどの象限にあるか確認する + x=i_vertex.x=i_center.x; + y=i_vertex.y=i_center.y; + int dir; + if(x<0){ + dir=2;//dir=y<0?1:2; + }else{ + dir=4;//dir=y<0?3:4; + } + return (dir+shift)%4; + } + + + +} \ No newline at end of file diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARRasterFilter_QrAreaAverage.java b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARRasterFilter_QrAreaAverage.java new file mode 100644 index 0000000..2202f70 --- /dev/null +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARRasterFilter_QrAreaAverage.java @@ -0,0 +1,89 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.toys.qrcode; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.INyARRasterFilter_GsToBin; +import jp.nyatla.nyartoolkit.core.types.*; + +/** + * 平均移動法を使った2値化フィルタ + * + */ +public class NyARRasterFilter_QrAreaAverage implements INyARRasterFilter_GsToBin +{ + private int _area = 8; + + public void doFilter(NyARGrayscaleRaster i_input, NyARBinRaster i_output) throws NyARException + { + final NyARIntSize size = i_output.getSize(); + final int[][] out_buf = (int[][]) i_output.getBufferReader().getBuffer(); + final int[][] in_buf = (int[][]) i_input.getBufferReader().getBuffer(); + assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); + assert (size.h % 8 == 0 && size.w % 8 == 0);//暫定実装なので。 + + final int area = this._area; + int y1 = area; + int x1 = area; + int y2 = size.h - area; + int x2 = size.w - area; + + for (int y = y1; y < y2; y++) { + int sum, nn; + sum = nn = 0; + for (int yy = y - area; yy < y + area + 1; yy++) { + for (int xx = x1 - area; xx < x1 + area; xx++) { + sum += in_buf[yy][xx]; + nn++; + } + } + int th; + boolean first = true; + th=0; + for (int x = area; x < x2; x++) { + if (!first) { + for (int yy = y - area; yy < y + area; yy++) { + sum += in_buf[yy][x + area]; + sum -= in_buf[yy][x - area]; + } + } + first = false; + th = (sum / nn); + int g = in_buf[y][x]; + out_buf[y][x] = th < g ? 1 : 0; + } + } + return; + } + +} diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARSingleQrDetector.java b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARSingleQrDetector.java new file mode 100644 index 0000000..2d7bf72 --- /dev/null +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/NyARSingleQrDetector.java @@ -0,0 +1,159 @@ +/* + * PROJECT: NyARToolkit + * -------------------------------------------------------------------------------- + * This work is based on the original ARToolKit developed by + * Hirokazu Kato + * Mark Billinghurst + * HITLab, University of Washington, Seattle + * http://www.hitl.washington.edu/artoolkit/ + * + * The NyARToolkit is Java version ARToolkit class library. + * Copyright (C)2008 R.Iizuka + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this framework; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * + * + */ +package jp.nyatla.nyartoolkit.toys.qrcode; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.match.*; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.pickup.*; +import jp.nyatla.nyartoolkit.core.raster.rgb.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; +import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.*; +import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*; +/** + * 画像からARCodeに最も一致するマーカーを1個検出し、その変換行列を計算するクラスです。 + * + */ +public class NyARSingleQrDetector +{ + private static final int AR_SQUARE_MAX = 100; + + private boolean _is_continue = false; + private INyARSquareDetector _square_detect; + + private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); + + protected INyARTransMat _transmat; + + private double _marker_width; + + // 検出結果の保存用 + private NyARSquare _detected_square; + + + /** + * 検出するARCodeとカメラパラメータから、1個のARCodeを検出するNyARSingleDetectMarkerインスタンスを作ります。 + * + * @param i_param + * カメラパラメータを指定します。 + * @param i_marker_width + * ARコードの物理サイズを、ミリメートルで指定します。 + * @throws NyARException + */ + public NyARSingleQrDetector(NyARParam i_param, double i_marker_width) throws NyARException + { + final NyARIntSize scr_size=i_param.getScreenSize(); + // 解析オブジェクトを作る + this._square_detect = new NyARQrCodeDetector(i_param.getDistortionFactor(),scr_size); + this._transmat = new NyARTransMat(i_param); + this._marker_width = i_marker_width; + //2値画像バッファを作る + this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h); + //中間のグレースケール画像のバッファを作る + this._gs_raster=new NyARGrayscaleRaster(scr_size.w,scr_size.h); + } + private NyARBinRaster _bin_raster; + private NyARGrayscaleRaster _gs_raster; + //画処理フィルター + private INyARRasterFilter_RgbToGs _rgb2gs_filter=new NyARRasterFilter_RgbAve(); + private INyARRasterFilter_GsToBin _gstobin_filter=new NyARRasterFilter_QrAreaAverage(); + + /** + * i_imageにマーカー検出処理を実行し、結果を記録します。 + * + * @param i_raster + * マーカーを検出するイメージを指定します。イメージサイズは、カメラパラメータ + * と一致していなければなりません。 + * @return マーカーが検出できたかを真偽値で返します。 + * @throws NyARException + */ + public boolean detectMarkerLite(INyARRgbRaster i_raster,int i_threshold) throws NyARException + { + //サイズチェック + if(!this._bin_raster.getSize().isEqualSize(i_raster.getSize())){ + throw new NyARException(); + } + //グレースケールに変換 + this._rgb2gs_filter.doFilter(i_raster, this._gs_raster); + //2値イメージに変換 + this._gstobin_filter.doFilter(this._gs_raster, this._bin_raster); + + this._detected_square = null; + NyARSquareStack l_square_list = this._square_list; + // スクエアコードを探す + this._square_detect.detectMarker(this._bin_raster, l_square_list); + //変換する + + + int number_of_square = l_square_list.getLength(); + // コードは見つかった? + if (number_of_square < 1) { + return false; + } + this._detected_square=(NyARSquare)l_square_list.getItem(0); + return true; + } + + /** + * 検出したマーカーの変換行列を計算して、o_resultへ値を返します。 + * 直前に実行したdetectMarkerLiteが成功していないと使えません。 + * + * @param o_result + * 変換行列を受け取るオブジェクトを指定します。 + * @throws NyARException + */ + public void getTransmationMatrix(NyARTransMatResult o_result) throws NyARException + { + // 一番一致したマーカーの位置とかその辺を計算 + if (this._is_continue) { + this._transmat.transMatContinue(this._detected_square,this._detected_square.direction,this._marker_width, o_result); + } else { + this._transmat.transMat(this._detected_square,this._detected_square.direction,this._marker_width, o_result); + } + return; + } + /** + * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。 + * + * @param i_is_continue + * TRUEなら、transMatCont互換の計算をします。 FALSEなら、transMat互換の計算をします。 + */ + public void setContinueMode(boolean i_is_continue) + { + this._is_continue = i_is_continue; + } +} diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrSample.java b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrSample.java new file mode 100644 index 0000000..8d488e2 --- /dev/null +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrSample.java @@ -0,0 +1,242 @@ +/* + * PROJECT: NyARToolkit JOGL sample program. + * -------------------------------------------------------------------------------- + * The MIT License + * Copyright (c) 2008 nyatla + * airmail(at)ebony.plala.or.jp + * http://nyatla.jp/nyartoolkit/ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jp.nyatla.nyartoolkit.toys.qrcode; + +import java.awt.event.*; +import java.awt.*; +import javax.media.Buffer; +import javax.media.opengl.*; +import com.sun.opengl.util.*; +import jp.nyatla.nyartoolkit.core.*; +import jp.nyatla.nyartoolkit.core.param.*; +import jp.nyatla.nyartoolkit.core.transmat.*; +import jp.nyatla.nyartoolkit.jmf.utils.*; +import jp.nyatla.nyartoolkit.jogl.utils.*; +/** + * simpleLiteと同じようなテストプログラム + * 出来る限りARToolKitのサンプルと似せて作ってあります。 + * 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。 + * + */ +public class SingleQrSample implements GLEventListener, JmfCaptureListener +{ + private final String CARCODE_FILE = "../../Data/patt.hiro"; + + private final String PARAM_FILE = "../../Data/camera_para.dat"; + + private final static int SCREEN_X = 320; + + private final static int SCREEN_Y = 240; + + private Animator _animator; + + private GLNyARRaster_RGB _cap_image; + + private JmfCameraCapture _capture; + + private GL _gl; + private NyARGLUtil _glnya; + + //NyARToolkit関係 + private NyARSingleQrDetector _nya; + private NyARParam _ar_param; + + private double[] _camera_projection=new double[16]; + + /** + * 立方体を書く + * + */ + void drawCube() + { + // Colour cube data. + int polyList = 0; + float fSize = 0.5f;//マーカーサイズに対して0.5倍なので、4cmの立方体 + int f, i; + float[][] cube_vertices = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, -1.0f }, { 1.0f, -1.0f, -1.0f }, { -1.0f, -1.0f, -1.0f }, { -1.0f, 1.0f, -1.0f } }; + float[][] cube_vertex_colors = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }; + int cube_num_faces = 6; + short[][] cube_faces = new short[][] { { 3, 2, 1, 0 }, { 2, 3, 7, 6 }, { 0, 1, 5, 4 }, { 3, 0, 4, 7 }, { 1, 2, 6, 5 }, { 4, 5, 6, 7 } }; + + if (polyList == 0) { + polyList = _gl.glGenLists(1); + _gl.glNewList(polyList, GL.GL_COMPILE); + _gl.glBegin(GL.GL_QUADS); + for (f = 0; f < cube_num_faces; f++) + for (i = 0; i < 4; i++) { + _gl.glColor3f(cube_vertex_colors[cube_faces[f][i]][0], cube_vertex_colors[cube_faces[f][i]][1], cube_vertex_colors[cube_faces[f][i]][2]); + _gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize); + } + _gl.glEnd(); + _gl.glColor3f(0.0f, 0.0f, 0.0f); + for (f = 0; f < cube_num_faces; f++) { + _gl.glBegin(GL.GL_LINE_LOOP); + for (i = 0; i < 4; i++) + _gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize); + _gl.glEnd(); + } + _gl.glEndList(); + } + + _gl.glPushMatrix(); // Save world coordinate system. + _gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface. + _gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis. + _gl.glDisable(GL.GL_LIGHTING); // Just use colours. + _gl.glCallList(polyList); // Draw the cube. + _gl.glPopMatrix(); // Restore world coordinate system. + + } + + public SingleQrSample() + { + Frame frame = new Frame("Java simpleLite with NyARToolkit"); + + // 3Dを描画するコンポーネント + GLCanvas canvas = new GLCanvas(); + frame.add(canvas); + canvas.addGLEventListener(this); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) + { + System.exit(0); + } + }); + + frame.setVisible(true); + Insets ins = frame.getInsets(); + frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom); + canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y); + } + + public void init(GLAutoDrawable drawable) + { + _gl = drawable.getGL(); + _gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + //NyARToolkitの準備 + try { + //キャプチャの準備 + _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB); + _capture.setCaptureListener(this); + //NyARToolkitの準備 + _ar_param = new NyARParam(); + NyARCode ar_code = new NyARCode(16, 16); + _ar_param.loadARParamFromFile(PARAM_FILE); + _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y); + _nya = new NyARSingleQrDetector(_ar_param,80); + _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。 + ar_code.loadARPattFromFile(CARCODE_FILE); + //NyARToolkit用の支援クラス + _glnya = new NyARGLUtil(_gl); + //GL対応のRGBラスタオブジェクト + _cap_image = new GLNyARRaster_RGB(_ar_param); + //キャプチャ開始 + _capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + //カメラパラメータの計算 + _glnya.toCameraFrustumRH(_ar_param,_camera_projection); + + _animator = new Animator(drawable); + _animator.start(); + return; + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) + { + _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + _gl.glViewport(0, 0, width, height); + + //視体積の設定 + _gl.glMatrixMode(GL.GL_PROJECTION); + _gl.glLoadIdentity(); + //見る位置 + _gl.glMatrixMode(GL.GL_MODELVIEW); + _gl.glLoadIdentity(); + } + private NyARTransMatResult __display_transmat_result=new NyARTransMatResult(); + private double[] __display_wk=new double[16]; + + public void display(GLAutoDrawable drawable) + { + NyARTransMatResult transmat_result=__display_transmat_result; + try { + if (!_cap_image.hasData()) { + return; + } + _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame. + //画像チェックしてマーカー探して、背景を書く + boolean is_marker_exist; + synchronized (_cap_image) { + is_marker_exist = _nya.detectMarkerLite(_cap_image, 0); + //背景を書く + _glnya.drawBackGround(_cap_image, 1.0); + } + //マーカーがあれば、立方体を描画 + if (is_marker_exist) { + //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。 + // Projection transformation. + _gl.glMatrixMode(GL.GL_PROJECTION); + _gl.glLoadMatrixd(_camera_projection, 0); + _gl.glMatrixMode(GL.GL_MODELVIEW); + // Viewing transformation. + _gl.glLoadIdentity(); + //変換行列を取得 + _nya.getTransmationMatrix(transmat_result); + //変換行列をOpenGL形式に変換 + _glnya.toCameraViewRH(transmat_result, __display_wk); + _gl.glLoadMatrixd(__display_wk, 0); + + // All other lighting and geometry goes here. + drawCube(); + } + Thread.sleep(1);//タスク実行権限を一旦渡す + } catch (Exception e) { + e.printStackTrace(); + } + + } + public void onUpdateBuffer(Buffer i_buffer) + { + try { + synchronized (_cap_image) { + _cap_image.setBuffer(i_buffer, true); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) + { + } + + public static void main(String[] args) + { + new SingleQrSample(); + } +} diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrTest.java b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrTest.java new file mode 100644 index 0000000..6b98d06 --- /dev/null +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/qrcode/SingleQrTest.java @@ -0,0 +1,189 @@ +/* このソースは実験用のソースです。 + * 動いたり動かなかったりします。 + * + */ +package jp.nyatla.nyartoolkit.toys.qrcode; + +import javax.media.*; + +import javax.media.util.BufferToImage; +import javax.media.format.*; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.jmf.utils.*; + +import jp.nyatla.nyartoolkit.core.*; + +import java.awt.*; + +import jp.nyatla.nyartoolkit.core.labeling.*; +import jp.nyatla.nyartoolkit.core.param.*; +import jp.nyatla.nyartoolkit.core.raster.*; +import jp.nyatla.nyartoolkit.core.rasterfilter.*; +import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*; +import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.*; +import jp.nyatla.utils.j2se.LabelingBufferdImage; +import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; +import jp.nyatla.nyartoolkit.core.types.*; + + + + +public class SingleQrTest extends Frame implements JmfCaptureListener +{ + private final String camera_file = "../../Data/camera_para.dat"; + + private JmfNyARRaster_RGB _raster; + + private JmfCameraCapture capture; + private NyARParam ap; + public SingleQrTest() throws NyARException, NyARException + { + setBounds(0, 0, 640 + 64, 720 + 64); + // キャプチャの準備 + capture = new JmfCameraCapture(320, 240, 30f, JmfCameraCapture.PIXEL_FORMAT_RGB); + capture.setCaptureListener(this); + + // キャプチャイメージ用のラスタを準備 + this._raster = new JmfNyARRaster_RGB(320, 240); + + // AR用カメラパラメタファイルをロード + ap = new NyARParam(); + ap.loadARParamFromFile(camera_file); + ap.changeScreenSize(320, 240); + + + } + + // そのラベルが特徴点候補か返す。 + + private NyARBinRaster _binraster1 = new NyARBinRaster(320, 240); + + private NyARGrayscaleRaster _gsraster1 = new NyARGrayscaleRaster(320, 240); + + + private LabelingBufferdImage _bimg = new LabelingBufferdImage(320, 240); + + + public void onUpdateBuffer(Buffer i_buffer) + { + NyARRasterFilter_QrAreaAverage gs2bin=new NyARRasterFilter_QrAreaAverage(); + + try { + // キャプチャしたバッファをラスタにセット + _raster.setBuffer(i_buffer); + + Graphics g = getGraphics(); + // キャプチャ画像 + BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat()); + Image img = b2i.createImage(i_buffer); + this.getGraphics().drawImage(img, 32, 32, this); + + // 画像1 + INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbAve(); +// INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbMul(); + + filter_rgb2gs.doFilter(_raster, _gsraster1); + this._bimg.drawImage(this._gsraster1); + this.getGraphics().drawImage(this._bimg, 32 + 320, 32, 320 + 320 + 32, 240 + 32, 0, 240, 320, 0, this); + + + // 画像2 + gs2bin.doFilter(_gsraster1, _binraster1); + this._bimg.drawImage(_binraster1); + this.getGraphics().drawImage(this._bimg, 32, 32 + 240, 320 + 32, 240 + 32 + 240, 0, 240, 320, 0, this); + + // 画像3 + NyARLabelingImage limage = new NyARLabelingImage(320, 240); + NyARLabeling_ARToolKit labeling = new NyARLabeling_ARToolKit(); + labeling.attachDestination(limage); + labeling.labeling(_binraster1); + this._bimg.drawImage(this._gsraster1); + + NyARSquareStack stack = new NyARSquareStack(100); + NyARQrCodeDetector detect = new NyARQrCodeDetector(ap.getDistortionFactor(), new NyARIntSize(320,240)); +// detect.bimg=this._bimg; + + detect.detectMarker(_binraster1, stack); + for (int i = 0; i < stack.getLength(); i++) { + NyARSquare[] square_ptr = (NyARSquare[]) stack.getArray(); + int d=square_ptr[i].direction; + int[] xp=new int[4]; + int[] yp=new int[4]; + for(int i2=0;i2<4;i2++){ + xp[i2]=square_ptr[i].imvertex[(i2+d)%4].x; + yp[i2]=square_ptr[i].imvertex[(i2+d)%4].y; + } + Graphics g2=this._bimg.getGraphics(); + g2.setColor(Color.RED); + g2.drawPolygon(xp, yp,3); + g2.setColor(Color.CYAN); + g2.drawRect(square_ptr[i].imvertex[d].x, square_ptr[i].imvertex[d].y,5,5); + } + this.getGraphics().drawImage(this._bimg, 32 + 320, 32 + 240, 320 + 32 + 320, 240 + 32 + 240, 0, 240, 320, 0, this); + + // 画像3 + // threshold.debugDrawHistgramMap(_workraster, _workraster2); + // this._bimg2.setImage(this._workraster2); + // this.getGraphics().drawImage(this._bimg2, 32+320, 32+240,320+32+320,240+32+240,0,240,320,0, this); + + // 画像4 + // NyARRasterThresholdAnalyzer_SlidePTile threshold=new NyARRasterThresholdAnalyzer_SlidePTile(15); + // threshold.analyzeRaster(_gsraster1); + // filter_gs2bin=new NyARRasterFilter_AreaAverage(); + // filter_gs2bin.doFilter(_gsraster1, _binraster1); + // this._bimg.drawImage(_binraster1); + + // NyARRasterDetector_QrCodeEdge detector=new NyARRasterDetector_QrCodeEdge(10000); + // detector.analyzeRaster(_binraster1); + + // this._bimg.overlayData(detector.geResult()); + + // this.getGraphics().drawImage(this._bimg, 32, 32+480,320+32,480+32+240,0,240,320,0, this); + // 画像5 + + /* + * threshold2.debugDrawHistgramMap(_workraster, _workraster2); this._bimg2.drawImage(this._workraster2); this.getGraphics().drawImage(this._bimg2, + * 32+320, 32+480,320+32+320,480+32+240,0,240,320,0, this); + */ + + // this.getGraphics().drawImage(this._bimg, 32, 32, this); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private INyARLabeling labelingFactory(int i_idx) + { + // switch(i_idx){ + // case 0:{NyARLabeling_ARToolKit l=new NyARLabeling_ARToolKit();l.setThresh(4);return l;} + // case 1:{return new NyLineLabeling();} + // } + return null; + + } + + private void startCapture() + { + try { + capture.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + try { + SingleQrTest mainwin = new SingleQrTest(); + mainwin.setVisible(true); + mainwin.startCapture(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/NyARQrCodeDetector.java b/sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/NyARVertexDetector.java similarity index 96% rename from sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/NyARQrCodeDetector.java rename to sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/NyARVertexDetector.java index 530ea0c..4d8ad23 100644 --- a/sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/NyARQrCodeDetector.java +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/vertexdetect/NyARVertexDetector.java @@ -37,12 +37,11 @@ import jp.nyatla.nyartoolkit.core.types.*; import jp.nyatla.nyartoolkit.core.param.*; import jp.nyatla.nyartoolkit.core.*; import jp.nyatla.nyartoolkit.toys.x2.*; - /** * 矩形座標をPCAではなく、頂点座標そのものからSquare位置を計算するクラス * */ -public class NyARQrCodeDetector implements INyARSquareDetector +public class NyARVertexDetector implements INyARSquareDetector { private static final double VERTEX_FACTOR = 1.0;// 線検出のファクタ @@ -64,7 +63,7 @@ public class NyARQrCodeDetector implements INyARSquareDetector * * @param i_param */ - public NyARQrCodeDetector(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException + public NyARVertexDetector(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException { this._width = i_size.w; this._height = i_size.h; diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/x2/GLNyARSingleDetectMarker_X2.java b/sample/toys/jp/nyatla/nyartoolkit/toys/x2/GLNyARSingleDetectMarker_X2.java index f0d5bb1..cfadf3a 100644 --- a/sample/toys/jp/nyatla/nyartoolkit/toys/x2/GLNyARSingleDetectMarker_X2.java +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/x2/GLNyARSingleDetectMarker_X2.java @@ -35,7 +35,6 @@ import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.NyARCode; import jp.nyatla.nyartoolkit.core.param.NyARParam; import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; -import jp.nyatla.nyartoolkit.nymodel.x2.*; /** diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/x2/JavaSimpleLite_X2.java b/sample/toys/jp/nyatla/nyartoolkit/toys/x2/JavaSimpleLite_X2.java index c11308a..8e5cc24 100644 --- a/sample/toys/jp/nyatla/nyartoolkit/toys/x2/JavaSimpleLite_X2.java +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/x2/JavaSimpleLite_X2.java @@ -73,7 +73,7 @@ public class JavaSimpleLite_X2 implements GLEventListener, JmfCaptureListener //NyARToolkit関係 private GLNyARSingleDetectMarker_X2 _nya; - private GLNyARParam _ar_param; + private NyARParam _ar_param; /** * 立方体を書く diff --git a/sample/toys/jp/nyatla/nyartoolkit/toys/x2/RawFileTest_X2.java b/sample/toys/jp/nyatla/nyartoolkit/toys/x2/RawFileTest_X2.java index bcad2e6..3d48cf9 100644 --- a/sample/toys/jp/nyatla/nyartoolkit/toys/x2/RawFileTest_X2.java +++ b/sample/toys/jp/nyatla/nyartoolkit/toys/x2/RawFileTest_X2.java @@ -38,7 +38,6 @@ import jp.nyatla.nyartoolkit.core.*; import jp.nyatla.nyartoolkit.core.param.NyARParam; import jp.nyatla.nyartoolkit.core.raster.rgb.*; import jp.nyatla.nyartoolkit.core.transmat.*; -import jp.nyatla.nyartoolkit.nymodel.x2.*; /** diff --git a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java b/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java deleted file mode 100644 index 9bf44e6..0000000 --- a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARDetectMarker.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * PROJECT: NyARToolkit JOGL utilities. - * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * - * The NyARToolkit is Java version ARToolkit class library. - * Copyright (C)2008 R.Iizuka - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this framework; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * - * - */ -package jp.nyatla.nyartoolkit.jogl.utils; - -import jp.nyatla.nyartoolkit.NyARException; -import jp.nyatla.nyartoolkit.core.NyARCode; -import jp.nyatla.nyartoolkit.core.param.NyARParam; -import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; -import jp.nyatla.nyartoolkit.detector.NyARDetectMarker; - -/** - * NyARSingleDetectMarkerにOpenGL向け関数を追加したもの - */ -public class GLNyARDetectMarker extends NyARDetectMarker -{ - private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); - - private double view_scale_factor = 0.025; - - public GLNyARDetectMarker(NyARParam i_param, NyARCode[] i_code, double[] i_marker_width, int i_number_of_code) throws NyARException - { - super(i_param, i_code, i_marker_width, i_number_of_code); - } - - public void setScaleFactor(double i_new_value) - { - view_scale_factor = i_new_value; - } - - /** - * @param i_index - * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 - * @param o_result - * 結果値を格納する配列を指定してください。double[16]以上が必要です。 - * @throws NyARException - */ - public void getCameraViewRH(int i_index, double[] o_result) throws NyARException - { - // 座標を計算 - this.getTransmationMatrix(i_index, this.trans_mat_result); - // 行列変換 - final NyARTransMatResult mat = this.trans_mat_result; - o_result[0 + 0 * 4] = mat.m00; - o_result[0 + 1 * 4] = mat.m01; - o_result[0 + 2 * 4] = mat.m02; - o_result[0 + 3 * 4] = mat.m03; - o_result[1 + 0 * 4] = -mat.m10; - o_result[1 + 1 * 4] = -mat.m11; - o_result[1 + 2 * 4] = -mat.m12; - o_result[1 + 3 * 4] = -mat.m13; - o_result[2 + 0 * 4] = -mat.m20; - o_result[2 + 1 * 4] = -mat.m21; - o_result[2 + 2 * 4] = -mat.m22; - o_result[2 + 3 * 4] = -mat.m23; - o_result[3 + 0 * 4] = 0.0; - o_result[3 + 1 * 4] = 0.0; - o_result[3 + 2 * 4] = 0.0; - o_result[3 + 3 * 4] = 1.0; - if (view_scale_factor != 0.0) { - o_result[12] *= view_scale_factor; - o_result[13] *= view_scale_factor; - o_result[14] *= view_scale_factor; - } - return; - } -} diff --git a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java b/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java deleted file mode 100644 index 416d1be..0000000 --- a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARParam.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * PROJECT: NyARToolkit JOGL utilities. - * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * - * The NyARToolkit is Java version ARToolkit class library. - * Copyright (C)2008 R.Iizuka - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this framework; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * - * - */ -package jp.nyatla.nyartoolkit.jogl.utils; - -import jp.nyatla.nyartoolkit.core.*; -import jp.nyatla.nyartoolkit.core.param.NyARParam; - -/** - * NyARParamにOpenGL向け関数を追加したもの - */ -public class GLNyARParam extends NyARParam -{ - private double view_distance_min = 0.1;//#define VIEW_DISTANCE_MIN 0.1 // Objects closer to the camera than this will not be displayed. - - private double view_distance_max = 100.0;//#define VIEW_DISTANCE_MAX 100.0 // Objects further away from the camera than this will not be displayed. - - private double[] m_projection = null; - - public void setViewDistanceMin(double i_new_value) - { - m_projection = null;//キャッシュ済変数初期化 - view_distance_min = i_new_value; - } - - public void setViewDistanceMax(double i_new_value) - { - m_projection = null;//キャッシュ済変数初期化 - view_distance_max = i_new_value; - } - - /** - * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16]) - * 関数の置き換え - * @param focalmin - * @param focalmax - * @return - */ - public double[] getCameraFrustumRH() - { - //既に値がキャッシュされていたらそれを使う - if (m_projection != null) { - return m_projection; - } - //無ければ計算 - m_projection = new double[16]; - NyARMat trans_mat = new NyARMat(3, 4); - NyARMat icpara_mat = new NyARMat(3, 4); - double[][] p = new double[3][3], q = new double[4][4]; - - int i, j; - - final int width = this._screen_size.w; - final int height = this._screen_size.h; - - this.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat); - - double[][] icpara = icpara_mat.getArray(); - double[][] trans = trans_mat.getArray(); - for (i = 0; i < 4; i++) { - icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i]; - } - - for (i = 0; i < 3; i++) { - for (j = 0; j < 3; j++) { - p[i][j] = icpara[i][j] / icpara[2][2]; - } - } - q[0][0] = (2.0 * p[0][0] / (width - 1)); - q[0][1] = (2.0 * p[0][1] / (width - 1)); - q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0); - q[0][3] = 0.0; - - q[1][0] = 0.0; - q[1][1] = -(2.0 * p[1][1] / (height - 1)); - q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0); - q[1][3] = 0.0; - - q[2][0] = 0.0; - q[2][1] = 0.0; - q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max); - q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max); - - q[3][0] = 0.0; - q[3][1] = 0.0; - q[3][2] = -1.0; - q[3][3] = 0.0; - - for (i = 0; i < 4; i++) { // Row. - // First 3 columns of the current row. - for (j = 0; j < 3; j++) { // Column. - m_projection[i + j * 4] = q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]; - } - // Fourth column of the current row. - m_projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3]; - } - return m_projection; - } -} diff --git a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java b/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java deleted file mode 100644 index c1d79c6..0000000 --- a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/GLNyARSingleDetectMarker.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * PROJECT: NyARToolkit JOGL utilities. - * -------------------------------------------------------------------------------- - * This work is based on the original ARToolKit developed by - * Hirokazu Kato - * Mark Billinghurst - * HITLab, University of Washington, Seattle - * http://www.hitl.washington.edu/artoolkit/ - * - * The NyARToolkit is Java version ARToolkit class library. - * Copyright (C)2008 R.Iizuka - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this framework; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * - * - */ -package jp.nyatla.nyartoolkit.jogl.utils; - -import jp.nyatla.nyartoolkit.NyARException; -import jp.nyatla.nyartoolkit.core.NyARCode; -import jp.nyatla.nyartoolkit.core.param.NyARParam; -import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; -import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker; - - -/** - * NyARSingleDetectMarkerにOpenGL向け関数を追加したもの - */ -public class GLNyARSingleDetectMarker extends NyARSingleDetectMarker -{ - private NyARTransMatResult trans_mat_result = new NyARTransMatResult(); - - private double view_scale_factor = 0.025;// #define VIEW_SCALEFACTOR 0.025 // 1.0 ARToolKit unit becomes 0.025 of my OpenGL units. - - public GLNyARSingleDetectMarker(NyARParam i_param, NyARCode i_code, double i_marker_width) throws NyARException - { - super(i_param, i_code, i_marker_width); - } - - public void setScaleFactor(double i_new_value) - { - view_scale_factor = i_new_value; - } - - // public static void arglCameraViewRH(const double para[3][4], GLdouble m_modelview[16], const double scale) - public double[] getCameraViewRH() throws NyARException - { - double[] result = new double[16]; - getCameraViewRH(result); - return result; - } - - /** - * - * @param o_result - * 結果値を格納する配列を指定してください。double[16]以上が必要です。 - * @throws NyARException - */ - public void getCameraViewRH(double[] o_result) throws NyARException - { - // 座標を計算 - this.getTransmationMatrix(this.trans_mat_result); - // 行列変換 - final NyARTransMatResult mat = this.trans_mat_result; - o_result[0 + 0 * 4] = mat.m00; // R1C1 - o_result[0 + 1 * 4] = mat.m01; // R1C2 - o_result[0 + 2 * 4] = mat.m02; - o_result[0 + 3 * 4] = mat.m03; - o_result[1 + 0 * 4] = -mat.m10; // R2 - o_result[1 + 1 * 4] = -mat.m11; - o_result[1 + 2 * 4] = -mat.m12; - o_result[1 + 3 * 4] = -mat.m13; - o_result[2 + 0 * 4] = -mat.m20; // R3 - o_result[2 + 1 * 4] = -mat.m21; - o_result[2 + 2 * 4] = -mat.m22; - o_result[2 + 3 * 4] = -mat.m23; - o_result[3 + 0 * 4] = 0.0; - o_result[3 + 1 * 4] = 0.0; - o_result[3 + 2 * 4] = 0.0; - o_result[3 + 3 * 4] = 1.0; - if (view_scale_factor != 0.0) { - o_result[12] *= view_scale_factor; - o_result[13] *= view_scale_factor; - o_result[14] *= view_scale_factor; - } - return; - } -} diff --git a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java b/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java index f26c2f9..eeeede4 100644 --- a/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java +++ b/src.utils/jogl/jp/nyatla/nyartoolkit/jogl/utils/NyARGLUtil.java @@ -37,6 +37,10 @@ import java.nio.IntBuffer; import javax.media.opengl.GL; import javax.media.opengl.glu.GLU; +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.NyARMat; +import jp.nyatla.nyartoolkit.core.param.NyARParam; +import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; import jp.nyatla.nyartoolkit.core.types.*; /** * NyARToolkit用のJOGL支援関数群 @@ -125,4 +129,121 @@ public class NyARGLUtil ByteBuffer buf = ByteBuffer.wrap(i_raster.getGLRgbArray()); gl_.glDrawPixels(rsize.w,rsize.h, i_raster.getGLPixelFlag(), GL.GL_UNSIGNED_BYTE, buf); } + + private double view_scale_factor = 0.025; + private double view_distance_min = 0.1;//#define VIEW_DISTANCE_MIN 0.1 // Objects closer to the camera than this will not be displayed. + private double view_distance_max = 100.0;//#define VIEW_DISTANCE_MAX 100.0 // Objects further away from the camera than this will not be displayed. + + public void setScaleFactor(double i_new_value) + { + this.view_scale_factor = i_new_value; + } + + public void setViewDistanceMin(double i_new_value) + { + this.view_distance_min = i_new_value; + } + + public void setViewDistanceMax(double i_new_value) + { + this.view_distance_max = i_new_value; + } + + /** + * void arglCameraFrustumRH(const ARParam *cparam, const double focalmin, const double focalmax, GLdouble m_projection[16]) + * 関数の置き換え + * NyARParamからOpenGLのProjectionを作成します。 + * @param i_arparam + * @param o_gl_projection + * double[16]を指定して下さい。 + */ + public void toCameraFrustumRH(NyARParam i_arparam,double[] o_gl_projection) + { + NyARMat trans_mat = new NyARMat(3, 4); + NyARMat icpara_mat = new NyARMat(3, 4); + double[][] p = new double[3][3], q = new double[4][4]; + int i, j; + + final NyARIntSize size=i_arparam.getScreenSize(); + final int width = size.w; + final int height = size.h; + + i_arparam.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat); + + double[][] icpara = icpara_mat.getArray(); + double[][] trans = trans_mat.getArray(); + for (i = 0; i < 4; i++) { + icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i]; + } + + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + p[i][j] = icpara[i][j] / icpara[2][2]; + } + } + q[0][0] = (2.0 * p[0][0] / (width - 1)); + q[0][1] = (2.0 * p[0][1] / (width - 1)); + q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0); + q[0][3] = 0.0; + + q[1][0] = 0.0; + q[1][1] = -(2.0 * p[1][1] / (height - 1)); + q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0); + q[1][3] = 0.0; + + q[2][0] = 0.0; + q[2][1] = 0.0; + q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max); + q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max); + + q[3][0] = 0.0; + q[3][1] = 0.0; + q[3][2] = -1.0; + q[3][3] = 0.0; + + for (i = 0; i < 4; i++) { // Row. + // First 3 columns of the current row. + for (j = 0; j < 3; j++) { // Column. + o_gl_projection[i + j * 4] = q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]; + } + // Fourth column of the current row. + o_gl_projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3]; + } + return; + } + + + + /** + * NyARTransMatResultをOpenGLの行列へ変換します。 + * @param i_ny_result + * @param o_gl_result + * @throws NyARException + */ + public void toCameraViewRH(NyARTransMatResult i_ny_result, double[] o_gl_result) throws NyARException + { + o_gl_result[0 + 0 * 4] = i_ny_result.m00; + o_gl_result[0 + 1 * 4] = i_ny_result.m01; + o_gl_result[0 + 2 * 4] = i_ny_result.m02; + o_gl_result[0 + 3 * 4] = i_ny_result.m03; + o_gl_result[1 + 0 * 4] = -i_ny_result.m10; + o_gl_result[1 + 1 * 4] = -i_ny_result.m11; + o_gl_result[1 + 2 * 4] = -i_ny_result.m12; + o_gl_result[1 + 3 * 4] = -i_ny_result.m13; + o_gl_result[2 + 0 * 4] = -i_ny_result.m20; + o_gl_result[2 + 1 * 4] = -i_ny_result.m21; + o_gl_result[2 + 2 * 4] = -i_ny_result.m22; + o_gl_result[2 + 3 * 4] = -i_ny_result.m23; + o_gl_result[3 + 0 * 4] = 0.0; + o_gl_result[3 + 1 * 4] = 0.0; + o_gl_result[3 + 2 * 4] = 0.0; + o_gl_result[3 + 3 * 4] = 1.0; + if (view_scale_factor != 0.0) { + o_gl_result[12] *= view_scale_factor; + o_gl_result[13] *= view_scale_factor; + o_gl_result[14] *= view_scale_factor; + } + return; + } + } diff --git a/src/jp/nyatla/nyartoolkit/core/NyARSquare.java b/src/jp/nyatla/nyartoolkit/core/NyARSquare.java index 78f18e9..b8a2b5b 100644 --- a/src/jp/nyatla/nyartoolkit/core/NyARSquare.java +++ b/src/jp/nyatla/nyartoolkit/core/NyARSquare.java @@ -35,14 +35,20 @@ import jp.nyatla.nyartoolkit.core.types.*; /** * ARMarkerInfoに相当するクラス。 矩形情報を保持します。 * + * directionは方角を表します。 + * 決定しないときはDIRECTION_UNKNOWNを設定してください。 + * */ public class NyARSquare { + public final static int DIRECTION_UNKNOWN=-1; + public int direction; public NyARLinear[] line = new NyARLinear[4]; public NyARDoublePoint2d[] sqvertex = new NyARDoublePoint2d[4]; public NyARIntPoint[] imvertex = new NyARIntPoint[4]; public NyARSquare() { + this.direction=DIRECTION_UNKNOWN; for(int i=0;i<4;i++){ this.sqvertex[i]=new NyARDoublePoint2d(); this.imvertex[i]=new NyARIntPoint(); diff --git a/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java b/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java index 1a385a3..ac051ab 100644 --- a/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java +++ b/src/jp/nyatla/nyartoolkit/core/NyARSquareDetector.java @@ -104,9 +104,9 @@ public class NyARSquareDetector implements INyARSquareDetector private final int[] __detectMarker_mkvertex = new int[5]; /** - * 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 ) 関数の代替品 ラベリング情報からマーカー一覧を作成してo_marker_listを更新します。 関数はo_marker_listに重なりを除外したマーカーリストを作成します。 - * + * arDetectMarker2を基にした関数 + * この関数はNyARSquare要素のうち、directionを除くパラメータを取得して返します。 + * directionの確定は行いません。 * @param i_raster * 解析する2値ラスタイメージを指定します。 * @param o_square_stack @@ -394,7 +394,8 @@ public class NyARSquareDetector implements INyARSquareDetector /** - * ラベル同士の重なり(内包関係)を調べるクラスです。 ラベルリストに内包するラベルを蓄積し、それにターゲットのラベルが内包されているか を確認します。 + * ラベル同士の重なり(内包関係)を調べるクラスです。 + * ラベルリストに内包するラベルを蓄積し、それにターゲットのラベルが内包されているか を確認します。 */ class OverlapChecker { diff --git a/src/jp/nyatla/nyartoolkit/core/raster/NyARGlayscaleRaster.java b/src/jp/nyatla/nyartoolkit/core/raster/NyARGrayscaleRaster.java similarity index 91% rename from src/jp/nyatla/nyartoolkit/core/raster/NyARGlayscaleRaster.java rename to src/jp/nyatla/nyartoolkit/core/raster/NyARGrayscaleRaster.java index b828df1..314c9c3 100644 --- a/src/jp/nyatla/nyartoolkit/core/raster/NyARGlayscaleRaster.java +++ b/src/jp/nyatla/nyartoolkit/core/raster/NyARGrayscaleRaster.java @@ -35,13 +35,13 @@ import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader; import jp.nyatla.nyartoolkit.core.rasterreader.NyARBufferReader; import jp.nyatla.nyartoolkit.core.types.*; -public final class NyARGlayscaleRaster extends NyARRaster_BasicClass +public final class NyARGrayscaleRaster extends NyARRaster_BasicClass { protected int[][] _ref_buf; private INyARBufferReader _buffer_reader; - public NyARGlayscaleRaster(int i_width, int i_height) + public NyARGrayscaleRaster(int i_width, int i_height) { super(new NyARIntSize(i_width,i_height)); this._ref_buf = new int[i_height][i_width]; diff --git a/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java b/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java index 29f4920..ba16833 100644 --- a/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java +++ b/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_GsToBin.java @@ -36,5 +36,5 @@ import jp.nyatla.nyartoolkit.core.raster.*; public interface INyARRasterFilter_GsToBin { - public void doFilter(NyARGlayscaleRaster i_input, NyARBinRaster i_output) throws NyARException; + public void doFilter(NyARGrayscaleRaster i_input, NyARBinRaster i_output) throws NyARException; } diff --git a/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java b/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java index 0fdf48c..c818c2d 100644 --- a/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java +++ b/src/jp/nyatla/nyartoolkit/core/rasterfilter/INyARRasterFilter_RgbToGs.java @@ -41,5 +41,5 @@ import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; */ public interface INyARRasterFilter_RgbToGs { - public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException; + public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException; } diff --git a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java index 1a83b0c..58f73d8 100644 --- a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java +++ b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_AreaAverage.java @@ -44,7 +44,7 @@ public class NyARRasterFilter_AreaAverage implements INyARRasterFilter_GsToBin { private int _area = 8; - public void doFilter(NyARGlayscaleRaster i_input, NyARBinRaster i_output) throws NyARException + public void doFilter(NyARGrayscaleRaster i_input, NyARBinRaster i_output) throws NyARException { final NyARIntSize size = i_output.getSize(); final int[][] out_buf = (int[][]) i_output.getBufferReader().getBuffer(); diff --git a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java index 0ba8de8..f4f3615 100644 --- a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java +++ b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/gs2bin/NyARRasterFilter_Threshold.java @@ -49,7 +49,7 @@ public class NyARRasterFilter_Threshold implements INyARRasterFilter_GsToBin this._threshold = i_threshold; } - public void doFilter(NyARGlayscaleRaster i_input, NyARBinRaster i_output) throws NyARException + public void doFilter(NyARGrayscaleRaster i_input, NyARBinRaster i_output) throws NyARException { assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); diff --git a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java index c9da6f5..f349e12 100644 --- a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java +++ b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbAve.java @@ -40,7 +40,7 @@ import jp.nyatla.nyartoolkit.core.types.NyARIntSize; public class NyARRasterFilter_RgbAve implements INyARRasterFilter_RgbToGs { - public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException + public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException { INyARBufferReader in_buffer_reader=i_input.getBufferReader(); INyARBufferReader out_buffer_reader=i_output.getBufferReader(); diff --git a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java index d0e21e2..5f11bc0 100644 --- a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java +++ b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbMul.java @@ -40,14 +40,14 @@ import jp.nyatla.nyartoolkit.core.types.NyARIntSize; public class NyARRasterFilter_RgbMul implements INyARRasterFilter_RgbToGs { - public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException + public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException { INyARBufferReader in_buffer_reader=i_input.getBufferReader(); INyARBufferReader out_buffer_reader=i_output.getBufferReader(); assert (i_input.getSize().isEqualSize(i_output.getSize()) == true); - int[][] out_buf = (int[][]) in_buffer_reader.getBuffer(); - byte[] in_buf = (byte[]) out_buffer_reader.getBuffer(); + int[][] out_buf = (int[][]) out_buffer_reader.getBuffer(); + byte[] in_buf = (byte[]) in_buffer_reader.getBuffer(); NyARIntSize size = i_output.getSize(); switch (in_buffer_reader.getBufferType()) { diff --git a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java index a2b6405..742133b 100644 --- a/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java +++ b/src/jp/nyatla/nyartoolkit/core2/rasterfilter/rgb2gs/NyARRasterFilter_RgbOr.java @@ -40,7 +40,7 @@ import jp.nyatla.nyartoolkit.core.types.NyARIntSize; public class NyARRasterFilter_RgbOr implements INyARRasterFilter_RgbToGs { - public void doFilter(INyARRgbRaster i_input, NyARGlayscaleRaster i_output) throws NyARException + public void doFilter(INyARRgbRaster i_input, NyARGrayscaleRaster i_output) throws NyARException { INyARBufferReader in_buffer_reader=i_input.getBufferReader(); INyARBufferReader out_buffer_reader=i_output.getBufferReader(); diff --git a/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java b/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java index 6306ae0..ebc4988 100644 --- a/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java +++ b/src/jp/nyatla/utils/j2se/LabelingBufferdImage.java @@ -120,7 +120,7 @@ public class LabelingBufferdImage extends BufferedImage - public void drawImage(NyARGlayscaleRaster i_raster) throws NyARException + public void drawImage(NyARGrayscaleRaster i_raster) throws NyARException { assert (i_raster.getBufferReader().getBufferType() == INyARBufferReader.BUFFERFORMAT_INT2D_GLAY_8); diff --git a/test/jp/nyatla/nyartoolkit/dev/CopyOfLabelingCamera.java b/test/jp/nyatla/nyartoolkit/dev/CopyOfLabelingCamera.java deleted file mode 100644 index 2f7d9e8..0000000 --- a/test/jp/nyatla/nyartoolkit/dev/CopyOfLabelingCamera.java +++ /dev/null @@ -1,920 +0,0 @@ -/* このソースは実験用のソースです。 - * 動いたり動かなかったりします。 - * - */ -package jp.nyatla.nyartoolkit.dev; - -import javax.media.*; - -import javax.media.util.BufferToImage; -import javax.media.format.*; - -import jp.nyatla.nyartoolkit.NyARException; -import jp.nyatla.nyartoolkit.jmf.utils.*; -import jp.nyatla.nyartoolkit.core.*; -import jp.nyatla.nyartoolkit.toys.x2.NyARLabeling_ARToolKit_X2; -import jp.nyatla.nyartoolkit.toys.x2.NyARTransMat_X2; - -import java.awt.*; - -import jp.nyatla.nyartoolkit.core.INyARSquareDetector; -import jp.nyatla.nyartoolkit.core.labeling.*; -import jp.nyatla.nyartoolkit.core.match.*; -import jp.nyatla.nyartoolkit.core.param.*; -import jp.nyatla.nyartoolkit.core.pickup.*; -import jp.nyatla.nyartoolkit.core.raster.*; -import jp.nyatla.nyartoolkit.core.raster.rgb.INyARRgbRaster; -import jp.nyatla.nyartoolkit.core.rasterfilter.*; -import jp.nyatla.nyartoolkit.core2.rasteranalyzer.*; -import jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold.*; -import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*; -import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.NyARRasterFilter_RgbAve; -import jp.nyatla.utils.NyObjectStack; -import jp.nyatla.utils.j2se.LabelingBufferdImage; -import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; -import jp.nyatla.nyartoolkit.core.transmat.*; -import jp.nyatla.nyartoolkit.core.types.*; - - -class NyARMkVertexStack extends NyObjectStack -{ - public NyARMkVertexStack(int i_length) - { - super(new NyARIntPoint[i_length][]); - - } - - protected void onReservRequest(int i_start, int i_end, Object[] i_buffer) - { - for (int i = i_start; i < i_end; i++) { - i_buffer[i] = NyARIntPoint.createArray(4); - } - } -} -/** - * QRコードのシンボルを結びつける偉いクラス - * - */ -class NyQrCodeSymbolBinder -{ - LabelingBufferdImage bimg; - - NyARIntPoint[][] _sqare; - /** - * 最小の三角形を構成する頂点セットを得る - * @param i_s0 - * @param i_s1 - * @param i_s2 - * @param o_vertex - */ - public static void getMinimumTriangleVertex(NyARIntPoint[][] i_sqare,int[] o_vertex_id) - { - //辺の長さが最小になる頂点の組合せを探す - int d; - int x,y; - int dmax=0x7fffffff; - for(int i=0;i<4;i++) - { - for(int i2=0;i2<4;i2++) - { - for(int i3=0;i3<4;i3++){ - x=i_sqare[0][i].x-i_sqare[2][i3].x; - y=i_sqare[0][i].y-i_sqare[2][i3].y; - d=x*x+y*y; - x=i_sqare[1][i2].x-i_sqare[2][i3].x; - y=i_sqare[1][i2].y-i_sqare[2][i3].y; - d+=x*x+y*y; - x=i_sqare[1][i2].x-i_sqare[0][i].x; - y=i_sqare[1][i2].y-i_sqare[0][i].y; - d+=x*x+y*y; - if(d d) { - d = w; - ret = i; - } - // ここでうまく終了条件入れられないかな。 - } - return ret; - } - - private final NyARVertexCounter __getSquareVertex_wv1 = new NyARVertexCounter(); - - private final NyARVertexCounter __getSquareVertex_wv2 = new NyARVertexCounter(); - - /** - * static int arDetectMarker2_check_square( int area, ARMarkerInfo2 *marker_info2, double factor ) 関数の代替関数 OPTIMIZED STEP [450->415] o_squareに頂点情報をセットします。 - * - * @param i_x_coord - * @param i_y_coord - * @param i_vertex1_index - * @param i_coord_num - * @param i_area - * @param o_vertex - * 要素数はint[4]である事 - * @return - */ - 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) - { - final NyARVertexCounter wv1 = this.__getSquareVertex_wv1; - final NyARVertexCounter wv2 = this.__getSquareVertex_wv2; - final int end_of_coord = i_vertex1_index + i_coord_num - 1; - final int sx = i_x_coord[i_vertex1_index];// sx = marker_info2->x_coord[0]; - final int sy = i_y_coord[i_vertex1_index];// sy = marker_info2->y_coord[0]; - int dmax = 0; - int v1 = i_vertex1_index; - for (int i = 1 + i_vertex1_index; i < end_of_coord; i++) {// for(i=1;icoord_num-1;i++) - // { - final int d = (i_x_coord[i] - sx) * (i_x_coord[i] - sx) + (i_y_coord[i] - sy) * (i_y_coord[i] - sy); - if (d > dmax) { - dmax = d; - v1 = i; - } - } - final double thresh = (i_area / 0.75) * 0.01 * VERTEX_FACTOR; - - o_vertex[0] = i_vertex1_index; - - 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)< - // 0 ) { - return false; - } - 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) - // < 0) { - return false; - } - - int v2; - if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) {// if(wvnum1 == 1 && wvnum2== 1) { - o_vertex[1] = wv1.vertex[0]; - o_vertex[2] = v1; - o_vertex[3] = wv2.vertex[0]; - } else if (wv1.number_of_vertex > 1 && wv2.number_of_vertex == 0) {// }else if( wvnum1 > 1 && wvnum2== 0) { - // 頂点位置を、起点から対角点の間の1/2にあると予想して、検索する。 - v2 = (v1 - i_vertex1_index) / 2 + i_vertex1_index; - if (!wv1.getVertex(i_x_coord, i_y_coord, i_vertex1_index, v2, thresh)) { - return false; - } - if (!wv2.getVertex(i_x_coord, i_y_coord, v2, v1, thresh)) { - return false; - } - if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) { - o_vertex[1] = wv1.vertex[0]; - o_vertex[2] = wv2.vertex[0]; - o_vertex[3] = v1; - } else { - return false; - } - } else if (wv1.number_of_vertex == 0 && wv2.number_of_vertex > 1) { - // v2 = (v1-i_vertex1_index+ end_of_coord-i_vertex1_index) / 2+i_vertex1_index; - v2 = (v1 + end_of_coord) / 2; - - if (!wv1.getVertex(i_x_coord, i_y_coord, v1, v2, thresh)) { - return false; - } - if (!wv2.getVertex(i_x_coord, i_y_coord, v2, end_of_coord, thresh)) { - return false; - } - if (wv1.number_of_vertex == 1 && wv2.number_of_vertex == 1) { - o_vertex[1] = v1; - o_vertex[2] = wv1.vertex[0]; - o_vertex[3] = wv2.vertex[0]; - } else { - return false; - } - } else { - return false; - } - o_vertex[4] = end_of_coord; - return true; - } - /** - * QRコードのエッジ特徴を持つラベルであるかを調べる - * @param buf - * @param index_table - * @param i_label - * @return - */ - private boolean hasQrEdgeFeature(int buf[][], int[] index_table, NyARLabelingLabel i_label) - { - int tx, bx; - int w; - int i_label_id = i_label.id; - int[] limage_j; - final int clip_l = i_label.clip_l; - final int clip_b = i_label.clip_b; - final int clip_r = i_label.clip_r; - final int clip_t = i_label.clip_t; - - tx = bx = 0; - // 上接点(→) - limage_j = buf[clip_t]; - for (int i = clip_l; i <= clip_r; i++) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) { - w = limage_j[i]; - if (w > 0 && index_table[w - 1] == i_label_id) { - tx = i; - break; - } - } - // 下接点(←) - limage_j = buf[clip_b]; - for (int i = clip_r; i >= clip_l; i--) {// for( i = clip[0]; i <=clip[1]; i++, p1++ ) { - w = limage_j[i]; - if (w > 0 && index_table[w - 1] == i_label_id) { - bx = i; - break; - } - } - final int cx = (clip_l + clip_r) / 2; - final int cy = (clip_t + clip_b) / 2; - // 横断チェック(中心から線を引いて、101になるかしらべる) - if (!checkDiagonalLine(buf, cx, cy, bx, clip_b)) { - return false; - } - if (!checkDiagonalLine(buf, tx, clip_t, cx, cy)) { - return false; - } - return true; - } - - /** - * 対角線のパターンを調べる。 - * - * @param buf - * @param i_px1 - * @param i_py1 - * @param i_px2 - * @param i_py2 - * @return - */ - private boolean checkDiagonalLine(int[][] buf, int i_px1, int i_py1, int i_px2, int i_py2) - { - int sub_y = i_py2 - i_py1; - int sub_x = i_px2 - i_px1; - // 黒 - int i = 0; - for (; i < sub_y; i++) { - int yp = i_py1 + i; - int xp = i_px1 + i * sub_x / sub_y; - if (buf[yp][xp] == 0 && buf[yp][xp-1] == 0 && buf[yp][xp+1] == 0) { - break; - } - - } - if (i == sub_y) { - return false; - } - // 白 - for (; i < sub_y; i++) { - int yp = i_py1 + i; - int xp = i_px1 + i * sub_x / sub_y; - if (buf[yp][xp] != 0 && buf[yp][xp-1] != 0 && buf[yp][xp+1] != 0) { - break; - } - - } - if (i == sub_y) { - return false; - } - // 黒 - for (; i < sub_y; i++) { - int yp = i_py1 + i; - int xp = i_px1 + i * sub_x / sub_y; - if (buf[yp][xp] == 0 && buf[yp][xp-1] == 0 && buf[yp][xp+1] == 0) { - break; - } - - } - if (i != sub_y) { - return false; - } - // 端まで到達したらOK - return true; - } - -} - - -public class CopyOfLabelingCamera extends Frame implements JmfCaptureListener -{ - private final String camera_file = "../Data/camera_para.dat"; - - private JmfNyARRaster_RGB _raster; - - private JmfCameraCapture capture; - private NyARParam ap; - public CopyOfLabelingCamera() throws NyARException, NyARException - { - setBounds(0, 0, 640 + 64, 720 + 64); - // キャプチャの準備 - capture = new JmfCameraCapture(320, 240, 30f, JmfCameraCapture.PIXEL_FORMAT_RGB); - capture.setCaptureListener(this); - - // キャプチャイメージ用のラスタを準備 - this._raster = new JmfNyARRaster_RGB(320, 240); - - // AR用カメラパラメタファイルをロード - ap = new NyARParam(); - ap.loadARParamFromFile(camera_file); - ap.changeScreenSize(320, 240); - - - } - - // そのラベルが特徴点候補か返す。 - - private NyARBinRaster _binraster1 = new NyARBinRaster(320, 240); - - private NyARGlayscaleRaster _gsraster1 = new NyARGlayscaleRaster(320, 240); - - private NyARLabelingImage _limage = new NyARLabelingImage(320, 240); - - private LabelingBufferdImage _bimg = new LabelingBufferdImage(320, 240); - - private NyARRasterFilter_ARToolkitThreshold filter_gs2bin; - - public void onUpdateBuffer(Buffer i_buffer) - { - NyARRasterFilter_AreaAverage gs2bin=new NyARRasterFilter_AreaAverage(); - - try { - // キャプチャしたバッファをラスタにセット - _raster.setBuffer(i_buffer); - - Graphics g = getGraphics(); - // キャプチャ画像 - BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat()); - Image img = b2i.createImage(i_buffer); - this.getGraphics().drawImage(img, 32, 32, this); - - // 画像1 - INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbAve(); - filter_rgb2gs.doFilter(_raster, _gsraster1); - this._bimg.drawImage(this._gsraster1); - this.getGraphics().drawImage(this._bimg, 32 + 320, 32, 320 + 320 + 32, 240 + 32, 0, 240, 320, 0, this); - - - // 画像2 - gs2bin.doFilter(_gsraster1, _binraster1); - this._bimg.drawImage(_binraster1); - this.getGraphics().drawImage(this._bimg, 32, 32 + 240, 320 + 32, 240 + 32 + 240, 0, 240, 320, 0, this); - - // 画像3 - NyARLabelingImage limage = new NyARLabelingImage(320, 240); - NyARLabeling_ARToolKit labeling = new NyARLabeling_ARToolKit(); - labeling.attachDestination(limage); - labeling.labeling(_binraster1); - this._bimg.drawImage(this._gsraster1); - NyARLabelingLabel[] labels = (NyARLabelingLabel[]) limage.getLabelStack().getArray(); - - NyARSquareStack stack = new NyARSquareStack(100); - NyARQRCodeDetector detect = new NyARQRCodeDetector(ap.getDistortionFactor(), new NyARIntSize(320,240)); - detect.bimg=this._bimg; - - detect.detectMarker(_binraster1, stack); - for (int i = 0; i < stack.getLength(); i++) { - NyARSquare[] square_ptr = (NyARSquare[]) stack.getArray(); - int[] xp=new int[4]; - int[] yp=new int[4]; - for(int i2=0;i2<4;i2++){ - xp[i2]=square_ptr[i].imvertex[i2].x; - yp[i2]=square_ptr[i].imvertex[i2].y; - } - this._bimg.getGraphics().setColor(Color.RED); - this._bimg.getGraphics().drawPolygon(xp, yp,2); - } - this.getGraphics().drawImage(this._bimg, 32 + 320, 32 + 240, 320 + 32 + 320, 240 + 32 + 240, 0, 240, 320, 0, this); - - // 画像3 - // threshold.debugDrawHistgramMap(_workraster, _workraster2); - // this._bimg2.setImage(this._workraster2); - // this.getGraphics().drawImage(this._bimg2, 32+320, 32+240,320+32+320,240+32+240,0,240,320,0, this); - - // 画像4 - // NyARRasterThresholdAnalyzer_SlidePTile threshold=new NyARRasterThresholdAnalyzer_SlidePTile(15); - // threshold.analyzeRaster(_gsraster1); - // filter_gs2bin=new NyARRasterFilter_AreaAverage(); - // filter_gs2bin.doFilter(_gsraster1, _binraster1); - // this._bimg.drawImage(_binraster1); - - // NyARRasterDetector_QrCodeEdge detector=new NyARRasterDetector_QrCodeEdge(10000); - // detector.analyzeRaster(_binraster1); - - // this._bimg.overlayData(detector.geResult()); - - // this.getGraphics().drawImage(this._bimg, 32, 32+480,320+32,480+32+240,0,240,320,0, this); - // 画像5 - - /* - * threshold2.debugDrawHistgramMap(_workraster, _workraster2); this._bimg2.drawImage(this._workraster2); this.getGraphics().drawImage(this._bimg2, - * 32+320, 32+480,320+32+320,480+32+240,0,240,320,0, this); - */ - - // this.getGraphics().drawImage(this._bimg, 32, 32, this); - - } catch (Exception e) { - e.printStackTrace(); - } - - } - - private INyARLabeling labelingFactory(int i_idx) - { - // switch(i_idx){ - // case 0:{NyARLabeling_ARToolKit l=new NyARLabeling_ARToolKit();l.setThresh(4);return l;} - // case 1:{return new NyLineLabeling();} - // } - return null; - - } - - private void startCapture() - { - try { - capture.start(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void main(String[] args) - { - try { - CopyOfLabelingCamera mainwin = new CopyOfLabelingCamera(); - mainwin.setVisible(true); - mainwin.startCapture(); - } catch (Exception e) { - e.printStackTrace(); - } - - } - -} diff --git a/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java b/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java index 1946a5d..785ba68 100644 --- a/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java +++ b/test/jp/nyatla/nyartoolkit/dev/LabelingCamera.java @@ -11,9 +11,8 @@ import javax.media.format.*; import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.jmf.utils.*; -import jp.nyatla.nyartoolkit.nymodel.x2.NyARLabeling_ARToolKit_X2; + import jp.nyatla.nyartoolkit.core.*; -import jp.nyatla.nyartoolkit.nymodel.x2.NyARTransMat_X2; import java.awt.*; @@ -39,251 +38,236 @@ import jp.nyatla.nyartoolkit.core.types.*; import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22; -class NyARMkVertexStack extends NyObjectStack -{ - public NyARMkVertexStack(int i_length) - { - super(new NyARIntPoint[i_length][]); - - } - - protected void onReservRequest(int i_start, int i_end, Object[] i_buffer) - { - for (int i = i_start; i < i_end; i++) { - i_buffer[i] = NyARIntPoint.createArray(4); - } - } -} -/** - * QRコードのシンボルを結びつける偉いクラス - * - */ -class NyQrCodeSymbolBinder -{ - LabelingBufferdImage bimg; - - NyARIntPoint[][] _sqare; - /** - * 最小の三角形を構成する頂点セットを得る - * @param i_s0 - * @param i_s1 - * @param i_s2 - * @param o_vertex - */ - public static void getMinimumTriangleVertex(NyARSquare[] i_sqare,int[] o_vertex_id) - { - //辺の長さが最小になる頂点の組合せを探す - int d; - int x,y; - int dmax=0x7fffffff; - final NyARIntPoint[] vertex0=i_sqare[0].imvertex; - final NyARIntPoint[] vertex1=i_sqare[1].imvertex; - final NyARIntPoint[] vertex2=i_sqare[2].imvertex; - for(int i=0;i<4;i++) - { - for(int i2=0;i2<4;i2++) - { - for(int i3=0;i3<4;i3++){ - x=vertex0[i].x-vertex2[i3].x; - y=vertex0[i].y-vertex2[i3].y; - d=x*x+y*y; - x=vertex1[i2].x-vertex2[i3].x; - y=vertex1[i2].y-vertex2[i3].y; - d+=x*x+y*y; - x=vertex1[i2].x-vertex0[i].x; - y=vertex1[i2].y-vertex0[i].y; - d+=x*x+y*y; - if(d