2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * This work is based on the original ARToolKit developed by
\r
7 * HITLab, University of Washington, Seattle
\r
8 * http://www.hitl.washington.edu/artoolkit/
\r
10 * The NyARToolkit is Java version ARToolkit class library.
\r
11 * Copyright (C)2008 R.Iizuka
\r
13 * This program is free software; you can redistribute it and/or
\r
14 * modify it under the terms of the GNU General Public License
\r
15 * as published by the Free Software Foundation; either version 2
\r
16 * of the License, or (at your option) any later version.
\r
18 * This program is distributed in the hope that it will be useful,
\r
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
21 * GNU General Public License for more details.
\r
23 * You should have received a copy of the GNU General Public License
\r
24 * along with this framework; if not, write to the Free Software
\r
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
27 * For further information please contact.
\r
28 * http://nyatla.jp/nyatoolkit/
\r
29 * <airmail(at)ebony.plala.or.jp>
\r
32 package jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold;
\r
34 import jp.nyatla.nyartoolkit.NyARException;
\r
35 import jp.nyatla.nyartoolkit.core.raster.*;
\r
36 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;
\r
37 import jp.nyatla.nyartoolkit.core.types.*;
\r
41 * 明点と暗点をPタイル法で検出して、その中央値を閾値とする。
\r
45 public class NyARRasterThresholdAnalyzer_SlidePTile implements INyARRasterThresholdAnalyzer
\r
47 interface ICreateHistgramImpl{
\r
48 public int createHistgramImpl(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram);
\r
51 * Glayscale(MAX256)のヒストグラム計算クラス
\r
53 final class CreateHistgramImpl_INT1D_GLAY_8 implements ICreateHistgramImpl
\r
55 public int _v_interval;
\r
56 public CreateHistgramImpl_INT1D_GLAY_8(int i_v_interval)
\r
58 this._v_interval=i_v_interval;
\r
61 public int createHistgramImpl(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram)
\r
63 assert (i_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8));
\r
66 final int[] input=(int[]) i_reader.getBuffer();
\r
67 for (int y = i_size.h-1; y >=0 ; y-=this._v_interval){
\r
70 for (int x = i_size.w-1; x >=0; x--) {
\r
71 o_histgram[input[pt]]++;
\r
79 * RGB24bitのヒストグラム計算クラス
\r
82 final class CreateHistgramImpl_BYTE1D_RGB_24 implements ICreateHistgramImpl
\r
84 private int _v_interval;
\r
85 public CreateHistgramImpl_BYTE1D_RGB_24(int i_v_interval)
\r
87 this._v_interval=i_v_interval;
\r
90 public int createHistgramImpl(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram)
\r
93 i_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24)||
\r
94 i_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24));
\r
96 final byte[] input=(byte[]) i_reader.getBuffer();
\r
97 final int pix_count=i_size.w;
\r
98 final int pix_mod_part=pix_count-(pix_count%8);
\r
100 for (int y = i_size.h-1; y >=0 ; y-=this._v_interval) {
\r
102 int pt=y*i_size.w*3;
\r
104 for (x = pix_count-1; x >=pix_mod_part; x--) {
\r
105 v=((input[pt+0]& 0xff)+(input[pt+1]& 0xff)+(input[pt+2]& 0xff))/3;
\r
111 v=((input[pt+ 0]& 0xff)+(input[pt+ 1]& 0xff)+(input[pt+ 2]& 0xff))/3;
\r
113 v=((input[pt+ 3]& 0xff)+(input[pt+ 4]& 0xff)+(input[pt+ 5]& 0xff))/3;
\r
115 v=((input[pt+ 6]& 0xff)+(input[pt+ 7]& 0xff)+(input[pt+ 8]& 0xff))/3;
\r
117 v=((input[pt+ 9]& 0xff)+(input[pt+10]& 0xff)+(input[pt+11]& 0xff))/3;
\r
119 v=((input[pt+12]& 0xff)+(input[pt+13]& 0xff)+(input[pt+14]& 0xff))/3;
\r
121 v=((input[pt+15]& 0xff)+(input[pt+16]& 0xff)+(input[pt+17]& 0xff))/3;
\r
123 v=((input[pt+18]& 0xff)+(input[pt+19]& 0xff)+(input[pt+20]& 0xff))/3;
\r
125 v=((input[pt+21]& 0xff)+(input[pt+22]& 0xff)+(input[pt+23]& 0xff))/3;
\r
134 * RGBX32bitのヒストグラム計算クラス
\r
137 final class CreateHistgramImpl_BYTE1D_B8G8R8X8_32 implements ICreateHistgramImpl
\r
139 private int _v_interval;
\r
140 public CreateHistgramImpl_BYTE1D_B8G8R8X8_32(int i_v_interval)
\r
142 this._v_interval = i_v_interval;
\r
145 public int createHistgramImpl(INyARBufferReader i_reader, NyARIntSize i_size, int[] o_histgram)
\r
147 assert(i_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32));
\r
148 byte[] input = (byte[])i_reader.getBuffer();
\r
149 int pix_count = i_size.w;
\r
150 int pix_mod_part = pix_count - (pix_count % 8);
\r
152 for (int y = i_size.h - 1; y >= 0; y -= this._v_interval)
\r
155 int pt = y * i_size.w * 3;
\r
157 for (x = pix_count - 1; x >= pix_mod_part; x--)
\r
159 v = ((input[pt + 0] & 0xff) + (input[pt + 1] & 0xff) + (input[pt + 2] & 0xff)) / 3;
\r
164 for (; x >= 0; x -= 8)
\r
166 v = ((input[pt + 0] & 0xff) + (input[pt + 1] & 0xff) + (input[pt + 2] & 0xff)) / 3;
\r
168 v = ((input[pt + 4] & 0xff) + (input[pt + 5] & 0xff) + (input[pt + 6] & 0xff)) / 3;
\r
170 v = ((input[pt + 8] & 0xff) + (input[pt + 9] & 0xff) + (input[pt + 10] & 0xff)) / 3;
\r
172 v = ((input[pt + 12] & 0xff) + (input[pt + 13] & 0xff) + (input[pt + 14] & 0xff)) / 3;
\r
174 v = ((input[pt + 16] & 0xff) + (input[pt + 17] & 0xff) + (input[pt + 18] & 0xff)) / 3;
\r
176 v = ((input[pt + 20] & 0xff) + (input[pt + 21] & 0xff) + (input[pt + 22] & 0xff)) / 3;
\r
178 v = ((input[pt + 24] & 0xff) + (input[pt + 25] & 0xff) + (input[pt + 26] & 0xff)) / 3;
\r
180 v = ((input[pt + 28] & 0xff) + (input[pt + 29] & 0xff) + (input[pt + 30] & 0xff)) / 3;
\r
188 private int _persentage;
\r
189 private int _threshold;
\r
190 private ICreateHistgramImpl _histgram;
\r
193 * @param i_persentage
\r
194 * 0<=50であること。白/黒マーカーの場合は10~20を推奨 正の場合、黒点を基準にします。 負の場合、白点を基準にします。
\r
195 * (CMOSカメラの場合、基準点は白点の方が良い)
\r
197 public NyARRasterThresholdAnalyzer_SlidePTile(int i_persentage,int i_raster_format,int i_vertical_interval) throws NyARException
\r
199 assert (0 <= i_persentage && i_persentage <= 50);
\r
200 this._persentage = i_persentage;
\r
201 switch (i_raster_format)
\r
203 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:
\r
204 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:
\r
205 this._histgram = new CreateHistgramImpl_BYTE1D_RGB_24(i_vertical_interval);
\r
207 case INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8:
\r
208 this._histgram = new CreateHistgramImpl_INT1D_GLAY_8(i_vertical_interval);
\r
210 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8X8_32:
\r
211 this._histgram = new CreateHistgramImpl_BYTE1D_B8G8R8X8_32(i_vertical_interval);
\r
214 throw new NyARException();
\r
217 public void setVerticalInterval(int i_step)
\r
222 private int[] _histgram_buf=new int[256];
\r
223 public void analyzeRaster(INyARRaster i_input) throws NyARException
\r
225 final int[] histgram = this._histgram_buf;
\r
226 final NyARIntSize size=i_input.getSize();
\r
229 assert size.w*size.h<0x40000000;
\r
232 for (int i = 0; i < 256; i++) {
\r
235 int sum_of_pixel=this._histgram.createHistgramImpl(i_input.getBufferReader(), size, histgram);
\r
238 final int th_pixcels = sum_of_pixel * this._persentage / 100;
\r
243 th_wk = th_pixcels;
\r
244 for (th_b = 0; th_b < 254; th_b++) {
\r
245 th_wk -= histgram[th_b];
\r
251 th_wk = th_pixcels;
\r
252 for (th_w = 255; th_w > 1; th_w--) {
\r
253 th_wk -= histgram[th_w];
\r
259 this._threshold = (th_w + th_b) / 2;
\r
262 public int getThreshold()
\r
264 return this._threshold;
\r
267 public int getThreshold(int i_x, int i_y)
\r
269 return this._threshold;
\r