OSDN Git Service

[backup]NyARToolkit for Java
authornyatla <nyatla@7cac0a50-4618-4814-88d0-24b83990f816>
Wed, 25 Nov 2009 16:07:01 +0000 (16:07 +0000)
committernyatla <nyatla@7cac0a50-4618-4814-88d0-24b83990f816>
Wed, 25 Nov 2009 16:07:01 +0000 (16:07 +0000)
画像処理フィルタをいくつか追加
いくつかのクラスを更新

git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit@345 7cac0a50-4618-4814-88d0-24b83990f816

trunk/src/jp/nyatla/nyartoolkit/core/analyzer/raster/NyARRasterAnalyzer_Histgram.java
trunk/src/jp/nyatla/nyartoolkit/core/raster/NyARGrayscaleRaster.java
trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_CustomToneTable.java [new file with mode: 0644]
trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_EqualizeHist.java [new file with mode: 0644]
trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_GaussianSmooth.java [new file with mode: 0644]
trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_ToneTable.java [new file with mode: 0644]
trunk/src/jp/nyatla/nyartoolkit/core/rasterreader/INyARBufferReader.java
trunk/src/jp/nyatla/nyartoolkit/core/types/NyARHistgram.java
trunk/src/jp/nyatla/nyartoolkit/sample/RawFileTest.java
trunk/src/jp/nyatla/utils/j2se/NyARRasterImageIO.java

index 920cb8d..0e3650e 100644 (file)
@@ -73,7 +73,7 @@ public class NyARRasterAnalyzer_Histgram
                for (int i = o_histgram.length-1; i >=0; i--){\r
                        h[i] = 0;\r
                }\r
-               o_histgram.total_of_data=size.w*size.h;\r
+               o_histgram.total_of_data=size.w*size.h/this._vertical_skip;\r
                return this._histImpl.createHistgram(i_input.getBufferReader(), size,h,this._vertical_skip);            \r
        }\r
        \r
index 5b75e9c..2a50800 100644 (file)
@@ -30,6 +30,7 @@
  */\r
 package jp.nyatla.nyartoolkit.core.raster;\r
 \r
+import jp.nyatla.nyartoolkit.NyARException;\r
 import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;\r
 import jp.nyatla.nyartoolkit.core.rasterreader.NyARBufferReader;\r
 import jp.nyatla.nyartoolkit.core.types.*;\r
@@ -91,5 +92,14 @@ public final class NyARGrayscaleRaster extends NyARRaster_BasicClass
                int f=buf[idx_p1+1];\r
                o_v.x=buf[idx_0+1]-buf[idx_0-1]+(d-b+f-h)/2;\r
                o_v.y=buf[idx_p1]-buf[idx_m1]+(f-d+h-b)/2;\r
+       }\r
+       \r
+       public void copyFrom(NyARGrayscaleRaster i_input) throws NyARException\r
+       {\r
+               int[] out_buf = (int[])this._ref_buf;\r
+               int[] in_buf = (int[]) i_input._ref_buf;\r
+               System.arraycopy(in_buf,0,out_buf,0,this._size.h*this._size.w);\r
+               return;\r
        }       \r
+       \r
 }\r
diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_CustomToneTable.java b/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_CustomToneTable.java
new file mode 100644 (file)
index 0000000..800fe7a
--- /dev/null
@@ -0,0 +1,78 @@
+/* \r
+ * PROJECT: NyARToolkit(Extension)\r
+ * --------------------------------------------------------------------------------\r
+ * The NyARToolkit is Java edition ARToolKit class library.\r
+ * Copyright (C)2008-2009 Ryo Iizuka\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ * \r
+ * For further information please contact.\r
+ *     http://nyatla.jp/nyatoolkit/\r
+ *     <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.core.rasterfilter;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.raster.INyARRaster;\r
+import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;\r
+import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
+\r
+/**\r
+ * 色調テーブルを使用したフィルターです。\r
+ * 色調テーブルクラスのベースクラスです。\r
+ */\r
+public class NyARRasterFilter_CustomToneTable implements INyARRasterFilter\r
+{\r
+       protected final int[] table=new int[256];\r
+       private IdoFilterImpl _dofilterimpl;\r
+       protected NyARRasterFilter_CustomToneTable(int i_raster_type) throws NyARException\r
+       {\r
+               switch (i_raster_type) {\r
+               case INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8:\r
+                       this._dofilterimpl=new IdoFilterImpl_INT1D_GRAY_8();\r
+                       break;\r
+               default:\r
+                       throw new NyARException();\r
+               }\r
+               this._dofilterimpl._table_ref=this.table;\r
+       }\r
+       public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
+       {\r
+               assert (i_input.getSize().isEqualSize(i_output.getSize()) == true);\r
+               this._dofilterimpl.doFilter(i_input.getBufferReader(),i_output.getBufferReader(),i_input.getSize());\r
+       }\r
+       \r
+       abstract class IdoFilterImpl\r
+       {\r
+               int[] _table_ref;\r
+               public abstract void doFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size) throws NyARException;\r
+               \r
+       }\r
+       class IdoFilterImpl_INT1D_GRAY_8 extends IdoFilterImpl\r
+       {\r
+               public void doFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size) throws NyARException\r
+               {\r
+                       assert(         i_input.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8));\r
+                       \r
+                       int[] out_buf = (int[]) i_output.getBuffer();\r
+                       int[] in_buf = (int[]) i_input.getBuffer();\r
+                       for(int i=i_size.h*i_size.w-1;i>=0;i--)\r
+                       {\r
+                               out_buf[i]=this._table_ref[in_buf[i]];\r
+                       }\r
+                       return;\r
+               }\r
+       }\r
+}\r
diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_EqualizeHist.java b/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_EqualizeHist.java
new file mode 100644 (file)
index 0000000..bf5e43c
--- /dev/null
@@ -0,0 +1,38 @@
+package jp.nyatla.nyartoolkit.core.rasterfilter;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.analyzer.raster.NyARRasterAnalyzer_Histgram;\r
+import jp.nyatla.nyartoolkit.core.raster.INyARRaster;\r
+import jp.nyatla.nyartoolkit.core.types.NyARHistgram;\r
+/**\r
+ * ヒストグラムを平滑化します。\r
+ *\r
+ */\r
+public class NyARRasterFilter_EqualizeHist extends NyARRasterFilter_CustomToneTable\r
+{\r
+       private NyARRasterAnalyzer_Histgram _hist_analyzer;\r
+       private NyARHistgram _histgram=new NyARHistgram(256);\r
+       public NyARRasterFilter_EqualizeHist(int i_raster_type,int i_sample_interval) throws NyARException\r
+       {\r
+               super(i_raster_type);\r
+               this._hist_analyzer=new NyARRasterAnalyzer_Histgram(i_raster_type,i_sample_interval);\r
+       }\r
+       public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
+       {\r
+               assert (i_input!=i_output);\r
+               //ヒストグラムを得る\r
+               NyARHistgram hist=this._histgram;\r
+               this._hist_analyzer.analyzeRaster(i_input,hist);\r
+               //変換テーブルを作成\r
+               int hist_total=this._histgram.total_of_data;\r
+               int min=hist.getMinData();\r
+               int hist_size=this._histgram.length;\r
+               int sum=0;\r
+               for(int i=0;i<hist_size;i++){\r
+                       sum+=hist.data[i];\r
+                       this.table[i]=(int)((sum-min)*(hist_size-1)/((hist_total-min)));\r
+               }\r
+               //変換\r
+               super.doFilter(i_input, i_output);\r
+       }\r
+}
\ No newline at end of file
diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_GaussianSmooth.java b/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_GaussianSmooth.java
new file mode 100644 (file)
index 0000000..c3c46c5
--- /dev/null
@@ -0,0 +1,99 @@
+package jp.nyatla.nyartoolkit.core.rasterfilter;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.raster.INyARRaster;\r
+import jp.nyatla.nyartoolkit.core.rasterreader.INyARBufferReader;\r
+import jp.nyatla.nyartoolkit.core.types.NyARIntSize;\r
+\r
+\r
+/**\r
+ * 平滑化フィルタ\r
+ * Gaussianフィルタで画像を平滑化します。\r
+ * カーネルサイズは3x3です。\r
+ */\r
+public class NyARRasterFilter_GaussianSmooth implements INyARRasterFilter\r
+{\r
+       private IdoFilterImpl _do_filter_impl; \r
+       public NyARRasterFilter_GaussianSmooth(int i_raster_type) throws NyARException\r
+       {\r
+               switch (i_raster_type) {\r
+               case INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8:\r
+                       this._do_filter_impl=new IdoFilterImpl_GRAY_8();\r
+                       break;\r
+               default:\r
+                       throw new NyARException();\r
+               }\r
+       }\r
+       public void doFilter(INyARRaster i_input, INyARRaster i_output) throws NyARException\r
+       {\r
+               assert (i_input!=i_output);\r
+               this._do_filter_impl.doFilter(i_input.getBufferReader(),i_output.getBufferReader(),i_input.getSize());\r
+       }\r
+       \r
+       interface IdoFilterImpl\r
+       {\r
+               public void doFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size) throws NyARException;\r
+       }\r
+       class IdoFilterImpl_GRAY_8 implements IdoFilterImpl\r
+       {\r
+               public void doFilter(INyARBufferReader i_input, INyARBufferReader i_output,NyARIntSize i_size) throws NyARException\r
+               {\r
+                       assert (i_input.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8));\r
+                       assert (i_output.isEqualBufferType(INyARBufferReader.BUFFERFORMAT_INT1D_GRAY_8));\r
+                       int[] in_ptr =(int[])i_input.getBuffer();\r
+                       int[] out_ptr=(int[])i_output.getBuffer();\r
+                       int width=i_size.w;\r
+                       int height=i_size.h;\r
+                       int col0,col1,col2;\r
+                       int bptr=0;\r
+                       //1行目\r
+                       col1=in_ptr[bptr  ]*2+in_ptr[bptr+width  ];\r
+                       col2=in_ptr[bptr+1]*2+in_ptr[bptr+width+1];\r
+                       out_ptr[bptr]=(col1*2+col2)/9;\r
+                       bptr++;\r
+                       for(int x=0;x<width-2;x++){\r
+                               col0=col1;\r
+                               col1=col2;\r
+                               col2=in_ptr[bptr+1]*2+in_ptr[bptr+width+1];\r
+                               out_ptr[bptr]=(col0+col1*2+col2)/12;\r
+                               bptr++;\r
+                       }                       \r
+                       out_ptr[bptr]=(col1+col2)/9;\r
+                       bptr++;\r
+                       //2行目-末行-1\r
+\r
+                       for(int y=0;y<height-2;y++){\r
+                               //左端\r
+                               col1=in_ptr[bptr  ]*2+in_ptr[bptr-width  ]+in_ptr[bptr+width  ];\r
+                               col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1]+in_ptr[bptr+width+1];\r
+                               out_ptr[bptr]=(col1+col2)/12;\r
+                               bptr++;\r
+                               for(int x=0;x<width-2;x++){\r
+                                       col0=col1;\r
+                                       col1=col2;\r
+                                       col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1]+in_ptr[bptr+width+1];\r
+                                       out_ptr[bptr]=(col0+col1*2+col2)/16;\r
+                                       bptr++;\r
+                               }\r
+                               //右端\r
+                               out_ptr[bptr]=(col1*2+col2)/12;\r
+                               bptr++;\r
+                       }\r
+                       //末行目\r
+                       col1=in_ptr[bptr  ]*2+in_ptr[bptr-width  ];\r
+                       col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1];\r
+                       out_ptr[bptr]=(col1+col2)/9;\r
+                       bptr++;\r
+                       for(int x=0;x<width-2;x++){\r
+                               col0=col1;\r
+                               col1=col2;\r
+                               col2=in_ptr[bptr+1]*2+in_ptr[bptr-width+1];\r
+                               out_ptr[bptr]=(col0+col1*2+col2)/12;\r
+                               bptr++;\r
+                       }                       \r
+                       out_ptr[bptr]=(col1*2+col2)/9;\r
+                       bptr++;\r
+                       return;\r
+               }\r
+       }\r
+}
\ No newline at end of file
diff --git a/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_ToneTable.java b/trunk/src/jp/nyatla/nyartoolkit/core/rasterfilter/NyARRasterFilter_ToneTable.java
new file mode 100644 (file)
index 0000000..bc46d3b
--- /dev/null
@@ -0,0 +1,101 @@
+/* \r
+ * PROJECT: NyARToolkit(Extension)\r
+ * --------------------------------------------------------------------------------\r
+ * The NyARToolkit is Java edition ARToolKit class library.\r
+ * Copyright (C)2008-2009 Ryo Iizuka\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ * \r
+ * For further information please contact.\r
+ *     http://nyatla.jp/nyatoolkit/\r
+ *     <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.core.rasterfilter;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+\r
+/**\r
+ * 色調テーブルを使用したフィルターです。\r
+ * 基本的な関数テーブルで色調テーブルを作成できます。\r
+ */\r
+public class NyARRasterFilter_ToneTable extends NyARRasterFilter_CustomToneTable\r
+{\r
+       public NyARRasterFilter_ToneTable(int i_raster_type) throws NyARException\r
+       {\r
+               super(i_raster_type);\r
+       }\r
+       /**\r
+        * 点x,yを通過する、傾きi_aの直線をテーブルに書き込みます。\r
+        * @param i_x\r
+        * @param i_y\r
+        * @param i_a\r
+        */\r
+       public void setLine(int i_x,int i_y,double i_a)\r
+       {\r
+               if(i_a==0){\r
+                       int i;\r
+                       for(i=0;i<=i_x;i++){\r
+                               this.table[i]=0;\r
+                       }\r
+                       for(i=0;i<256;i++){\r
+                               this.table[i]=255;\r
+                       }\r
+               }else{\r
+                       int b=i_y-(int)(i_a*i_x);\r
+                       for(int i=0;i<256;i++){\r
+                               int v=(int)(i_a*i)+b;\r
+                               this.table[i]=v<0?0:v>255?255:v;\r
+                       }\r
+               }\r
+       }\r
+       /**\r
+        * 点0,0を通過する、傾きaの直線をテーブルに書き込みます。\r
+        * i_aの値をvとしたとき、以下のようになります。\r
+        * v<=0         黒色\r
+        * 0<v<1        暗くする。\r
+        * v=0          変化しない\r
+        * 1<v<255      明るくする。\r
+        * 255<=v       白色\r
+        * @param i_ax\r
+        * @param i_ay\r
+        */\r
+       public void setLine(double i_a)\r
+       {\r
+               setLine(0,0,i_a);\r
+       }\r
+       /**\r
+        * 点 i_x,i_yを中心とする、ゲインi_gainのシグモイド関数をテーブルに書き込みます。\r
+        * @param i_x\r
+        * @param i_y\r
+        * @param i_gain\r
+        */\r
+       public void setSigmoid(int i_x,int i_y,double i_gain)\r
+       {\r
+               for(int i=0;i<256;i++){\r
+                       int v=255*(int)(1/(1+Math.exp(i_gain*(i-i_x)))-0.5)+i_y;\r
+                       this.table[i]=v<0?0:v>255?255:v;\r
+               }\r
+       }\r
+       /**\r
+        * ガンマ補正値をテーブルに書き込みます。\r
+        * @param i_gamma\r
+        */\r
+       public void setGamma(double i_gamma)\r
+       {\r
+               for(int i=0;i<256;i++){\r
+                       this.table[i]=(int)(Math.pow((double)i/255.0,i_gamma)*255.0);\r
+               }\r
+       }\r
+}\r
index fe1a444..ca0fddd 100644 (file)
@@ -109,13 +109,18 @@ public interface INyARBufferReader
         * これは、階調1bitのBUFFERFORMAT_INT1D_GRAY_1と同じです。\r
         */\r
        public static final int BUFFERFORMAT_INT1D_BIN_8  = INT1D|0x0002;\r
