2017年2月17日金曜日

ヒストグラム均一化フィルタ(ToneEqualizationFilter)

今回はヒストグラムを均一化するフィルタを作成します。ヒストグラム均一化では、ヒストグラムの累積度数(輝度値0から画素数を累積したもの)のグラフの傾きが一定になるように変換する処理です。こうすることによって、コントラストが悪かったり、明るさが偏っている画像の全体的なバランスを改善することが可能になります。
http://www.mis.med.akita-u.ac.jp/~kata/image/equalize.html

  Tutorial1Activityクラスと同じ場所にToneEqualizationFilterクラスを作成します。その後、filterメソッドに以下のコードを入力します。
    public int[] filter(int[] imageArray, int width, int height) {
        int tone[][]=ToneCalculator(imageArray);//①
        int length = width*height;
        int tonemap[][]=new int[3][256];
        tonemap[0]=tonemapping(tone[0],length);//②
        tonemap[1]=tonemapping(tone[1],length);
        tonemap[2]=tonemapping(tone[2],length);
        int[] oimgArray = new int[imageArray.length];
        int rgb;
        int alpha,red,green,blue;
        for(int i =0;i < length;i++){
            rgb = imageArray[i];
            red = (rgb >> 16) & 0xff;
            green = (rgb >> 8) & 0xff;
            blue = (rgb) & 0xff;
            oimgArray[i]=255*16777216+
                    tonemap[0][red]*65536+tonemap[1][green]*256+tonemap[2][blue];
        }
        return oimgArray;
    }

 ①は前回同様に画像のヒストグラムを計算します。以下にメソッドを記載します。
   public int[][] ToneCalculator(int[] imageArray){
        int tone[][]=new int[3][256];
        int length = imageArray.length;
        int rgb;
        int red,green,blue;
        for(int i =0;i < length;i++){
            rgb = imageArray[i];
            red = (rgb >> 16) & 0xff;
            green = (rgb >> 8) & 0xff;
            blue = (rgb) & 0xff;
            tone[0][red]+=1;
            tone[1][green]+=1;
            tone[2][blue]+=1;
        }
        return tone;
    }

 ②はヒストグラムの均一化を行います。以下にメソッドを記載します。
   public int[] tonemapping(int tone[], int all_pixel){
        int cumulative=0;
        int tonemap[]=new int[256];
        for(int i =0;i < 256;i++){
            cumulative+=tone[i];//③
            tonemap[i]=(cumulative*255)/all_pixel;//④
        }
        return tonemap;
    }
③はヒストグラムを左から累積した値を示します。
 ④では累積した値を全画素数で割ることで、現在の色の値(0~255)がどの値にマッピングされるかを計算します。

 最後に、 Tutorial1Activityクラスのint imageArray[]=ba2ia(ba);の下に以下の文を記入します。
imageArray=new ToneEqualizationFilter().filter(imageArray, imagewidth, imageheight);

以下に実機での実行結果を示します。ヒストグラムの棒が長いほど、棒の左側に間隔が空くことがわかります。



0 件のコメント:

コメントを投稿