From 4d016e434b245427b43a0e6a405b706fde932783 Mon Sep 17 00:00:00 2001 From: nyatla Date: Thu, 3 Dec 2009 05:36:03 +0000 Subject: [PATCH] =?utf8?q?[backup]NyARToolkit=20for=20Java=202.4.1?= =?utf8?q?=E5=80=99=E8=A3=9C=201.NyARSquare=E3=82=AF=E3=83=A9=E3=82=B9?= =?utf8?q?=E3=81=AE=E4=BB=95=E6=A7=98=E5=A4=89=E6=9B=B4=E3=80=82=E3=81=93?= =?utf8?q?=E3=82=8C=E3=81=AB=E4=BC=B4=E3=81=84=E3=80=81NyARSquare=E3=82=92?= =?utf8?q?=E6=89=B1=E3=81=86=E3=82=AF=E3=83=A9=E3=82=B9=E3=82=92=E4=BF=AE?= =?utf8?q?=E6=AD=A3=E3=80=82direction=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC?= =?utf8?q?=E3=82=BF=E3=82=92=E5=BB=83=E6=AD=A2=E3=81=97=E3=81=A6=E3=80=81?= =?utf8?q?=E3=81=82=E3=82=89=E3=81=8B=E3=81=98=E3=82=81=E9=A0=82=E7=82=B9?= =?utf8?q?=E5=BA=A7=E6=A8=99=E3=81=AB=E5=8F=8D=E6=98=A0=E3=81=99=E3=82=8B?= =?utf8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2.NyARSquareDetectorの仕様変更。スタックに矩形を一括して取得する方式から、コールバック関数で列挙する方式に変更。これに伴い、NyARSquareDetectorを使用するクラスを変更。 --- readme.ja.txt | 2 +- .../sandbox/qrcode/NyARQrCodeDetector.java | 12 +- .../qrcode/NyARRasterFilter_QrAreaAverage.java | 1 - .../sandbox/qrcode/NyARSingleQrDetector.java | 6 +- .../quadx2/NyARSingleDetectMarker_Quad.java | 6 +- .../sandbox/quadx2/NyARSquareDetector_Quad.java | 12 +- .../sandbox/vertexdetect/NyARVertexDetector.java | 8 +- .../sandbox/x2/NyARSingleDetectMarker_X2.java | 6 +- .../sandbox/x2/NyARSquareDetector_X2.java | 2 +- .../sandbox/x2/SingleARMarkerProcesser_X2.java | 4 +- .../core/match/NyARMatchPattResult.java | 4 + .../core/match/NyARMatchPatt_BlackWhite.java | 2 +- .../match/NyARMatchPatt_Color_WITHOUT_PCA.java | 4 +- .../core/match/NyARMatchPatt_Color_WITH_PCA.java | 2 +- .../core/squaredetect/ContourPickup.java | 6 +- .../core/squaredetect/Coord2Linear.java | 134 +++++++++ .../squaredetect/Coord2SquareVertexIndexes.java | 97 ++++++- ...tector.java => INyARSquareContourDetector.java} | 15 +- .../nyartoolkit/core/squaredetect/NyARSquare.java | 2 - ...va => NyARSquareContourDetector_ARToolKit.java} | 34 ++- ...Rle.java => NyARSquareContourDetector_Rle.java} | 50 ++-- .../core/squaredetect/NyARVertexCounter.java | 123 -------- .../core/squaredetect/SquareContourDetector.java | 153 ---------- .../nyartoolkit/core/transmat/INyARTransMat.java | 4 +- .../nyartoolkit/core/transmat/NyARTransMat.java | 44 +-- .../core/transmat/NyARTransMat_ARToolKit.java | 53 +--- .../nyartoolkit/core/types/NyARHistgram.java | 5 +- .../detector/NyARCustomSingleDetectMarker.java | 188 +++++++------ .../nyartoolkit/detector/NyARDetectMarker.java | 262 +++++++++-------- .../detector/NyARSingleDetectMarker.java | 8 +- .../nyartoolkit/nyidmarker/NyIdMarkerPickup.java | 4 +- .../nyidmarker/data/NyIdMarkerData_RawBit.java | 2 +- .../processor/SingleARMarkerProcesser.java | 313 ++++++++++----------- .../processor/SingleNyIdMarkerProcesser.java | 236 +++++++++------- 34 files changed, 879 insertions(+), 925 deletions(-) create mode 100644 src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2Linear.java rename src/jp/nyatla/nyartoolkit/core/squaredetect/{INyARSquareDetector.java => INyARSquareContourDetector.java} (73%) rename src/jp/nyatla/nyartoolkit/core/squaredetect/{NyARSquareDetector_ARToolKit.java => NyARSquareContourDetector_ARToolKit.java} (82%) rename src/jp/nyatla/nyartoolkit/core/squaredetect/{NyARSquareDetector_Rle.java => NyARSquareContourDetector_Rle.java} (77%) delete mode 100644 src/jp/nyatla/nyartoolkit/core/squaredetect/NyARVertexCounter.java delete mode 100644 src/jp/nyatla/nyartoolkit/core/squaredetect/SquareContourDetector.java diff --git a/readme.ja.txt b/readme.ja.txt index a4b6d29..ada4a7d 100644 --- a/readme.ja.txt +++ b/readme.ja.txt @@ -1,7 +1,7 @@ ARToolKit Java class library NyARToolkit. Copyright (C)2008 R.Iizuka -version 2.4.0 +version 2.4.1 http://nyatla.jp/nyartoolkit/ airmail(at)ebony.plala.or.jp diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARQrCodeDetector.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARQrCodeDetector.java index 623437a..f17f34c 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARQrCodeDetector.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARQrCodeDetector.java @@ -8,13 +8,13 @@ import jp.nyatla.nyartoolkit.core.labeling.artoolkit.NyARLabeling_ARToolKit; import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor; import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster; import jp.nyatla.nyartoolkit.core.squaredetect.ContourPickup; -import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack; -import jp.nyatla.nyartoolkit.core.squaredetect.SquareContourDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.Coord2Linear; import jp.nyatla.nyartoolkit.core.types.*; -public class NyARQrCodeDetector implements INyARSquareDetector +public class NyARQrCodeDetector implements INyARSquareContourDetector { private NyARQrCodeSymbolBinder _binder; @@ -30,7 +30,7 @@ public class NyARQrCodeDetector implements INyARSquareDetector private final NyARLabelingImage _limage; - private final SquareContourDetector _sqconvertor; + private final Coord2Linear _sqconvertor; private final ContourPickup _cpickup=new ContourPickup(); /** @@ -45,7 +45,7 @@ public class NyARQrCodeDetector implements INyARSquareDetector this._labeling = new NyARLabeling_ARToolKit(); this._limage = new NyARLabelingImage(this._width, this._height); this._binder=new NyARQrCodeSymbolBinder(i_dist_factor_ref); - this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref); + this._sqconvertor=new Coord2Linear(i_size,i_dist_factor_ref); // 輪郭の最大長はMAX_COORD_NUMの2倍に制限 int number_of_coord = MAX_COORD_NUM* 2; @@ -144,7 +144,7 @@ public class NyARQrCodeDetector implements INyARSquareDetector continue; } //輪郭分析用に正規化する。 - final int vertex1 = SquareContourDetector.normalizeCoord(xcoord, ycoord, coord_num); + final int vertex1 = Coord2Linear.normalizeCoord(xcoord, ycoord, coord_num); //ここから先が輪郭分析 NyARSquare square_ptr = o_square_stack.prePush(); diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARRasterFilter_QrAreaAverage.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARRasterFilter_QrAreaAverage.java index 629b8f5..006ad96 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARRasterFilter_QrAreaAverage.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARRasterFilter_QrAreaAverage.java @@ -33,7 +33,6 @@ package jp.nyatla.nyartoolkit.sandbox.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.*; /** diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARSingleQrDetector.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARSingleQrDetector.java index 621c401..874c555 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARSingleQrDetector.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/qrcode/NyARSingleQrDetector.java @@ -38,7 +38,7 @@ import jp.nyatla.nyartoolkit.core.raster.rgb.*; import jp.nyatla.nyartoolkit.core.rasterfilter.*; import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.INyARRasterFilter_RgbToGs; import jp.nyatla.nyartoolkit.core.raster.*; -import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack; import jp.nyatla.nyartoolkit.core.transmat.*; @@ -54,7 +54,7 @@ public class NyARSingleQrDetector private static final int AR_SQUARE_MAX = 100; private boolean _is_continue = false; - private INyARSquareDetector _square_detect; + private INyARSquareContourDetector _square_detect; private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); @@ -116,7 +116,7 @@ public class NyARSingleQrDetector this._detected_square = null; NyARSquareStack l_square_list = this._square_list; // スクエアコードを探す - this._square_detect.detectMarker(this._bin_raster, l_square_list); + this._square_detect.detectMarkerCB(this._bin_raster, l_square_list); //変換する diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSingleDetectMarker_Quad.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSingleDetectMarker_Quad.java index b1dd106..664f231 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSingleDetectMarker_Quad.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSingleDetectMarker_Quad.java @@ -38,7 +38,7 @@ 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.raster.*; -import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack; import jp.nyatla.nyartoolkit.core.transmat.*; @@ -57,7 +57,7 @@ public class NyARSingleDetectMarker_Quad private boolean _is_continue = false; private NyARMatchPatt_Color_WITHOUT_PCA _match_patt; - private INyARSquareDetector _square_detect; + private INyARSquareContourDetector _square_detect; private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); @@ -136,7 +136,7 @@ public class NyARSingleDetectMarker_Quad this._detected_square = null; NyARSquareStack l_square_list = this._square_list; // スクエアコードを探す - this._square_detect.detectMarker(this._bin_raster, l_square_list); + this._square_detect.detectMarkerCB(this._bin_raster, l_square_list); int number_of_square = l_square_list.getLength(); diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSquareDetector_Quad.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSquareDetector_Quad.java index 689861e..c4737cd 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSquareDetector_Quad.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/quadx2/NyARSquareDetector_Quad.java @@ -35,10 +35,10 @@ import jp.nyatla.nyartoolkit.core.labeling.*; import jp.nyatla.nyartoolkit.core.labeling.artoolkit.*; import jp.nyatla.nyartoolkit.core.raster.*; import jp.nyatla.nyartoolkit.core.squaredetect.ContourPickup; -import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack; -import jp.nyatla.nyartoolkit.core.squaredetect.SquareContourDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.Coord2Linear; import jp.nyatla.nyartoolkit.core.types.*; import jp.nyatla.nyartoolkit.core.param.*; @@ -49,7 +49,7 @@ import jp.nyatla.nyartoolkit.sandbox.x2.*; * 1/4に解像度を落して解析するNyARSquareDetector_X2 * 与えるBinRasterが既に1/4のサイズになっていないといけないことに注意 */ -public class NyARSquareDetector_Quad implements INyARSquareDetector +public class NyARSquareDetector_Quad implements INyARSquareContourDetector { private static int AR_AREA_MAX = 25000;// #define AR_AREA_MAX 100000 @@ -62,7 +62,7 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector private NyARLabelingImage _limage; private final LabelOverlapChecker _overlap_checker = new LabelOverlapChecker(32,NyARLabelingLabel.class); - private final SquareContourDetector _sqconvertor; + private final Coord2Linear _sqconvertor; private final ContourPickup _cpickup=new ContourPickup(); /** * 最大i_squre_max個のマーカーを検出するクラスを作成する。 @@ -75,7 +75,7 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector this._height = i_size.h / 2; this._labeling = new NyARLabeling_ARToolKit(); this._limage = new NyARLabelingImage(this._width, this._height); - this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref); + this._sqconvertor=new Coord2Linear(i_size,i_dist_factor_ref); // 輪郭の最大長は画面に映りうる最大の長方形サイズ。 int number_of_coord = (this._width + this._height) * 2; @@ -190,7 +190,7 @@ public class NyARSquareDetector_Quad implements INyARSquareDetector continue; } //輪郭分析用に正規化する。 - final int vertex1 = SquareContourDetector.normalizeCoord(xcoord, ycoord, coord_num); + final int vertex1 = Coord2Linear.normalizeCoord(xcoord, ycoord, coord_num); //ここから先が輪郭分析 NyARSquare square_ptr = o_square_stack.prePush(); diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/vertexdetect/NyARVertexDetector.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/vertexdetect/NyARVertexDetector.java index 09a34e7..1a2a70b 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/vertexdetect/NyARVertexDetector.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/vertexdetect/NyARVertexDetector.java @@ -41,7 +41,7 @@ import jp.nyatla.nyartoolkit.core.param.*; * PCAではなく、頂点座標そのものからSquare位置を計算するクラス * */ -public class NyARVertexDetector implements INyARSquareDetector +public class NyARVertexDetector implements INyARSquareContourDetector { private static final int AR_AREA_MAX = 100000;// #define AR_AREA_MAX 100000 @@ -54,7 +54,7 @@ public class NyARVertexDetector implements INyARSquareDetector private final NyARLabelingImage _limage; private final LabelOverlapChecker _overlap_checker = new LabelOverlapChecker(32,NyARLabelingLabel.class); - private final SquareContourDetector _sqconvertor; + private final Coord2Linear _sqconvertor; private final ContourPickup _cpickup=new ContourPickup(); /** @@ -68,7 +68,7 @@ public class NyARVertexDetector implements INyARSquareDetector this._height = i_size.h; this._labeling = new NyARLabeling_ARToolKit(); this._limage = new NyARLabelingImage(this._width, this._height); - this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref); + this._sqconvertor=new Coord2Linear(i_size,i_dist_factor_ref); // 輪郭の最大長は画面に映りうる最大の長方形サイズ。 int number_of_coord = (this._width + this._height) * 2; @@ -165,7 +165,7 @@ public class NyARVertexDetector implements INyARSquareDetector continue; } //輪郭分析用に正規化する。 - final int vertex1 = SquareContourDetector.normalizeCoord(xcoord, ycoord, coord_num); + final int vertex1 = Coord2Linear.normalizeCoord(xcoord, ycoord, coord_num); //ここから先が輪郭分析 NyARSquare square_ptr = o_square_stack.prePush(); diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSingleDetectMarker_X2.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSingleDetectMarker_X2.java index 90344cd..4e31eef 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSingleDetectMarker_X2.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSingleDetectMarker_X2.java @@ -43,7 +43,7 @@ import jp.nyatla.nyartoolkit.core.types.NyARIntSize; import jp.nyatla.nyartoolkit.core.rasterfilter.gs2bin.*; import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilterBuilder_RgbToBin; import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold; -import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack; @@ -58,7 +58,7 @@ public class NyARSingleDetectMarker_X2 private boolean _is_continue = false; private NyARMatchPatt_Color_WITHOUT_PCA _match_patt; - private INyARSquareDetector _square_detect; + private INyARSquareContourDetector _square_detect; private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); @@ -137,7 +137,7 @@ public class NyARSingleDetectMarker_X2 this._detected_square = null; NyARSquareStack l_square_list = this._square_list; // スクエアコードを探す - this._square_detect.detectMarker(this._bin_raster, l_square_list); + this._square_detect.detectMarkerCB(this._bin_raster, l_square_list); int number_of_square = l_square_list.getLength(); diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSquareDetector_X2.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSquareDetector_X2.java index fd2eaf4..0549425 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSquareDetector_X2.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/NyARSquareDetector_X2.java @@ -45,7 +45,7 @@ import jp.nyatla.nyartoolkit.core.param.*; * このクラスは、arDetectMarker2.cとの置き換えになります。 * */ -public class NyARSquareDetector_X2 implements INyARSquareDetector +public class NyARSquareDetector_X2 implements INyARSquareContourDetector { private final int _width; private final int _height; diff --git a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/SingleARMarkerProcesser_X2.java b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/SingleARMarkerProcesser_X2.java index 67bf77b..5ae473c 100644 --- a/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/SingleARMarkerProcesser_X2.java +++ b/sample/sandbox/jp/nyatla/nyartoolkit/sandbox/x2/SingleARMarkerProcesser_X2.java @@ -72,7 +72,7 @@ public abstract class SingleARMarkerProcesser_X2 private int _lost_delay = 5; - private INyARSquareDetector _square_detect; + private INyARSquareContourDetector _square_detect; // protected NyARTransMat_X2 _transmat; @@ -170,7 +170,7 @@ public abstract class SingleARMarkerProcesser_X2 NyARSquareStack square_stack = this._square_list; // スクエアコードを探す - this._square_detect.detectMarker(this._bin_raster, square_stack); + this._square_detect.detectMarkerCB(this._bin_raster, square_stack); // 認識処理 if (this._current_arcode_index == -1) { // マーカ未認識 detectNewMarker(i_raster, square_stack); diff --git a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPattResult.java b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPattResult.java index fe82622..4c9064a 100644 --- a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPattResult.java +++ b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPattResult.java @@ -29,12 +29,16 @@ * */ package jp.nyatla.nyartoolkit.core.match; + + + /** * [[Strage class]] * */ public class NyARMatchPattResult { + public static final int DIRECTION_UNKNOWN=-1; public double confidence; public int direction; } \ No newline at end of file diff --git a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java index 5cdef1b..cd16554 100644 --- a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java +++ b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_BlackWhite.java @@ -76,7 +76,7 @@ public class NyARMatchPatt_BlackWhite implements INyARMatchPatt final int[] linput = i_patt.refData(); int sum; double max = 0.0; - int res = NyARSquare.DIRECTION_UNKNOWN; + int res = NyARMatchPattResult.DIRECTION_UNKNOWN; for (int j = 0; j < 4; j++) { diff --git a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java index 1e32b23..69e5609 100644 --- a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java +++ b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITHOUT_PCA.java @@ -79,8 +79,8 @@ public class NyARMatchPatt_Color_WITHOUT_PCA implements INyARMatchPatt // final int[] linput = i_patt.refData(); int sum; - double max = 0.0; - int res = NyARSquare.DIRECTION_UNKNOWN; + double max = Double.MIN_VALUE; + int res = NyARMatchPattResult.DIRECTION_UNKNOWN; final int for_mod=this._optimize_for_mod; for (int j = 0; j < 4; j++) { //合計値初期化 diff --git a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java index c3bcf55..7aa9d16 100644 --- a/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java +++ b/src/jp/nyatla/nyartoolkit/core/match/NyARMatchPatt_Color_WITH_PCA.java @@ -62,7 +62,7 @@ public class NyARMatchPatt_Color_WITH_PCA extends NyARMatchPatt_Color_WITHOUT_PC final int[] linput = i_patt.refData(); int sum; double max = 0.0; - int res = NyARSquare.DIRECTION_UNKNOWN; + int res = NyARMatchPattResult.DIRECTION_UNKNOWN; /* NyARException.trap( "NyARMatchPatt_Color_WITH_PCA\n"+ diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/ContourPickup.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/ContourPickup.java index 3d7977b..bd103e6 100644 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/ContourPickup.java +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/ContourPickup.java @@ -34,6 +34,10 @@ import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.raster.*; import jp.nyatla.nyartoolkit.core.labeling.artoolkit.*; +/** + * 輪郭線を取得するクラスです。 + * + */ public class ContourPickup { //巡回参照できるように、テーブルを二重化 @@ -77,7 +81,7 @@ public class ContourPickup * 輪郭線の長さを返します。 * @throws NyARException */ - public int impl_getContour(INyARRaster i_raster,int i_th,int i_entry_x,int i_entry_y,int i_array_size,int[] o_coord_x,int[] o_coord_y) throws NyARException + private int impl_getContour(INyARRaster i_raster,int i_th,int i_entry_x,int i_entry_y,int i_array_size,int[] o_coord_x,int[] o_coord_y) throws NyARException { final int[] xdir = _getContour_xdir;// static int xdir[8] = { 0, 1, 1, 1, 0,-1,-1,-1}; final int[] ydir = _getContour_ydir;// static int ydir[8] = {-1,-1, 0, 1, 1, 1, 0,-1}; diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2Linear.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2Linear.java new file mode 100644 index 0000000..d3bb98c --- /dev/null +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2Linear.java @@ -0,0 +1,134 @@ +/* + * 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 edition ARToolKit class library. + * Copyright (C)2008-2009 Ryo 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 3 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 program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/nyatoolkit/ + * or + * + */ +package jp.nyatla.nyartoolkit.core.squaredetect; + +import jp.nyatla.nyartoolkit.NyARException; +import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor; +import jp.nyatla.nyartoolkit.core.param.NyARObserv2IdealMap; +import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d; +import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2; +import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d; +import jp.nyatla.nyartoolkit.core.types.NyARIntSize; +import jp.nyatla.nyartoolkit.core.types.NyARLinear; +import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22; + + + +/** + * 頂点集合を一次方程式のパラメータに変換します。 + * + * + */ +public class Coord2Linear +{ + private final double[] _xpos; + private final double[] _ypos; + private final INyARPca2d _pca; + private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22(); + private final double[] __getSquareLine_mean=new double[2]; + private final double[] __getSquareLine_ev=new double[2]; + private final NyARObserv2IdealMap _dist_factor; + public Coord2Linear(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor_ref) + { + //歪み計算テーブルを作ると、8*width/height*2の領域を消費します。 + //領域を取りたくない場合は、i_dist_factor_refの値をそのまま使ってください。 + this._dist_factor = new NyARObserv2IdealMap(i_distfactor_ref,i_size); + + + // 輪郭バッファ + this._pca=new NyARPca2d_MatrixPCA_O2(); + this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height + this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height + return; + } + + + /** + * 輪郭点集合からay+bx+c=0の直線式を計算します。 + * @param i_st + * @param i_ed + * @param i_xcoord + * @param i_ycoord + * @param i_cood_num + * @param o_line + * @return + * @throws NyARException + */ + public boolean coord2Line(int i_st,int i_ed,int[] i_xcoord, int[] i_ycoord,int i_cood_num, NyARLinear o_line) throws NyARException + { + //頂点を取得 + int n,st,ed; + double w1; + + //探索区間の決定 + if(i_ed>=i_st){ + //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき + w1 = (double) (i_ed - i_st + 1) * 0.05 + 0.5; + //探索区間の決定 + st = (int) (i_st+w1); + ed = (int) (i_ed - w1); + }else{ + //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき + w1 = (double) (i_ed+i_cood_num-i_st+1)%i_cood_num * 0.05 + 0.5; + //探索区間の決定 + st = (int) (i_st+w1)%i_cood_num; + ed = (int) (i_ed+i_cood_num-w1)%i_cood_num; + } + //探索区間数を確認 + if(st<=ed){ + //探索区間は1区間 + n = ed - st + 1; + this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st, n,this._xpos,this._ypos,0); + }else{ + //探索区間は2区間 + n=ed+1+i_cood_num-st; + this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st,i_cood_num-st,this._xpos,this._ypos,0); + this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, 0,ed+1,this._xpos,this._ypos,i_cood_num-st); + } + //要素数の確認 + if (n < 2) { + // nが2以下でmatrix.PCAを計算することはできないので、エラー + return false; + } + //主成分分析する。 + final NyARDoubleMatrix22 evec=this.__getSquareLine_evec; + final double[] mean=this.__getSquareLine_mean; + + + this._pca.pca(this._xpos,this._ypos,n,evec, this.__getSquareLine_ev,mean); + o_line.dy = evec.m01;// line[i][0] = evec->m[1]; + o_line.dx = -evec.m00;// line[i][1] = -evec->m[0]; + o_line.c = -(o_line.dy * mean[0] + o_line.dx * mean[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]); + + return true; + } +} \ No newline at end of file diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2SquareVertexIndexes.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2SquareVertexIndexes.java index 47e8806..8a6ea35 100644 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2SquareVertexIndexes.java +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/Coord2SquareVertexIndexes.java @@ -31,7 +31,7 @@ package jp.nyatla.nyartoolkit.core.squaredetect; /** - * 座標店集合(輪郭線)から、頂点リストを計算します。 + * 座標店集合(輪郭線)から、四角系の頂点候補点を計算します。 * */ public class Coord2SquareVertexIndexes @@ -157,4 +157,99 @@ public class Coord2SquareVertexIndexes } return ret; } +} + + + + +/** + * get_vertex関数を切り離すためのクラス + * + */ +final class NyARVertexCounter +{ + public final int[] vertex = new int[10];// 6まで削れる + + public int number_of_vertex; + + private double thresh; + + private int[] x_coord; + + private int[] y_coord; + + public boolean getVertex(int[] i_x_coord, int[] i_y_coord,int i_coord_len,int st, int ed, double i_thresh) + { + this.number_of_vertex = 0; + this.thresh = i_thresh; + this.x_coord = i_x_coord; + this.y_coord = i_y_coord; + return get_vertex(st, ed,i_coord_len); + } + + /** + * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数 + * + * @param x_coord + * @param y_coord + * @param st + * @param ed + * @param thresh + * @return + */ + private boolean get_vertex(int st, int ed,int i_coord_len) + { + //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。 + //dmaxは4乗なのでやるとしてもint64じゃないとマズイ + int v1 = 0; + final int[] lx_coord = this.x_coord; + final int[] ly_coord = this.y_coord; + final int a = ly_coord[ed] - ly_coord[st]; + final int b = lx_coord[st] - lx_coord[ed]; + final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st]; + double dmax = 0; + if(st dmax) { + dmax = d * d; + v1 = i; + } + } + }else{ + //stとedが2区間 + for (int i = st + 1; i < i_coord_len; i++) { + final double d = a * lx_coord[i] + b * ly_coord[i] + c; + if (d * d > dmax) { + dmax = d * d; + v1 = i; + } + } + for (int i = 0; i < ed; i++) { + final double d = a * lx_coord[i] + b * ly_coord[i] + c; + if (d * d > dmax) { + dmax = d * d; + v1 = i; + } + } + } + + + if (dmax / (double)(a * a + b * b) > thresh) { + if (!get_vertex(st, v1,i_coord_len)) { + return false; + } + if (number_of_vertex > 5) { + return false; + } + vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1; + number_of_vertex++;// (*vnum)++; + + if (!get_vertex(v1, ed,i_coord_len)) { + return false; + } + } + return true; + } } \ No newline at end of file diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/INyARSquareDetector.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/INyARSquareContourDetector.java similarity index 73% rename from src/jp/nyatla/nyartoolkit/core/squaredetect/INyARSquareDetector.java rename to src/jp/nyatla/nyartoolkit/core/squaredetect/INyARSquareContourDetector.java index b689a03..fa149ac 100644 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/INyARSquareDetector.java +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/INyARSquareContourDetector.java @@ -33,7 +33,18 @@ package jp.nyatla.nyartoolkit.core.squaredetect; import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster; -public interface INyARSquareDetector +public interface INyARSquareContourDetector { - public void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException; + public interface DetectMarkerCallback + { + public void onSquareDetect(INyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException; + } + /** + * + * @param i_raster + * @param o_square_stack + * @throws NyARException + */ + public void detectMarkerCB(NyARBinRaster i_raster, DetectMarkerCallback i_callback) throws NyARException; } + diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquare.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquare.java index 1fba556..18fe108 100644 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquare.java +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquare.java @@ -40,8 +40,6 @@ import jp.nyatla.nyartoolkit.core.types.*; */ public class NyARSquare { - public final static int DIRECTION_UNKNOWN=-1; - public int direction; public NyARLinear[] line = NyARLinear.createArray(4); public NyARDoublePoint2d[] sqvertex = NyARDoublePoint2d.createArray(4); public NyARIntPoint2d[] imvertex = NyARIntPoint2d.createArray(4); diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareDetector_ARToolKit.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareContourDetector_ARToolKit.java similarity index 82% rename from src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareDetector_ARToolKit.java rename to src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareContourDetector_ARToolKit.java index 6f6c591..50c346c 100644 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareDetector_ARToolKit.java +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareContourDetector_ARToolKit.java @@ -42,7 +42,7 @@ import jp.nyatla.nyartoolkit.core.types.NyARIntSize; -public class NyARSquareDetector_ARToolKit implements INyARSquareDetector +public class NyARSquareContourDetector_ARToolKit implements INyARSquareContourDetector { private static final int AR_AREA_MAX = 100000;// #define AR_AREA_MAX 100000 private static final int AR_AREA_MIN = 70;// #define AR_AREA_MIN 70 @@ -54,23 +54,23 @@ public class NyARSquareDetector_ARToolKit implements INyARSquareDetector private final NyARLabelingImage _limage; private final LabelOverlapChecker _overlap_checker = new LabelOverlapChecker(32,NyARLabelingLabel.class); - private final SquareContourDetector _sqconvertor; private final ContourPickup _cpickup=new ContourPickup(); + private final Coord2SquareVertexIndexes _coord2vertex=new Coord2SquareVertexIndexes(); private final int _max_coord; private final int[] _xcoord; private final int[] _ycoord; + private final int[] __detectMarker_mkvertex = new int[4]; /** * 最大i_squre_max個のマーカーを検出するクラスを作成する。 * * @param i_param */ - public NyARSquareDetector_ARToolKit(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException + public NyARSquareContourDetector_ARToolKit(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException { this._width = i_size.w; this._height = i_size.h; this._labeling = new NyARLabeling_ARToolKit(); - this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref); this._limage = new NyARLabelingImage(this._width, this._height); // 輪郭の最大長は画面に映りうる最大の長方形サイズ。 @@ -93,16 +93,11 @@ public class NyARSquareDetector_ARToolKit implements INyARSquareDetector * 抽出した正方形候補を格納するリスト * @throws NyARException */ - public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException + public final void detectMarkerCB(NyARBinRaster i_raster, DetectMarkerCallback i_callback) throws NyARException { final NyARLabelingImage limage = this._limage; - // 初期化 - - // マーカーホルダをリセット - o_square_stack.clear(); - - // ラベル数が0ならここまで(Labeling内部でソートするようにした。) + // ラベル数が0ならここまで final int label_num = this._labeling.labeling(i_raster,this._limage); if (label_num < 1) { return; @@ -122,17 +117,17 @@ public class NyARSquareDetector_ARToolKit implements INyARSquareDetector 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 LabelOverlapChecker overlap = this._overlap_checker; //重なりチェッカの最大数を設定 overlap.setMaxLabels(label_num); - for (; i < label_num; i++) { final NyARLabelingLabel label_pt = labels[i]; final int label_area = label_pt.area; @@ -158,14 +153,17 @@ public class NyARSquareDetector_ARToolKit implements INyARSquareDetector // 輪郭が大きすぎる。 continue; } - //ここから先が輪郭分析 - NyARSquare square_ptr = o_square_stack.prePush(); - if(!this._sqconvertor.coordToSquare(xcoord,ycoord,coord_num,label_area,square_ptr)){ - o_square_stack.pop();// 頂点の取得が出来なかったので破棄 - continue; + //輪郭線をチェックして、矩形かどうかを判定。矩形ならばmkvertexに取得 + if (!this._coord2vertex.getVertexIndexes(xcoord, ycoord,coord_num,label_area, mkvertex)) { + // 頂点の取得が出来なかった + continue; } + //矩形を発見したことをコールバック関数で通知 + i_callback.onSquareDetect(this,xcoord,ycoord,coord_num,mkvertex); + // 検出済の矩形の属したラベルを重なりチェックに追加する。 overlap.push(label_pt); + } return; } diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareDetector_Rle.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareContourDetector_Rle.java similarity index 77% rename from src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareDetector_Rle.java rename to src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareContourDetector_Rle.java index a8caba2..1d460d1 100644 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareDetector_Rle.java +++ b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARSquareContourDetector_Rle.java @@ -35,11 +35,11 @@ import jp.nyatla.nyartoolkit.core.labeling.LabelOverlapChecker; import jp.nyatla.nyartoolkit.core.labeling.rlelabeling.*; import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor; import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster; -import jp.nyatla.nyartoolkit.core.types.NyARIntSize; +import jp.nyatla.nyartoolkit.core.types.*; +import java.util.*; - -public class NyARSquareDetector_Rle implements INyARSquareDetector +public class NyARSquareContourDetector_Rle implements INyARSquareContourDetector { private static final int AR_AREA_MAX = 100000;// #define AR_AREA_MAX 100000 private static final int AR_AREA_MIN = 70;// #define AR_AREA_MIN 70 @@ -49,9 +49,9 @@ public class NyARSquareDetector_Rle implements INyARSquareDetector private final NyARLabeling_Rle _labeling; private final LabelOverlapChecker _overlap_checker = new LabelOverlapChecker(32,RleLabelFragmentInfoStack.RleLabelFragmentInfo.class); - private final SquareContourDetector _sqconvertor; private final ContourPickup _cpickup=new ContourPickup(); private final RleLabelFragmentInfoStack _stack; + private final Coord2SquareVertexIndexes _coord2vertex=new Coord2SquareVertexIndexes(); private final int _max_coord; private final int[] _xcoord; @@ -61,14 +61,13 @@ public class NyARSquareDetector_Rle implements INyARSquareDetector * * @param i_param */ - public NyARSquareDetector_Rle(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException + public NyARSquareContourDetector_Rle(NyARCameraDistortionFactor i_dist_factor_ref,NyARIntSize i_size) throws NyARException { this._width = i_size.w; this._height = i_size.h; //ラベリングのサイズを指定したいときはsetAreaRangeを使ってね。 this._labeling = new NyARLabeling_Rle(this._width,this._height); this._labeling.setAreaRange(AR_AREA_MAX, AR_AREA_MIN); - this._sqconvertor=new SquareContourDetector(i_size,i_dist_factor_ref); this._stack=new RleLabelFragmentInfoStack(i_size.w*i_size.h*2048/(320*240)+32);//検出可能な最大ラベル数 @@ -82,24 +81,13 @@ public class NyARSquareDetector_Rle implements INyARSquareDetector return; } - /** - * arDetectMarker2を基にした関数 - * この関数はNyARSquare要素のうち、directionを除くパラメータを取得して返します。 - * directionの確定は行いません。 - * @param i_raster - * 解析する2値ラスタイメージを指定します。 - * @param o_square_stack - * 抽出した正方形候補を格納するリスト - * @throws NyARException - */ - public final void detectMarker(NyARBinRaster i_raster, NyARSquareStack o_square_stack) throws NyARException + private final int[] __detectMarker_mkvertex = new int[4]; + + public void detectMarkerCB(NyARBinRaster i_raster, DetectMarkerCallback i_callback) throws NyARException { final RleLabelFragmentInfoStack flagment=this._stack; final LabelOverlapChecker overlap = this._overlap_checker; - // マーカーホルダをリセット - o_square_stack.clear(); - // ラベル数が0ならここまで final int label_num=this._labeling.labeling(i_raster, 0, i_raster.getHeight(), flagment); if (label_num < 1) { @@ -115,6 +103,7 @@ public class NyARSquareDetector_Rle implements INyARSquareDetector int[] xcoord = this._xcoord; int[] ycoord = this._ycoord; final int coord_max = this._max_coord; + final int[] mkvertex =this.__detectMarker_mkvertex; //重なりチェッカの最大数を設定 @@ -136,25 +125,24 @@ public class NyARSquareDetector_Rle implements INyARSquareDetector // 重なっているようだ。 continue; } - - // 輪郭を取得 + + //輪郭を取得 int coord_num = _cpickup.getContour(i_raster,label_pt.entry_x,label_pt.clip_t, coord_max, xcoord, ycoord); if (coord_num == coord_max) { // 輪郭が大きすぎる。 continue; } - - NyARSquare square_ptr = o_square_stack.prePush(); - - //ここから先が輪郭分析 - if(!this._sqconvertor.coordToSquare(xcoord,ycoord,coord_num,label_area,square_ptr)){ - o_square_stack.pop();// 頂点の取得が出来なかったので破棄 - continue; + //輪郭線をチェックして、矩形かどうかを判定。矩形ならばmkvertexに取得 + if (!this._coord2vertex.getVertexIndexes(xcoord, ycoord,coord_num,label_area, mkvertex)) { + // 頂点の取得が出来なかった + continue; } - - + //矩形を発見したことをコールバック関数で通知 + i_callback.onSquareDetect(this,xcoord,ycoord,coord_num,mkvertex); + // 検出済の矩形の属したラベルを重なりチェックに追加する。 overlap.push(label_pt); + } return; } diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARVertexCounter.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARVertexCounter.java deleted file mode 100644 index 8aa1833..0000000 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/NyARVertexCounter.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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 edition ARToolKit class library. - * Copyright (C)2008-2009 Ryo 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 3 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 program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * or - * - */ -package jp.nyatla.nyartoolkit.core.squaredetect; - -/** - * get_vertex関数を切り離すためのクラス - * - */ -final public class NyARVertexCounter -{ - public final int[] vertex = new int[10];// 6まで削れる - - public int number_of_vertex; - - private double thresh; - - private int[] x_coord; - - private int[] y_coord; - - public boolean getVertex(int[] i_x_coord, int[] i_y_coord,int i_coord_len,int st, int ed, double i_thresh) - { - this.number_of_vertex = 0; - this.thresh = i_thresh; - this.x_coord = i_x_coord; - this.y_coord = i_y_coord; - return get_vertex(st, ed,i_coord_len); - } - - /** - * static int get_vertex( int x_coord[], int y_coord[], int st, int ed,double thresh, int vertex[], int *vnum) 関数の代替関数 - * - * @param x_coord - * @param y_coord - * @param st - * @param ed - * @param thresh - * @return - */ - private boolean get_vertex(int st, int ed,int i_coord_len) - { - //メモ:座標値は65536を超えなければint32で扱って大丈夫なので変更。 - //dmaxは4乗なのでやるとしてもint64じゃないとマズイ - int v1 = 0; - final int[] lx_coord = this.x_coord; - final int[] ly_coord = this.y_coord; - final int a = ly_coord[ed] - ly_coord[st]; - final int b = lx_coord[st] - lx_coord[ed]; - final int c = lx_coord[ed] * ly_coord[st] - ly_coord[ed] * lx_coord[st]; - double dmax = 0; - if(st dmax) { - dmax = d * d; - v1 = i; - } - } - }else{ - //stとedが2区間 - for (int i = st + 1; i < i_coord_len; i++) { - final double d = a * lx_coord[i] + b * ly_coord[i] + c; - if (d * d > dmax) { - dmax = d * d; - v1 = i; - } - } - for (int i = 0; i < ed; i++) { - final double d = a * lx_coord[i] + b * ly_coord[i] + c; - if (d * d > dmax) { - dmax = d * d; - v1 = i; - } - } - } - - - if (dmax / (double)(a * a + b * b) > thresh) { - if (!get_vertex(st, v1,i_coord_len)) { - return false; - } - if (number_of_vertex > 5) { - return false; - } - vertex[number_of_vertex] = v1;// vertex[(*vnum)] = v1; - number_of_vertex++;// (*vnum)++; - - if (!get_vertex(v1, ed,i_coord_len)) { - return false; - } - } - return true; - } -} \ No newline at end of file diff --git a/src/jp/nyatla/nyartoolkit/core/squaredetect/SquareContourDetector.java b/src/jp/nyatla/nyartoolkit/core/squaredetect/SquareContourDetector.java deleted file mode 100644 index 61a2f2a..0000000 --- a/src/jp/nyatla/nyartoolkit/core/squaredetect/SquareContourDetector.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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 edition ARToolKit class library. - * Copyright (C)2008-2009 Ryo 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 3 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 program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/nyatoolkit/ - * or - * - */ -package jp.nyatla.nyartoolkit.core.squaredetect; - -import jp.nyatla.nyartoolkit.NyARException; -import jp.nyatla.nyartoolkit.core.param.NyARCameraDistortionFactor; -import jp.nyatla.nyartoolkit.core.param.NyARObserv2IdealMap; -import jp.nyatla.nyartoolkit.core.pca2d.INyARPca2d; -import jp.nyatla.nyartoolkit.core.pca2d.NyARPca2d_MatrixPCA_O2; -import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; -import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d; -import jp.nyatla.nyartoolkit.core.types.NyARIntSize; -import jp.nyatla.nyartoolkit.core.types.NyARLinear; -import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22; - - - - -public class SquareContourDetector -{ - private final double[] _xpos; - private final double[] _ypos; - private final int[] __detectMarker_mkvertex = new int[4]; - private final INyARPca2d _pca; - private final NyARDoubleMatrix22 __getSquareLine_evec=new NyARDoubleMatrix22(); - private final double[] __getSquareLine_mean=new double[2]; - private final double[] __getSquareLine_ev=new double[2]; - private final Coord2SquareVertexIndexes _coord2vertex=new Coord2SquareVertexIndexes(); - private final NyARObserv2IdealMap _dist_factor; - public SquareContourDetector(NyARIntSize i_size,NyARCameraDistortionFactor i_distfactor_ref) - { - //歪み計算テーブルを作ると、8*width/height*2の領域を消費します。 - //領域を取りたくない場合は、i_dist_factor_refの値をそのまま使ってください。 - this._dist_factor = new NyARObserv2IdealMap(i_distfactor_ref,i_size); - - - // 輪郭バッファ - this._pca=new NyARPca2d_MatrixPCA_O2(); - this._xpos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height - this._ypos=new double[i_size.w+i_size.h];//最大辺長はthis._width+this._height - return; - } - - public boolean coordToSquare(int[] i_xcoord,int[] i_ycoord,int i_coord_num,int i_label_area,NyARSquare o_square) throws NyARException - { - - final int[] mkvertex = this.__detectMarker_mkvertex; - // 頂点情報を取得 - if (!this._coord2vertex.getVertexIndexes(i_xcoord, i_ycoord, i_coord_num, i_label_area, mkvertex)) { - // 頂点の取得が出来なかったので破棄 - return false; - } - // マーカーを検出 - if (!getSquareLine(mkvertex, i_xcoord, i_ycoord,i_coord_num-1, o_square)){ - // 矩形が成立しなかった。 - return false; - } - return true; - } - - private boolean getSquareLine(int[] i_mkvertex, int[] i_xcoord, int[] i_ycoord,int i_cood_num, NyARSquare o_square) throws NyARException - { - final NyARLinear[] l_line = o_square.line; - final NyARDoubleMatrix22 evec=this.__getSquareLine_evec; - final double[] mean=this.__getSquareLine_mean; - final double[] ev=this.__getSquareLine_ev; - - double w1; - for (int i = 0; i < 4; i++){ - //頂点を取得 - int ver1=i_mkvertex[i]; - int ver2=i_mkvertex[(i+1)%4]; - int n,st,ed; - //探索区間の決定 - if(ver2>=i_mkvertex[i]){ - //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき - w1 = (double) (ver2 - ver1 + 1) * 0.05 + 0.5; - //探索区間の決定 - st = (int) (ver1+w1); - ed = (int) (ver2 - w1); - }else{ - //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき - w1 = (double) (ver2+i_cood_num-ver1+1)%i_cood_num * 0.05 + 0.5; - //探索区間の決定 - st = (int) (ver1+w1)%i_cood_num; - ed = (int) (ver2+i_cood_num-w1)%i_cood_num; - } - //探索区間数を確認 - if(st<=ed){ - //探索区間は1区間 - n = ed - st + 1; - this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st, n,this._xpos,this._ypos,0); - }else{ - //探索区間は2区間 - n=ed+1+i_cood_num-st; - this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, st,i_cood_num-st,this._xpos,this._ypos,0); - this._dist_factor.observ2IdealBatch(i_xcoord, i_ycoord, 0,ed+1,this._xpos,this._ypos,i_cood_num-st); - } - //要素数の確認 - if (n < 2) { - // nが2以下でmatrix.PCAを計算することはできないので、エラー - return false; - } - //主成分分析する。 - this._pca.pca(this._xpos,this._ypos,n,evec, ev,mean); - final NyARLinear l_line_i = l_line[i]; - l_line_i.dy = evec.m01;// line[i][0] = evec->m[1]; - l_line_i.dx = -evec.m00;// line[i][1] = -evec->m[0]; - l_line_i.c = -(l_line_i.dy * mean[0] + l_line_i.dx * mean[1]);// line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]); - } - - final NyARDoublePoint2d[] l_sqvertex = o_square.sqvertex; - final NyARIntPoint2d[] l_imvertex = o_square.imvertex; - for (int i = 0; i < 4; i++) { - //直線同士の交点計算 - if(!NyARLinear.crossPos(l_line[i],l_line[(i + 3) % 4],l_sqvertex[i])){ - return false; - } - // 頂点インデクスから頂点座標を得て保存 - l_imvertex[i].x = i_xcoord[i_mkvertex[i]]; - l_imvertex[i].y = i_ycoord[i_mkvertex[i]]; - } - return true; - } -} \ No newline at end of file diff --git a/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java b/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java index 7d263fc..e5c5841 100644 --- a/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java +++ b/src/jp/nyatla/nyartoolkit/core/transmat/INyARTransMat.java @@ -41,6 +41,6 @@ import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; public interface INyARTransMat { public void setCenter(double i_x, double i_y); - public void transMat(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result) throws NyARException; - public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult io_result_conv) throws NyARException; + public void transMat(NyARSquare i_square, double i_width, NyARTransMatResult o_result) throws NyARException; + public void transMatContinue(NyARSquare i_square, double i_width, NyARTransMatResult io_result_conv) throws NyARException; } diff --git a/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java b/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java index b155345..4d4998e 100644 --- a/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java +++ b/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat.java @@ -88,32 +88,10 @@ public class NyARTransMat implements INyARTransMat - /** - * 頂点順序をi_directionに対応して並べ替えます。 - * @param i_square - * @param i_direction - * @param o_sqvertex_ref - * @param o_liner_ref - */ - private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref) - { - //頂点順序を考慮した矩形の頂点情報 - o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4]; - o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4]; - o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4]; - o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4]; - o_liner_ref[0]=i_square.line[(4 - i_direction) % 4]; - o_liner_ref[1]=i_square.line[(5 - i_direction) % 4]; - o_liner_ref[2]=i_square.line[(6 - i_direction) % 4]; - o_liner_ref[3]=i_square.line[(7 - i_direction) % 4]; - return; - } - private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4]; private final NyARDoublePoint2d[] __transMat_vertex_2d = NyARDoublePoint2d.createArray(4); private final NyARDoublePoint3d[] __transMat_vertex_3d = NyARDoublePoint3d.createArray(4); - private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4]; private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d(); /** @@ -142,27 +120,23 @@ public class NyARTransMat implements INyARTransMat * @return * @throws NyARException */ - public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException + public void transMat(final NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException { - final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; - final NyARLinear[] linear_ref=__transMat_linear_ref; final NyARDoublePoint3d trans=this.__transMat_trans; double err_threshold=makeErrThreshold(i_square.sqvertex); - //計算用に頂点情報を初期化(順番調整) - initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); //平行移動量計算機に、2D座標系をセット NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d; NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; - this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4); + this._ref_dist_factor.ideal2ObservBatch(i_square.sqvertex, vertex_2d,4); this._transsolver.set2dVertex(vertex_2d,4); //基準矩形の3D座標系を作成 this._offset.setSquare(i_width,this._center); //回転行列を計算 - this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); + this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex); //回転後の3D座標系から、平行移動量を計算 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); @@ -180,28 +154,24 @@ public class NyARTransMat implements INyARTransMat * (non-Javadoc) * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult) */ - public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException + public void transMatContinue(NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException { - final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; - final NyARLinear[] linear_ref=__transMat_linear_ref; final NyARDoublePoint3d trans=this.__transMat_trans; // io_result_convが初期値なら、transMatで計算する。 if (!o_result_conv.has_value) { - this.transMat(i_square, i_direction, i_width, o_result_conv); + this.transMat(i_square,i_width, o_result_conv); return; } //最適化計算の閾値を決定 double err_threshold=makeErrThreshold(i_square.sqvertex); - //計算用に頂点情報を初期化(順番調整) - initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); //平行移動量計算機に、2D座標系をセット NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d; NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; - this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4); + this._ref_dist_factor.ideal2ObservBatch(i_square.sqvertex, vertex_2d,4); this._transsolver.set2dVertex(vertex_2d,4); //基準矩形の3D座標系を作成 @@ -237,7 +207,7 @@ public class NyARTransMat implements INyARTransMat this.updateMatrixValue(this._rotmatrix, this._offset.point, trans,o_result_conv); }else{ //回転行列を計算 - this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); + this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex); //回転後の3D座標系から、平行移動量を計算 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); diff --git a/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat_ARToolKit.java b/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat_ARToolKit.java index d14d8b0..5bfa314 100644 --- a/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat_ARToolKit.java +++ b/src/jp/nyatla/nyartoolkit/core/transmat/NyARTransMat_ARToolKit.java @@ -34,7 +34,6 @@ import jp.nyatla.nyartoolkit.NyARException; import jp.nyatla.nyartoolkit.core.param.*; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; import jp.nyatla.nyartoolkit.core.transmat.solver.*; -import jp.nyatla.nyartoolkit.core.transmat.optimize.*; import jp.nyatla.nyartoolkit.core.transmat.optimize.artoolkit.INyARRotMatrixOptimize; import jp.nyatla.nyartoolkit.core.transmat.optimize.artoolkit.NyARRotMatrixOptimize_O2; import jp.nyatla.nyartoolkit.core.transmat.rotmatrix.*; @@ -88,65 +87,33 @@ public class NyARTransMat_ARToolKit implements INyARTransMat } - - - /** - * 頂点順序をi_directionに対応して並べ替えます。 - * @param i_square - * @param i_direction - * @param o_sqvertex_ref - * @param o_liner_ref - */ - private final void initVertexOrder(NyARSquare i_square, int i_direction, NyARDoublePoint2d[] o_sqvertex_ref, NyARLinear[] o_liner_ref) - { - //頂点順序を考慮した矩形の頂点情報 - o_sqvertex_ref[0]= i_square.sqvertex[(4 - i_direction) % 4]; - o_sqvertex_ref[1]= i_square.sqvertex[(5 - i_direction) % 4]; - o_sqvertex_ref[2]= i_square.sqvertex[(6 - i_direction) % 4]; - o_sqvertex_ref[3]= i_square.sqvertex[(7 - i_direction) % 4]; - o_liner_ref[0]=i_square.line[(4 - i_direction) % 4]; - o_liner_ref[1]=i_square.line[(5 - i_direction) % 4]; - o_liner_ref[2]=i_square.line[(6 - i_direction) % 4]; - o_liner_ref[3]=i_square.line[(7 - i_direction) % 4]; - return; - } - - - private final NyARDoublePoint2d[] __transMat_sqvertex_ref = new NyARDoublePoint2d[4]; private final NyARDoublePoint2d[] __transMat_vertex_2d = NyARDoublePoint2d.createArray(4); private final NyARDoublePoint3d[] __transMat_vertex_3d = NyARDoublePoint3d.createArray(4); - private final NyARLinear[] __transMat_linear_ref=new NyARLinear[4]; private final NyARDoublePoint3d __transMat_trans=new NyARDoublePoint3d(); /** * double arGetTransMat( ARMarkerInfo *marker_info,double center[2], double width, double conv[3][4] ) * * @param i_square * 計算対象のNyARSquareオブジェクト - * @param i_direction * @param i_width * @return * @throws NyARException */ - public void transMat(final NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException + public void transMat(final NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException { - final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; - final NyARLinear[] linear_ref=__transMat_linear_ref; final NyARDoublePoint3d trans=this.__transMat_trans; - //計算用に頂点情報を初期化(順番調整) - initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); - //平行移動量計算機に、2D座標系をセット NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d; NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; - this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4); + this._ref_dist_factor.ideal2ObservBatch(i_square.sqvertex, vertex_2d,4); this._transsolver.set2dVertex(vertex_2d,4); //基準矩形の3D座標系を作成 this._offset.setSquare(i_width,this._center); //回転行列を計算 - this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); + this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex); //回転後の3D座標系から、平行移動量を計算 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); @@ -164,26 +131,20 @@ public class NyARTransMat_ARToolKit implements INyARTransMat * (non-Javadoc) * @see jp.nyatla.nyartoolkit.core.transmat.INyARTransMat#transMatContinue(jp.nyatla.nyartoolkit.core.NyARSquare, int, double, jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult) */ - public void transMatContinue(NyARSquare i_square, int i_direction, double i_width, NyARTransMatResult o_result_conv) throws NyARException + public void transMatContinue(NyARSquare i_square, double i_width, NyARTransMatResult o_result_conv) throws NyARException { - final NyARDoublePoint2d[] sqvertex_ref = __transMat_sqvertex_ref; - final NyARLinear[] linear_ref=__transMat_linear_ref; final NyARDoublePoint3d trans=this.__transMat_trans; // io_result_convが初期値なら、transMatで計算する。 if (!o_result_conv.has_value) { - this.transMat(i_square, i_direction, i_width, o_result_conv); + this.transMat(i_square, i_width, o_result_conv); return; } - - //計算用に頂点情報を初期化(順番調整) - initVertexOrder(i_square, i_direction, sqvertex_ref,linear_ref); - //平行移動量計算機に、2D座標系をセット NyARDoublePoint2d[] vertex_2d=this.__transMat_vertex_2d; NyARDoublePoint3d[] vertex_3d=this.__transMat_vertex_3d; - this._ref_dist_factor.ideal2ObservBatch(sqvertex_ref, vertex_2d,4); + this._ref_dist_factor.ideal2ObservBatch(i_square.sqvertex, vertex_2d,4); this._transsolver.set2dVertex(vertex_2d,4); //基準矩形の3D座標系を作成 @@ -205,7 +166,7 @@ public class NyARTransMat_ARToolKit implements INyARTransMat // エラー値が許容範囲でなければTransMatをやり直し if (err > AR_GET_TRANS_CONT_MAT_MAX_FIT_ERROR) { // rotationを矩形情報で初期化 - this._rotmatrix.initRotBySquare(linear_ref,sqvertex_ref); + this._rotmatrix.initRotBySquare(i_square.line,i_square.sqvertex); //回転行列の平行移動量の計算 this._rotmatrix.getPoint3dBatch(this._offset.vertex,vertex_3d,4); this._transsolver.solveTransportVector(vertex_3d,trans); diff --git a/src/jp/nyatla/nyartoolkit/core/types/NyARHistgram.java b/src/jp/nyatla/nyartoolkit/core/types/NyARHistgram.java index 2e161a4..fd1b553 100644 --- a/src/jp/nyatla/nyartoolkit/core/types/NyARHistgram.java +++ b/src/jp/nyatla/nyartoolkit/core/types/NyARHistgram.java @@ -8,7 +8,7 @@ public class NyARHistgram /** * サンプリング値の格納変数 */ - public int[] data; + public final int[] data; /** * 有効なサンプリング値の範囲。[0-data.length-1] */ @@ -17,6 +17,9 @@ public class NyARHistgram * 有効なサンプルの総数 data[i] */ public int total_of_data; + + + public NyARHistgram(int i_length) { this.data=new int[i_length]; diff --git a/src/jp/nyatla/nyartoolkit/detector/NyARCustomSingleDetectMarker.java b/src/jp/nyatla/nyartoolkit/detector/NyARCustomSingleDetectMarker.java index 00f4ae4..cb26cce 100644 --- a/src/jp/nyatla/nyartoolkit/detector/NyARCustomSingleDetectMarker.java +++ b/src/jp/nyatla/nyartoolkit/detector/NyARCustomSingleDetectMarker.java @@ -39,6 +39,7 @@ import jp.nyatla.nyartoolkit.core.raster.rgb.*; import jp.nyatla.nyartoolkit.core.raster.*; import jp.nyatla.nyartoolkit.core.transmat.*; import jp.nyatla.nyartoolkit.core.types.NyARIntSize; +import jp.nyatla.nyartoolkit.core.types.NyARLinear; import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; import jp.nyatla.nyartoolkit.core.types.*; import jp.nyatla.nyartoolkit.core.squaredetect.*; @@ -51,28 +52,99 @@ import jp.nyatla.nyartoolkit.core.squaredetect.*; * */ public class NyARCustomSingleDetectMarker -{ - private static final int AR_SQUARE_MAX = 100; - +{ + /** + * detectMarkerのコールバック関数 + */ + private class DetectSquareCB implements INyARSquareContourDetector.DetectMarkerCallback + { + //公開プロパティ + public double confidence; + public NyARSquare square=new NyARSquare(); + + //参照インスタンス + private INyARRgbRaster _ref_raster; + //所有インスタンス + private INyARColorPatt _inst_patt; + private NyARMatchPattDeviationColorData _deviation_data; + private NyARMatchPatt_Color_WITHOUT_PCA _match_patt; + private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult(); + private Coord2Linear _coordline; + + public DetectSquareCB(INyARColorPatt i_inst_patt,NyARCode i_ref_code,NyARParam i_param) + { + this._inst_patt=i_inst_patt; + this._deviation_data=new NyARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight()); + this._coordline=new Coord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); + this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code); + return; + } + private NyARIntPoint2d[] __tmp_vertex=NyARIntPoint2d.createArray(4); + /** + * 矩形が見付かるたびに呼び出されます。 + * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 + */ + public void onSquareDetect(INyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException + { + NyARMatchPattResult mr=this.__detectMarkerLite_mr; + //輪郭座標から頂点リストに変換 + NyARIntPoint2d[] vertex=this.__tmp_vertex; + vertex[0].x=i_coordx[i_vertex_index[0]]; + vertex[0].y=i_coordy[i_vertex_index[0]]; + vertex[1].x=i_coordx[i_vertex_index[1]]; + vertex[1].y=i_coordy[i_vertex_index[1]]; + vertex[2].x=i_coordx[i_vertex_index[2]]; + vertex[2].y=i_coordy[i_vertex_index[2]]; + vertex[3].x=i_coordx[i_vertex_index[3]]; + vertex[3].y=i_coordy[i_vertex_index[3]]; + + //画像を取得 + if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ + return; + } + //取得パターンをカラー差分データに変換して評価する。 + this._deviation_data.setRaster(this._inst_patt); + if(!this._match_patt.evaluate(this._deviation_data,mr)){ + return; + } + //現在の一致率より低ければ終了 + if (this.confidence > mr.confidence){ + return; + } + //一致率の高い矩形があれば、方位を考慮して頂点情報を作成 + NyARSquare sq=this.square; + this.confidence = mr.confidence; + //directionを考慮して、squareを更新する。 + for(int i=0;i<4;i++){ + int idx=(i+4 - mr.direction) % 4; + sq.imvertex[i].x=vertex[idx].x; + sq.imvertex[i].y=vertex[idx].y; + this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); + } + for (int i = 0; i < 4; i++) { + //直線同士の交点計算 + if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ + throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK + } + } + } + public final void init(INyARRgbRaster i_raster) + { + this.confidence=0; + this._ref_raster=i_raster; + + } + } + private boolean _is_continue = false; - private NyARMatchPatt_Color_WITHOUT_PCA _match_patt; - private INyARSquareDetector _square_detect; - - private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); - + private INyARSquareContourDetector _square_detect; protected INyARTransMat _transmat; - private double _marker_width; - // 検出結果の保存用 - private int _detected_direction; - private double _detected_confidence; - private NyARSquare _detected_square; - private INyARColorPatt _patt; //画処理用 private NyARBinRaster _bin_raster; protected INyARRasterFilter_RgbToBin _tobin_filter; + private DetectSquareCB _detect_cb; - private NyARMatchPattDeviationColorData _deviation_data; protected NyARCustomSingleDetectMarker() { @@ -80,7 +152,7 @@ public class NyARCustomSingleDetectMarker } protected void initInstance( INyARColorPatt i_patt_inst, - INyARSquareDetector i_sqdetect_inst, + INyARSquareContourDetector i_sqdetect_inst, INyARTransMat i_transmat_inst, INyARRasterFilter_RgbToBin i_filter, NyARParam i_ref_param, @@ -94,20 +166,17 @@ public class NyARCustomSingleDetectMarker this._tobin_filter=i_filter; // 比較コードを保存 this._marker_width = i_marker_width; - //パターンピックアップを作成 - this._patt = i_patt_inst; - //取得パターンの差分データ器を作成 - this._deviation_data=new NyARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight()); - //i_code用の評価器を作成 - this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code); //2値画像バッファを作る this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h); + //_detect_cb + this._detect_cb=new DetectSquareCB(i_patt_inst,i_ref_code,i_ref_param); return; } - private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult(); + + /** * i_imageにマーカー検出処理を実行し、結果を記録します。 * @@ -127,50 +196,14 @@ public class NyARCustomSingleDetectMarker //ラスタを2値イメージに変換する. this._tobin_filter.doFilter(i_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) { + //コールバックハンドラの準備 + this._detect_cb.init(i_raster); + //矩形を探す(戻り値はコールバック関数で受け取る。) + this._square_detect.detectMarkerCB(this._bin_raster,_detect_cb); + if(this._detect_cb.confidence==0){ return false; } - - boolean result=false; - NyARMatchPattResult mr=this.__detectMarkerLite_mr; - int square_index = 0; - int direction = NyARSquare.DIRECTION_UNKNOWN; - double confidence = 0; - for(int i=0;i c2) { - continue; - } - // もっと一致するマーカーがあったぽい - square_index = i; - direction = mr.direction; - confidence = c2; - result=true; - } - - // マーカー情報を保存 - this._detected_square = l_square_list.getItem(square_index); - this._detected_direction = direction; - this._detected_confidence = confidence; - return result; + return true; } /** @@ -185,9 +218,9 @@ public class NyARCustomSingleDetectMarker { // 一番一致したマーカーの位置とかその辺を計算 if (this._is_continue) { - this._transmat.transMatContinue(this._detected_square,this._detected_direction,this._marker_width, o_result); + this._transmat.transMatContinue(this._detect_cb.square,this._marker_width, o_result); } else { - this._transmat.transMat(this._detected_square,this._detected_direction,this._marker_width, o_result); + this._transmat.transMat(this._detect_cb.square,this._marker_width, o_result); } return; } @@ -198,7 +231,7 @@ public class NyARCustomSingleDetectMarker */ public void getSquarePosition(NyARIntPoint2d[] o_point) { - NyARIntPoint2d.copyArray(this._detected_square.imvertex,o_point); + NyARIntPoint2d.copyArray(this._detect_cb.square.imvertex,o_point); return; } /** @@ -209,7 +242,7 @@ public class NyARCustomSingleDetectMarker */ public NyARIntPoint2d[] refSquarePosition() { - return this._detected_square.imvertex; + return this._detect_cb.square.imvertex; } @@ -221,19 +254,8 @@ public class NyARCustomSingleDetectMarker */ public double getConfidence() { - return this._detected_confidence; + return this._detect_cb.confidence; } - - /** - * 検出したマーカーの方位を返します。 - * - * @return 0,1,2,3の何れかを返します。 - */ - public int getDirection() - { - return this._detected_direction; - } - /** * getTransmationMatrixの計算モードを設定します。 初期値はTRUEです。 * @@ -244,4 +266,8 @@ public class NyARCustomSingleDetectMarker { this._is_continue = i_is_continue; } + + + + } diff --git a/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java b/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java index e276a2b..8408b04 100644 --- a/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java +++ b/src/jp/nyatla/nyartoolkit/detector/NyARDetectMarker.java @@ -39,69 +39,160 @@ import jp.nyatla.nyartoolkit.core.raster.*; import jp.nyatla.nyartoolkit.core.raster.rgb.*; import jp.nyatla.nyartoolkit.core.transmat.*; import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*; -import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareDetector; +import jp.nyatla.nyartoolkit.core.squaredetect.Coord2Linear; +import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector; import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare; -import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareDetector_Rle; -import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareStack; +import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle; import jp.nyatla.nyartoolkit.core.types.*; +import jp.nyatla.utils.NyObjectStack; class NyARDetectMarkerResult { public int arcode_id; - - public int direction; - public double confidence; - public NyARSquare ref_square; + public NyARSquare square=new NyARSquare(); } -class NyARDetectMarkerResultHolder -{ - public NyARDetectMarkerResult[] result_array = new NyARDetectMarkerResult[1]; - /** - * result_holderを最大i_reserve_size個の要素を格納できるように予約します。 - * - * @param i_reserve_size - */ - public void reservHolder(int i_reserve_size) +class NyARDetectMarkerResultStack extends NyObjectStack +{ + public NyARDetectMarkerResultStack(int i_length) { - if (i_reserve_size >= result_array.length) { - int new_size = i_reserve_size + 5; - result_array = new NyARDetectMarkerResult[new_size]; - for (int i = 0; i < new_size; i++) { - result_array[i] = new NyARDetectMarkerResult(); - } - } + super(i_length,NyARDetectMarkerResult.class); + return; } + protected NyARDetectMarkerResult createElement() + { + return new NyARDetectMarkerResult(); + } } + /** * 複数のマーカーを検出し、それぞれに最も一致するARコードを、コンストラクタで登録したARコードから 探すクラスです。最大300個を認識しますが、ゴミラベルを認識したりするので100個程度が限界です。 * */ public class NyARDetectMarker { - private static final int AR_SQUARE_MAX = 300; + /** + * detectMarkerのコールバック関数 + */ + private class DetectSquareCB implements INyARSquareContourDetector.DetectMarkerCallback + { + //公開プロパティ + public NyARDetectMarkerResultStack result_stack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX); + //参照インスタンス + public INyARRgbRaster _ref_raster; + //所有インスタンス + private INyARColorPatt _inst_patt; + private NyARMatchPattDeviationColorData _deviation_data; + private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt; + private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult(); + private Coord2Linear _coordline; + + public DetectSquareCB(INyARColorPatt i_inst_patt,NyARCode[] i_ref_code,int i_num_of_code,NyARParam i_param) throws NyARException + { + final int cw = i_ref_code[0].getWidth(); + final int ch = i_ref_code[0].getHeight(); + + this._inst_patt=i_inst_patt; + this._coordline=new Coord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); + this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch); + + //NyARMatchPatt_Color_WITHOUT_PCA[]の作成 + this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_num_of_code]; + this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]); + for (int i = 1; i < i_num_of_code; i++){ + //解像度チェック + if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) { + throw new NyARException(); + } + this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]); + } + return; + } + private NyARIntPoint2d[] __tmp_vertex=NyARIntPoint2d.createArray(4); + /** + * 矩形が見付かるたびに呼び出されます。 + * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 + */ + public void onSquareDetect(INyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException + { + NyARMatchPattResult mr=this.__detectMarkerLite_mr; + //輪郭座標から頂点リストに変換 + NyARIntPoint2d[] vertex=this.__tmp_vertex; + vertex[0].x=i_coordx[i_vertex_index[0]]; + vertex[0].y=i_coordy[i_vertex_index[0]]; + vertex[1].x=i_coordx[i_vertex_index[1]]; + vertex[1].y=i_coordy[i_vertex_index[1]]; + vertex[2].x=i_coordx[i_vertex_index[2]]; + vertex[2].y=i_coordy[i_vertex_index[2]]; + vertex[3].x=i_coordx[i_vertex_index[3]]; + vertex[3].y=i_coordy[i_vertex_index[3]]; + + //画像を取得 + if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ + return; + } + //取得パターンをカラー差分データに変換して評価する。 + this._deviation_data.setRaster(this._inst_patt); + + //最も一致するパターンを割り当てる。 + int square_index,direction; + double confidence; + this._match_patt[0].evaluate(this._deviation_data,mr); + square_index=0; + direction=mr.direction; + confidence=mr.confidence; + //2番目以降 + for(int i=1;i mr.confidence) { + continue; + } + // もっと一致するマーカーがあったぽい + square_index = i; + direction = mr.direction; + confidence = mr.confidence; + } + //最も一致したマーカ情報を、この矩形の情報として記録する。 + final NyARDetectMarkerResult result = this.result_stack.prePush(); + result.arcode_id = square_index; + result.confidence = confidence; + final NyARSquare sq=result.square; + //directionを考慮して、squareを更新する。 + for(int i=0;i<4;i++){ + int idx=(i+4 - direction) % 4; + sq.imvertex[i].x=vertex[idx].x; + sq.imvertex[i].y=vertex[idx].y; + this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); + } + for (int i = 0; i < 4; i++) { + //直線同士の交点計算 + if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ + throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK + } + } + } + public void init(INyARRgbRaster i_raster) + { + this._ref_raster=i_raster; + this.result_stack.clear(); + + } + } + private DetectSquareCB _detect_cb; + + + private static final int AR_SQUARE_MAX = 300; private boolean _is_continue = false; - - private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt; - - private INyARSquareDetector _square_detect; - - private final NyARSquareStack _square_list = new NyARSquareStack(AR_SQUARE_MAX); - + private INyARSquareContourDetector _square_detect; protected INyARTransMat _transmat; - private double[] _marker_width; - // 検出結果の保存用 - private INyARColorPatt _patt; - private NyARDetectMarkerResultHolder _result_holder = new NyARDetectMarkerResultHolder(); - private NyARMatchPattDeviationColorData _deviation_data; /** * 複数のマーカーを検出し、最も一致するARCodeをi_codeから検索するオブジェクトを作ります。 * @@ -134,29 +225,20 @@ public class NyARDetectMarker final NyARIntSize scr_size=i_ref_param.getScreenSize(); // 解析オブジェクトを作る - - this._transmat = new NyARTransMat(i_ref_param); - //各コード用の比較器を作る。 - this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA[i_number_of_code]; final int cw = i_ref_code[0].getWidth(); final int ch = i_ref_code[0].getHeight(); - this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]); - for (int i = 1; i < i_number_of_code; i++){ - //解像度チェック - if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) { - throw new NyARException(); - } - this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]); - } + + //detectMarkerのコールバック関数 + this._detect_cb=new DetectSquareCB( + new NyARColorPatt_Perspective_O2(cw, ch,4,25), + i_ref_code,i_number_of_code,i_ref_param); + this._transmat = new NyARTransMat(i_ref_param); //NyARToolkitプロファイル - this._patt =new NyARColorPatt_Perspective_O2(cw, ch,4,25); - this._square_detect =new NyARSquareDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); + this._square_detect =new NyARSquareContourDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type); //実サイズ保存 this._marker_width = i_marker_width; - //差分データインスタンスの作成 - this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch); //2値画像バッファを作る this._bin_raster=new NyARBinRaster(scr_size.w,scr_size.h); return; @@ -165,7 +247,6 @@ public class NyARDetectMarker private NyARBinRaster _bin_raster; private INyARRasterFilter_RgbToBin _tobin_filter; - private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult(); /** * i_imageにマーカー検出処理を実行し、結果を記録します。 @@ -188,54 +269,12 @@ public class NyARDetectMarker ((NyARRasterFilter_ARToolkitThreshold)this._tobin_filter).setThreshold(i_threshold); this._tobin_filter.doFilter(i_raster, this._bin_raster); - NyARSquareStack l_square_list = this._square_list; - // スクエアコードを探す - this._square_detect.detectMarker(this._bin_raster, l_square_list); - - final int number_of_square = l_square_list.getLength(); - // コードは見つかった? - if (number_of_square < 1) { - // ないや。おしまい。 - return 0; - } - // 保持リストのサイズを調整 - this._result_holder.reservHolder(number_of_square); - NyARMatchPattResult mr=this.__detectMarkerLite_mr; - - // 1スクエア毎に、一致するコードを決定していく - for (int i = 0; i < number_of_square; i++) { - NyARSquare square = (l_square_list.getItem(i)); - - // 評価基準になるパターンをイメージから切り出す - if (!this._patt.pickFromRaster(i_raster, square.imvertex)) { - // イメージの切り出しは失敗することもある。 - continue; - } - //取得パターンをカラー差分データに変換する。 - this._deviation_data.setRaster(this._patt); - int square_index = 0; - int direction = NyARSquare.DIRECTION_UNKNOWN; - double confidence = 0; - for(int i2=0;i2 c2) { - continue; - } - // もっと一致するマーカーがあったぽい - square_index = i2; - direction = mr.direction; - confidence = c2; - } - // i番目のパターン情報を記録する。 - final NyARDetectMarkerResult result = this._result_holder.result_array[i]; - result.arcode_id = square_index; - result.confidence = confidence; - result.direction = direction; - result.ref_square = square; - } - return number_of_square; + //見付かった数を返す。 + return this._detect_cb.result_stack.getLength(); } /** @@ -249,12 +288,12 @@ public class NyARDetectMarker */ public void getTransmationMatrix(int i_index, NyARTransMatResult o_result) throws NyARException { - final NyARDetectMarkerResult result = this._result_holder.result_array[i_index]; + final NyARDetectMarkerResult result = this._detect_cb.result_stack.getItem(i_index); // 一番一致したマーカーの位置とかその辺を計算 if (_is_continue) { - _transmat.transMatContinue(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result); + _transmat.transMatContinue(result.square, _marker_width[result.arcode_id], o_result); } else { - _transmat.transMat(result.ref_square, result.direction, _marker_width[result.arcode_id], o_result); + _transmat.transMat(result.square, _marker_width[result.arcode_id], o_result); } return; } @@ -269,21 +308,8 @@ public class NyARDetectMarker */ public double getConfidence(int i_index) { - return this._result_holder.result_array[i_index].confidence; + return this._detect_cb.result_stack.getItem(i_index).confidence; } - - /** - * i_indexのマーカーの方位を返します。 - * - * @param i_index - * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 - * @return 0,1,2,3の何れかを返します。 - */ - public int getDirection(int i_index) - { - return this._result_holder.result_array[i_index].direction; - } - /** * i_indexのマーカーのARCodeインデックスを返します。 * @@ -293,7 +319,7 @@ public class NyARDetectMarker */ public int getARCodeIndex(int i_index) { - return this._result_holder.result_array[i_index].arcode_id; + return this._detect_cb.result_stack.getItem(i_index).arcode_id; } /** diff --git a/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java b/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java index d9e82e7..c22907c 100644 --- a/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java +++ b/src/jp/nyatla/nyartoolkit/detector/NyARSingleDetectMarker.java @@ -94,23 +94,23 @@ public class NyARSingleDetectMarker extends NyARCustomSingleDetectMarker { final NyARRasterFilter_ARToolkitThreshold th=new NyARRasterFilter_ARToolkitThreshold(100,i_input_raster_type); INyARColorPatt patt_inst; - INyARSquareDetector sqdetect_inst; + INyARSquareContourDetector sqdetect_inst; INyARTransMat transmat_inst; switch(i_profile_id){ case PF_ARTOOLKIT_COMPATIBLE: patt_inst=new NyARColorPatt_O3(i_ref_code.getWidth(), i_ref_code.getHeight()); - sqdetect_inst=new NyARSquareDetector_ARToolKit(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); + sqdetect_inst=new NyARSquareContourDetector_ARToolKit(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); transmat_inst=new NyARTransMat_ARToolKit(i_ref_param); break; case PF_NYARTOOLKIT_ARTOOLKIT_FITTING: patt_inst=new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25); - sqdetect_inst=new NyARSquareDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); + sqdetect_inst=new NyARSquareContourDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); transmat_inst=new NyARTransMat_ARToolKit(i_ref_param); break; case PF_NYARTOOLKIT://default patt_inst=new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(),4,25); - sqdetect_inst=new NyARSquareDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); + sqdetect_inst=new NyARSquareContourDetector_Rle(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); transmat_inst=new NyARTransMat(i_ref_param); break; default: diff --git a/src/jp/nyatla/nyartoolkit/nyidmarker/NyIdMarkerPickup.java b/src/jp/nyatla/nyartoolkit/nyidmarker/NyIdMarkerPickup.java index 84e14fc..af85d7a 100644 --- a/src/jp/nyatla/nyartoolkit/nyidmarker/NyIdMarkerPickup.java +++ b/src/jp/nyatla/nyartoolkit/nyidmarker/NyIdMarkerPickup.java @@ -1043,11 +1043,11 @@ public class NyIdMarkerPickup * @return * @throws NyARException */ - public boolean pickFromRaster(INyARRgbRaster image, NyARSquare i_square,NyIdMarkerPattern o_data,NyIdMarkerParam o_param)throws NyARException + public boolean pickFromRaster(INyARRgbRaster image, NyARIntPoint2d[] i_vertex,NyIdMarkerPattern o_data,NyIdMarkerParam o_param)throws NyARException { //遠近法のパラメータを計算 - if(!this._perspective_reader.setSourceSquare(i_square.imvertex)){ + if(!this._perspective_reader.setSourceSquare(i_vertex)){ return false; }; diff --git a/src/jp/nyatla/nyartoolkit/nyidmarker/data/NyIdMarkerData_RawBit.java b/src/jp/nyatla/nyartoolkit/nyidmarker/data/NyIdMarkerData_RawBit.java index 1e662be..832b432 100644 --- a/src/jp/nyatla/nyartoolkit/nyidmarker/data/NyIdMarkerData_RawBit.java +++ b/src/jp/nyatla/nyartoolkit/nyidmarker/data/NyIdMarkerData_RawBit.java @@ -39,7 +39,7 @@ public class NyIdMarkerData_RawBit implements INyIdMarkerData return false; } for(int i=s.length-1;i>=0;i--){ - if(s.packet[i]!=s.packet[i]){ + if(s.packet[i]!=this.packet[i]){ return false; } } diff --git a/src/jp/nyatla/nyartoolkit/processor/SingleARMarkerProcesser.java b/src/jp/nyatla/nyartoolkit/processor/SingleARMarkerProcesser.java index 374c824..fce51f6 100644 --- a/src/jp/nyatla/nyartoolkit/processor/SingleARMarkerProcesser.java +++ b/src/jp/nyatla/nyartoolkit/processor/SingleARMarkerProcesser.java @@ -51,17 +51,146 @@ import jp.nyatla.nyartoolkit.core.squaredetect.*; */ public abstract class SingleARMarkerProcesser { - /**selectARCodeIndexFromListが値を返す時に使う変数型です。 + /** + * detectMarkerのコールバック関数 */ - - private class TResult_selectARCodeIndex + private class DetectSquareCB implements INyARSquareContourDetector.DetectMarkerCallback { - public int direction; - - public double confidence; + //公開プロパティ + public final NyARSquare square=new NyARSquare(); + public double confidence=0.0; + public int code_index=-1; + public double cf_threshold_new = 0.30; + public double cf_threshold_exist = 0.15; + + //参照 + private INyARRgbRaster _ref_raster; + //所有インスタンス + private INyARColorPatt _inst_patt; + private NyARMatchPattDeviationColorData _deviation_data; + private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt; + private final NyARMatchPattResult __detectMarkerLite_mr=new NyARMatchPattResult(); + private Coord2Linear _coordline; + + public DetectSquareCB(NyARParam i_param) + { + this._match_patt=null; + this._coordline=new Coord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); + return; + } + public void setNyARCodeTable(NyARCode[] i_ref_code,int i_code_resolution) + { + /*unmanagedで実装するときは、ここでリソース解放をすること。*/ + this._deviation_data=new NyARMatchPattDeviationColorData(i_code_resolution,i_code_resolution); + this._inst_patt=new NyARColorPatt_Perspective_O2(i_code_resolution,i_code_resolution,4,25); + this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA[i_ref_code.length]; + for(int i=0;i c1) { + // 一致度が低い。 + return; + } + //認識しているマーカIDを保存 + this.code_index=lcode_index; + }else{ + //現在はマーカ認識中 + // 現在のマーカを認識したか? + if (lcode_index != this.code_index) { + // 認識中のマーカではないので無視 + return; + } + //認識中の閾値より大きいか? + if (c1 < this.cf_threshold_exist) { + return; + } + //現在の候補よりも一致度は大きいか? + if (this.confidence>c1) { + return; + } + } + //新しく認識、または継続認識中に更新があったときだけ、Square情報を更新する。 + //ココから先はこの条件でしか実行されない。 + + //一致率の高い矩形があれば、方位を考慮して頂点情報を作成 + this.confidence=c1; + NyARSquare sq=this.square; + //directionを考慮して、squareを更新する。 + for(int i=0;i<4;i++){ + int idx=(i+4 - dir) % 4; + sq.imvertex[i].x=vertex[idx].x; + sq.imvertex[i].y=vertex[idx].y; + this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); + } + for (int i = 0; i < 4; i++) { + //直線同士の交点計算 + if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ + throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK + } + } + } + } /**オーナーが自由に使えるタグ変数です。 */ public Object tag; @@ -70,21 +199,11 @@ public abstract class SingleARMarkerProcesser private int _lost_delay = 5; - private INyARSquareDetector _square_detect; + private INyARSquareContourDetector _square_detect; protected INyARTransMat _transmat; private double _marker_width; - - private NyARMatchPatt_Color_WITHOUT_PCA[] _match_patt; - - private NyARSquareStack _square_list = new NyARSquareStack(100); - - private INyARColorPatt _patt = null; - - private double _cf_threshold_new = 0.30; - private double _cf_threshold_exist = 0.15; - private int _threshold = 110; // [AR]検出結果の保存用 private NyARBinRaster _bin_raster; @@ -93,7 +212,6 @@ public abstract class SingleARMarkerProcesser protected int _current_arcode_index = -1; - private NyARMatchPattDeviationColorData _deviation_data; private NyARRasterThresholdAnalyzer_SlidePTile _threshold_detect; protected SingleARMarkerProcesser() @@ -110,7 +228,7 @@ public abstract class SingleARMarkerProcesser NyARIntSize scr_size = i_param.getScreenSize(); // 解析オブジェクトを作る - this._square_detect = new NyARSquareDetector_Rle(i_param.getDistortionFactor(), scr_size); + this._square_detect = new NyARSquareContourDetector_Rle(i_param.getDistortionFactor(), scr_size); this._transmat = new NyARTransMat(i_param); this._tobin_filter=new NyARRasterFilter_ARToolkitThreshold(110,i_raster_type); @@ -118,6 +236,9 @@ public abstract class SingleARMarkerProcesser this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h); this._threshold_detect=new NyARRasterThresholdAnalyzer_SlidePTile(15,i_raster_type,4); this._initialized=true; + //コールバックハンドラ + this._detectmarker_cb=new DetectSquareCB(i_param); + return; } @@ -138,14 +259,8 @@ public abstract class SingleARMarkerProcesser reset(true); } //検出するマーカセット、情報、検出器を作り直す。(1ピクセル4ポイントサンプリング,マーカのパターン領域は50%) - this._patt = new NyARColorPatt_Perspective_O2(i_code_resolution, i_code_resolution,4,25); - this._deviation_data=new NyARMatchPattDeviationColorData(i_code_resolution, i_code_resolution); + this._detectmarker_cb.setNyARCodeTable(i_ref_code_table,i_code_resolution); this._marker_width = i_marker_width; - - this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA[i_ref_code_table.length]; - for(int i=0;i