OSDN Git Service

[backup]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / trunk / sample / sandbox / jp / nyatla / nyartoolkit / core / rasteranalyzer / threshold / NyARRasterThresholdAnalyzer_DiffHistgram.java
1 /* \r
2  * PROJECT: NyARToolkit(Extension)\r
3  * --------------------------------------------------------------------------------\r
4  * The NyARToolkit is Java edition ARToolKit class library.\r
5  * Copyright (C)2008-2009 Ryo Iizuka\r
6  *\r
7  * This program is free software: you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation, either version 3 of the License, or\r
10  * (at your option) any later version.\r
11  * \r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19  * \r
20  * For further information please contact.\r
21  *      http://nyatla.jp/nyatoolkit/\r
22  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
23  * \r
24  */\r
25 package jp.nyatla.nyartoolkit.core.rasteranalyzer.threshold;\r
26 \r
27 import jp.nyatla.nyartoolkit.NyARException;\r
28 import jp.nyatla.nyartoolkit.core.analyzer.raster.threshold.INyARRasterThresholdAnalyzer;\r
29 import jp.nyatla.nyartoolkit.core.raster.*;\r
30 import jp.nyatla.nyartoolkit.core.types.*;\r
31 \r
32 /**\r
33  * 微分ヒストグラム法による閾値検出\r
34  * \r
35  */\r
36 public class NyARRasterThresholdAnalyzer_DiffHistgram implements INyARRasterThresholdAnalyzer\r
37 {\r
38         private int _threshold;\r
39 \r
40         public NyARRasterThresholdAnalyzer_DiffHistgram()\r
41         {\r
42         }\r
43 \r
44         private int createHistgram(int[] in_buf,NyARIntSize i_size, int[] o_histgram) throws NyARException\r
45         {\r
46                 int[][] fil1={\r
47                                 {-1,-2,-1},\r
48                                 { 0, 0, 0},\r
49                                 { 1, 2, 1}};\r
50 \r
51                 // ヒストグラムを作成\r
52                 for (int i = 0; i < 256; i++) {\r
53                         o_histgram[i] = 0;\r
54                 }\r
55                 int sam;\r
56                 int sam1,sam2;\r
57                 for (int y = 1; y < i_size.h-1; y++) {\r
58                         for (int x = 1; x < i_size.w-1; x++) {\r
59                                 int v = in_buf[y* i_size.w+x];\r
60                                 sam1=sam2=0;\r
61                                 for(int yy=0;yy<3;yy++){\r
62                                         for(int xx=0;xx<3;xx++){\r
63                                                 int v2=in_buf[(y+yy-1)* i_size.w+(x+xx-1)];\r
64                                                 sam1+=v2*fil1[xx][yy];\r
65                                                 sam2+=v2*fil1[yy][xx];\r
66                                         }                                       \r
67                                 }\r
68                                 sam=sam1*sam1+sam2*sam2;\r
69                                 o_histgram[v]+=sam;\r
70                         }\r
71                 }\r
72                 int th=0;\r
73                 int max=o_histgram[0];\r
74                 for(int i=1;i<256;i++){\r
75                         if(max<o_histgram[i]){\r
76                                 th=i;\r
77                                 max=o_histgram[i];\r
78                         }\r
79                 }\r
80                 return th;\r
81         }\r
82 \r
83         public int analyzeRaster(INyARRaster i_input) throws NyARException\r
84         {\r
85                 assert (i_input.isEqualBufferType(INyARRaster.BUFFERFORMAT_INT1D_GRAY_8));\r
86                 int[] histgram = new int[256];\r
87                 return createHistgram((int[])i_input.getBuffer(),i_input.getSize(), histgram);\r
88         }\r
89 \r
90         /**\r
91          * デバック用の関数です。 ヒストグラムをラスタに書き出します。\r
92          * \r
93          * @param i_output\r
94          * 書き出し先のラスタオブジェクト 256ピクセル以上の幅があること。\r
95          */\r
96         public void debugDrawHistgramMap(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
97         {\r
98                 assert (i_input.isEqualBufferType(INyARRaster.BUFFERFORMAT_INT1D_GRAY_8));\r
99                 assert (i_output.isEqualBufferType(INyARRaster.BUFFERFORMAT_INT1D_GRAY_8));\r
100                 NyARIntSize size = i_output.getSize();\r
101 \r
102                 int[] out_buf = (int[]) i_output.getBuffer();\r
103                 // 0で塗りつぶし\r
104                 for (int y = 0; y < size.h; y++) {\r
105                         for (int x = 0; x < size.w; x++) {\r
106                                 out_buf[y* size.w+x] = 0;\r
107                         }\r
108                 }\r
109                 // ヒストグラムを計算\r
110                 int[] histgram = new int[256];\r
111                 int threshold = createHistgram((int[])i_input.getBuffer(),i_input.getSize(), histgram);\r
112 \r
113                 // ヒストグラムの最大値を出す\r
114                 int max_v = 0;\r
115                 for (int i = 0; i < 255; i++) {\r
116                         if (max_v < histgram[i]) {\r
117                                 max_v = histgram[i];\r
118                         }\r
119                 }\r
120                 // 目盛り\r
121                 for (int i = 0; i < size.h; i++) {\r
122                         out_buf[i* size.w+0] = 128;\r
123                         out_buf[i* size.w+128] = 128;\r
124                         out_buf[i* size.w+255] = 128;\r
125                 }\r
126                 // スケーリングしながら描画\r
127                 for (int i = 0; i < 255; i++) {\r
128                         out_buf[(histgram[i] * (size.h - 1) / max_v)* size.w+i] = 255;\r
129                 }\r
130                 // 値\r
131                 for (int i = 0; i < size.h; i++) {\r
132                         out_buf[i* size.w+threshold] = 255;\r
133                 }\r
134                 return;\r
135         }\r
136 \r
137         public int getThreshold()\r
138         {\r
139                 return this._threshold;\r
140         }\r
141 }\r