-\r
+       \r
        \r
        /**\r
         * int[]で、XRGB32の32ビットで画素が格納されている。\r
         */     \r
-    public static final int BUFFERFORMAT_INT1D_X8R8G8B8_32 = INT1D|0x0102;\r
-       \r
+    public static final int BUFFERFORMAT_INT1D_X8R8G8B8_32=INT1D|0x0102;\r
+\r
+       /**\r
+        * H:9bit(0-359),S:8bit(0-255),V(0-255)\r
+        */\r
+       public static final int BUFFERFORMAT_INT1D_X7H9S8V8_32=INT1D|0x0103;\r
+    \r
 \r
        /**\r
         * バッファオブジェクトを返します。\r
index 78f16e4..2e161a4 100644 (file)
@@ -1,7 +1,7 @@
 package jp.nyatla.nyartoolkit.core.types;\r
 \r
 /**\r
- * 順データ型\r
+ * ヒストグラムを格納するクラスです。\r
  */\r
 public class NyARHistgram\r
 {\r
@@ -65,5 +65,43 @@ public class NyARHistgram
                }\r
                this.total_of_data-=s;\r
        }\r
+       /**\r
+        * 最小の値が格納されているサンプル番号を返します。\r
+        */\r
+       public int getMinSample()\r
+       {\r
+               int[] data=this.data;\r
+               int ret=this.length-1;\r
+               int min=data[ret];\r
+               for(int i=this.length-2;i>=0;i--)\r
+               {\r
+                       if(data[i]<min){\r
+                               min=data[i];\r
+                               ret=i;\r
+                       }\r
+               }\r
+               return ret;\r
+       }\r
+       /**\r
+        * サンプルの中で最小の値を返します。\r
+        * @return\r
+        */\r
+       public int getMinData()\r
+       {\r
+               return this.data[this.getMinSample()];\r
+       }\r
+       /**\r
+        * 平均値を計算します。\r
+        * @return\r
+        */\r
+       public int getAverage()\r
+       {\r
+               long sum=0;\r
+               for(int i=this.length-1;i>=0;i--)\r
+               {\r
+                       sum+=this.data[i]*i;\r
+               }\r
+               return (int)(sum/this.total_of_data);\r
+       }\r
        \r
 }\r
index 815dd54..dd231b0 100644 (file)
@@ -79,7 +79,7 @@ public class RawFileTest
 \r
                // 1パターンのみを追跡するクラスを作成\r
                NyARSingleDetectMarker ar = new NyARSingleDetectMarker(\r
-                               ap, code, 80.0,ra.getBufferReader().getBufferType());\r
+                               ap, code, 80.0,ra.getBufferReader().getBufferType(),NyARSingleDetectMarker.PF_NYARTOOLKIT);\r
                NyARTransMatResult result_mat = new NyARTransMatResult();\r
                ar.setContinueMode(false);\r
                ar.detectMarkerLite(ra, 100);\r
index 2144c70..31af7af 100644 (file)
@@ -114,7 +114,7 @@ public class NyARRasterImageIO
         * @param o_out\r
         * @throws NyARException\r
         */\r
-       public static void copy(NyARHistgram i_in,Graphics i_g,int i_l,int i_t,int i_h) throws NyARException\r
+       public static void copy(NyARHistgram i_in,int i_l,int i_t,int i_h,Graphics i_g) throws NyARException\r
        {\r
                //正規化のための定数を出す。\r
                int max=0;\r