OSDN Git Service

[更新]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core2 / rasteranalyzer / threshold / NyARRasterThresholdAnalyzer_DiffHistgram.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  * \r
42  */\r
43 public class NyARRasterThresholdAnalyzer_DiffHistgram implements INyARRasterThresholdAnalyzer\r
44 {\r
45         private int _threshold;\r
46 \r
47         public NyARRasterThresholdAnalyzer_DiffHistgram()\r
48         {\r
49         }\r
50 \r
51         private int createHistgram(int[] in_buf,NyARIntSize i_size, int[] o_histgram) throws NyARException\r
52         {\r
53                 int[][] fil1={\r
54                                 {-1,-2,-1},\r
55                                 { 0, 0, 0},\r
56                                 { 1, 2, 1}};\r
57 \r
58                 // ヒストグラムを作成\r
59                 for (int i = 0; i < 256; i++) {\r
60                         o_histgram[i] = 0;\r
61                 }\r
62                 int sam;\r
63                 int sam1,sam2;\r
64                 for (int y = 1; y < i_size.h-1; y++) {\r
65                         for (int x = 1; x < i_size.w-1; x++) {\r
66                                 int v = in_buf[y* i_size.w+x];\r
67                                 sam1=sam2=0;\r
68                                 for(int yy=0;yy<3;yy++){\r
69                                         for(int xx=0;xx<3;xx++){\r
70                                                 int v2=in_buf[(y+yy-1)* i_size.w+(x+xx-1)];\r
71                                                 sam1+=v2*fil1[xx][yy];\r
72                                                 sam2+=v2*fil1[yy][xx];\r
73                                         }                                       \r
74                                 }\r
75                                 sam=sam1*sam1+sam2*sam2;\r
76                                 o_histgram[v]+=sam;\r
77                         }\r
78                 }\r
79                 int th=0;\r
80                 int max=o_histgram[0];\r
81                 for(int i=1;i<256;i++){\r
82                         if(max<o_histgram[i]){\r
83                                 th=i;\r
84                                 max=o_histgram[i];\r
85                         }\r
86                 }\r
87                 return th;\r
88         }\r
89 \r
90         public void analyzeRaster(INyARRaster i_input) throws NyARException\r
91         {\r
92                 final INyARBufferReader buffer_reader=i_input.getBufferReader();        \r
93                 assert (buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8));\r
94                 int[] histgram = new int[256];\r
95                 this._threshold = createHistgram((int[])buffer_reader.getBuffer(),i_input.getSize(), histgram);\r
96         }\r
97 \r
98         /**\r
99          * デバック用の関数です。 ヒストグラムをラスタに書き出します。\r
100          * \r
101          * @param i_output\r
102          * 書き出し先のラスタオブジェクト 256ピクセル以上の幅があること。\r
103          */\r
104         public void debugDrawHistgramMap(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
105         {\r
106                 INyARBufferReader in_buffer_reader=i_input.getBufferReader();   \r
107                 INyARBufferReader out_buffer_reader=i_output.getBufferReader(); \r
108                 assert (in_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8));\r
109                 assert (out_buffer_reader.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GLAY_8));\r
110                 NyARIntSize size = i_output.getSize();\r
111 \r
112                 int[] out_buf = (int[]) out_buffer_reader.getBuffer();\r
113                 // 0で塗りつぶし\r
114                 for (int y = 0; y < size.h; y++) {\r
115                         for (int x = 0; x < size.w; x++) {\r
116                                 out_buf[y* size.w+x] = 0;\r
117                         }\r
118                 }\r
119                 // ヒストグラムを計算\r
120                 int[] histgram = new int[256];\r
121                 int threshold = createHistgram((int[])in_buffer_reader.getBuffer(),i_input.getSize(), histgram);\r
122 \r
123                 // ヒストグラムの最大値を出す\r
124                 int max_v = 0;\r
125                 for (int i = 0; i < 255; i++) {\r
126                         if (max_v < histgram[i]) {\r
127                                 max_v = histgram[i];\r
128                         }\r
129                 }\r
130                 // 目盛り\r
131                 for (int i = 0; i < size.h; i++) {\r
132                         out_buf[i* size.w+0] = 128;\r
133                         out_buf[i* size.w+128] = 128;\r
134                         out_buf[i* size.w+255] = 128;\r
135                 }\r
136                 // スケーリングしながら描画\r
137                 for (int i = 0; i < 255; i++) {\r
138                         out_buf[(histgram[i] * (size.h - 1) / max_v)* size.w+i] = 255;\r
139                 }\r
140                 // 値\r
141                 for (int i = 0; i < size.h; i++) {\r
142                         out_buf[i* size.w+threshold] = 255;\r
143                 }\r
144                 return;\r
145         }\r
146 \r
147         public int getThreshold()\r
148         {\r
149                 return this._threshold;\r
150         }\r
151 \r
152         public int getThreshold(int i_x, int i_y)\r
153         {\r
154                 return this._threshold;\r
155         }\r
156 }\r