OSDN Git Service

009c3ac435d477dc2b2f602ec72a9dc06e6d582e
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core2 / rasteranalyzer / threshold / NyARRasterThresholdAnalyzer_SlidePTile.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\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
17  * \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
22  * \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
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.core2.rasteranalyzer.threshold;\r
33 \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
38 \r
39 \r
40 /**\r
41  * 明点と暗点をPタイル法で検出して、その中央値を閾値とする。\r
42  * \r
43  * \r
44  */\r
45 public class NyARRasterThresholdAnalyzer_SlidePTile implements INyARRasterThresholdAnalyzer\r
46 {\r
47         interface ICreateHistgramImpl{\r
48                 public int createHistgramImpl(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram);\r
49         }\r
50         /**\r
51          * Glayscale(MAX256)のヒストグラム計算クラス\r
52          */\r
53         final class CreateHistgramImpl_INT1D_GLAY_8 implements ICreateHistgramImpl\r
54         {\r
55                 public int _v_interval;\r
56                 public CreateHistgramImpl_INT1D_GLAY_8(int i_v_interval)\r
57                 {\r
58                         this._v_interval=i_v_interval;\r
59                         return;\r
60                 }\r
61                 public int createHistgramImpl(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram)\r
62                 {\r
63                         int sum=0;\r
64                         final int[] input=(int[]) i_reader.getBuffer();\r
65                         for (int y = i_size.h-1; y >=0 ; y-=this._v_interval){\r
66                                 sum+=i_size.w;\r
67                                 int pt=y*i_size.w;\r
68                                 for (int x = i_size.w-1; x >=0; x--) {\r
69                                         o_histgram[input[pt]]++;\r
70                                         pt++;\r
71                                 }\r
72                         }\r
73                         return sum;\r
74                 }\r
75         }\r
76         final class CreateHistgramImpl_BYTE1D_RGB_24 implements ICreateHistgramImpl\r
77         {\r
78                 private int _v_interval;\r
79                 public CreateHistgramImpl_BYTE1D_RGB_24(int i_v_interval)\r
80                 {\r
81                         this._v_interval=i_v_interval;\r
82                         return;\r
83                 }\r
84                 public int createHistgramImpl(INyARBufferReader i_reader,NyARIntSize i_size, int[] o_histgram)\r
85                 {\r
86                         final byte[] input=(byte[]) i_reader.getBuffer();\r
87                         final int pix_count=i_size.w;\r
88                         final int pix_mod_part=pix_count-(pix_count%8);\r
89                         int sum=0;\r
90                         for (int y = i_size.h-1; y >=0 ; y-=this._v_interval) {\r
91                                 sum+=i_size.w;\r
92                                 int pt=y*i_size.w*3;\r
93                                 int x,v;\r
94                                 for (x = pix_count-1; x >=pix_mod_part; x--) {\r
95                                         v=((input[pt+0]& 0xff)+(input[pt+1]& 0xff)+(input[pt+2]& 0xff))/3;\r
96                                         o_histgram[v]++;\r
97                                         pt+=3;\r
98                                 }\r
99                                 //タイリング\r
100                                 for (;x>=0;x-=8){\r
101                                         v=((input[pt+ 0]& 0xff)+(input[pt+ 1]& 0xff)+(input[pt+ 2]& 0xff))/3;\r
102                                         o_histgram[v]++;\r
103                                         v=((input[pt+ 3]& 0xff)+(input[pt+ 4]& 0xff)+(input[pt+ 5]& 0xff))/3;\r
104                                         o_histgram[v]++;\r
105                                         v=((input[pt+ 6]& 0xff)+(input[pt+ 7]& 0xff)+(input[pt+ 8]& 0xff))/3;\r
106                                         o_histgram[v]++;\r
107                                         v=((input[pt+ 9]& 0xff)+(input[pt+10]& 0xff)+(input[pt+11]& 0xff))/3;\r
108                                         o_histgram[v]++;\r
109                                         v=((input[pt+12]& 0xff)+(input[pt+13]& 0xff)+(input[pt+14]& 0xff))/3;\r
110                                         o_histgram[v]++;\r
111                                         v=((input[pt+15]& 0xff)+(input[pt+16]& 0xff)+(input[pt+17]& 0xff))/3;\r
112                                         o_histgram[v]++;\r
113                                         v=((input[pt+18]& 0xff)+(input[pt+19]& 0xff)+(input[pt+20]& 0xff))/3;\r
114                                         o_histgram[v]++;\r
115                                         v=((input[pt+21]& 0xff)+(input[pt+22]& 0xff)+(input[pt+23]& 0xff))/3;\r
116                                         o_histgram[v]++;\r
117                                         pt+=3*8;\r
118                                 }\r
119                         }\r
120                         return sum;             \r
121                 }\r
122         }\r
123         private int _persentage;\r
124         private int _threshold;\r
125         private ICreateHistgramImpl _histgram;\r
126         \r
127         /**\r
128          * @param i_persentage\r
129          * 0<=50であること。白/黒マーカーの場合は10~20を推奨 正の場合、黒点を基準にします。 負の場合、白点を基準にします。\r
130          * (CMOSカメラの場合、基準点は白点の方が良い)\r
131          */\r
132         public NyARRasterThresholdAnalyzer_SlidePTile(int i_persentage,int i_raster_format,int i_vertical_interval) throws NyARException\r
133         {\r
134                 assert (0 <= i_persentage && i_persentage <= 50);\r
135                 this._persentage = i_persentage;\r
136                 switch(i_raster_format){\r
137                 case INyARBufferReader.BUFFERFORMAT_BYTE1D_B8G8R8_24:\r
138                 case INyARBufferReader.BUFFERFORMAT_BYTE1D_R8G8B8_24:\r
139                         this._histgram=new CreateHistgramImpl_BYTE1D_RGB_24(i_vertical_interval);\r
140                         break;\r
141                 case INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8:\r
142                         this._histgram=new CreateHistgramImpl_INT1D_GLAY_8(i_vertical_interval);\r
143                         break;\r
144                 default:\r
145                         throw new NyARException();\r
146                 }\r
147         }\r
148         public void setVerticalInterval(int i_step)\r
149         {\r
150                 return;//未実装一号\r
151         }\r
152 \r
153         private int[] _histgram_buf=new int[256];\r
154         public void analyzeRaster(INyARRaster i_input) throws NyARException\r
155         {\r
156                 final INyARBufferReader buffer_reader=i_input.getBufferReader();        \r
157                 assert (buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8));\r
158 \r
159                 final int[] histgram = this._histgram_buf;\r
160                 final NyARIntSize size=i_input.getSize();\r
161                 \r
162                 //最大画像サイズの制限\r
163                 assert size.w*size.h<0x40000000;\r
164 \r
165                 //ヒストグラム初期化\r
166                 for (int i = 0; i < 256; i++) {\r
167                         histgram[i] = 0;\r
168                 }\r
169                 int sum_of_pixel=this._histgram.createHistgramImpl(i_input.getBufferReader(), size, histgram);\r
170 \r
171                 // 閾値ピクセル数確定\r
172                 final int th_pixcels = sum_of_pixel * this._persentage / 100;\r
173                 int th_wk;\r
174                 int th_w, th_b;\r
175 \r
176                 // 黒点基準\r
177                 th_wk = th_pixcels;\r
178                 for (th_b = 0; th_b < 254; th_b++) {\r
179                         th_wk -= histgram[th_b];\r
180                         if (th_wk <= 0) {\r
181                                 break;\r
182                         }\r
183                 }\r
184                 // 白点基準\r
185                 th_wk = th_pixcels;\r
186                 for (th_w = 255; th_w > 1; th_w--) {\r
187                         th_wk -= histgram[th_w];\r
188                         if (th_wk <= 0) {\r
189                                 break;\r
190                         }\r
191                 }\r
192                 // 閾値の保存\r
193                 this._threshold = (th_w + th_b) / 2;\r
194                 return;\r
195         }\r
196         public int getThreshold()\r
197         {\r
198                 return this._threshold;\r
199         }\r
200 \r
201         public int getThreshold(int i_x, int i_y)\r
202         {\r
203                 return this._threshold;\r
204         }\r
205 }\r