1. 程式人生 > >積分圖演算法簡介

積分圖演算法簡介

原文來自https://blog.csdn.net/jia20003/article/details/52710751

影象處理之積分圖演算法

一:積分圖來源與發展

積分影象是Crow在1984年首次提出,是為了在多尺度透視投影中提高渲染速度。隨後這種技術被應用到基於NCC的快速匹配、物件檢測和SURF變換中、基於統計學的快速濾波器等方面。積分影象是一種在影象中快速計算矩形區域和的方法,這種演算法主要優點是一旦積分影象首先被計算出來我們可以計算影象中任意大小矩形區域的和而且是在常量時間內。這樣在影象模糊、邊緣提取、物件檢測的時候極大降低計算量、提高計算速度。第一個應用積分影象技術的應用是在Viola-Jones的物件檢測框架中出現。

二:積分影象概念

在積分影象(Integral Image - ii)上任意位置(x, y)處的ii(x, y)表示該點左上角所有畫素之和,表示如下:



從給定影象I從上到下、從左到右計算得到和的積分影象公式如下:


其中(x<0 || y<0) 時ii(x,y)=0, i(x,y)=0

得到積分影象之後,影象中任意矩形區域和通過如下公式計算:



三:程式碼實現:

積分影象演算法的Java程式碼實現如下:

[java] view plain copy print
?
  1. package com.gloomyfish.ii.demo;  
  2.   
  3. public class IntIntegralImage extends AbstractByteProcessor {  
  4.     // sum index tables  
  5.     private
     int[] sum;  
  6.     private int[] squaresum;  
  7.     // image  
  8.     private byte[] image;  
  9.     private int width;  
  10.     private int height;  
  11.   
  12.     public byte[] getImage() {  
  13.         return image;  
  14.     }  
  15.   
  16.     public void setImage(byte[] image) {  
  17.         this.image = image;  
  18.     }  
  19.       
  20.     public int getBlockSum(int x, int y, int m, int n) {  
  21.         int swx = x + n/2;  
  22.         int swy = y + m/2;  
  23.         int nex = x-n/2-1;  
  24.         int ney = y-m/2-1;  
  25.         int sum1, sum2, sum3, sum4;  
  26.         if(swx >= width) {  
  27.             swx = width - 1;  
  28.         }  
  29.         if(swy >= height) {  
  30.             swy = height - 1;  
  31.         }  
  32.         if(nex < 0) {  
  33.             nex = 0;  
  34.         }  
  35.         if(ney < 0) {  
  36.             ney = 0;  
  37.         }  
  38.         sum1 = sum[ney*width+nex];  
  39.         sum4 = sum[swy*width+swx];  
  40.         sum2 = sum[swy*width+nex];  
  41.         sum3 = sum[ney*width+swx];  
  42.         return ((sum1 + sum4) - sum2 - sum3);  
  43.     }  
  44.       
  45.     public int getBlockSquareSum(int x, int y, int m, int n) {        
  46.         int swx = x + n/2;  
  47.         int swy = y + m/2;  
  48.         int nex = x-n/2-1;  
  49.         int ney = y-m/2-1;  
  50.         int sum1, sum2, sum3, sum4;  
  51.         if(swx >= width) {  
  52.             swx = width - 1;  
  53.         }  
  54.         if(swy >= height) {  
  55.             swy = height - 1;  
  56.         }  
  57.         if(nex < 0) {  
  58.             nex = 0;  
  59.         }  
  60.         if(ney < 0) {  
  61.             ney = 0;  
  62.         }  
  63.         sum1 = squaresum[ney*width+nex];  
  64.         sum4 = squaresum[swy*width+swx];  
  65.         sum2 = squaresum[swy*width+nex];  
  66.         sum3 = squaresum[ney*width+swx];  
  67.         return ((sum1 + sum4) - sum2 - sum3);  
  68.     }  
  69.   
  70.     @Override  
  71.     public void process(int width, int height) {  
  72.         this.width = width;  
  73.         this.height = height;  
  74.         sum = new int[width*height];  
  75.         squaresum = new int[width*height];  
  76.         // rows  
  77.         int p1=0, p2=0, p3=0, p4;  
  78.         int offset = 0, uprow=0, leftcol=0;  
  79.         int s=0;  
  80.         for(int row=0; row<height; row++ ) {  
  81.             offset = row*width;  
  82.             uprow = row-1;  
  83.             for(int col=0; col<width; col++) {  
  84.                 leftcol=col-1;  
  85.                 p1=image[offset]&0xff;// p(x, y)  
  86.                 p2=(leftcol<0) ? 0:sum[offset-1]; // p(x-1, y)  
  87.                 p3=(uprow<0) ? 0:sum[offset-width]; // p(x, y-1);  
  88.                 p4=(uprow<0||leftcol<0) ? 0:sum[offset-width-1]; // p(x-1, y-1);  
  89.                 s = sum[offset]= p1+p2+p3-p4;  
  90.                 squaresum[offset]=s*s;  
  91.                 // System.out.print(“\t[“ + offset+”]=” + s);  
  92.                 offset++;  
  93.             }  
  94.             // System.out.println();  
  95.         }  
  96.     }  
  97.       
  98.     public static void main(String[] args) {  
  99.         byte[] data = new byte[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};  
  100.         IntIntegralImage ii = new IntIntegralImage();  
  101.         ii.setImage(data);  
  102.         ii.process(73);  
  103.           
  104.         int sum = ii.getBlockSum(3233);  
  105.         System.out.println(”sum = ” + sum);  
  106.     }  
  107. }  
package com.gloomyfish.ii.demo;

public class IntIntegralImage extends AbstractByteProcessor {
    // sum index tables
    private int[] sum;
    private int[] squaresum;
    // image
    private byte[] image;
    private int width;
    private int height;

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public int getBlockSum(int x, int y, int m, int n) {
        int swx = x + n/2;
        int swy = y + m/2;
        int nex = x-n/2-1;
        int ney = y-m/2-1;
        int sum1, sum2, sum3, sum4;
        if(swx >= width) {
            swx = width - 1;
        }
        if(swy >= height) {
            swy = height - 1;
        }
        if(nex < 0) {
            nex = 0;
        }
        if(ney < 0) {
            ney = 0;
        }
        sum1 = sum[ney*width+nex];
        sum4 = sum[swy*width+swx];
        sum2 = sum[swy*width+nex];
        sum3 = sum[ney*width+swx];
        return ((sum1 + sum4) - sum2 - sum3);
    }

    public int getBlockSquareSum(int x, int y, int m, int n) {      
        int swx = x + n/2;
        int swy = y + m/2;
        int nex = x-n/2-1;
        int ney = y-m/2-1;
        int sum1, sum2, sum3, sum4;
        if(swx >= width) {
            swx = width - 1;
        }
        if(swy >= height) {
            swy = height - 1;
        }
        if(nex < 0) {
            nex = 0;
        }
        if(ney < 0) {
            ney = 0;
        }
        sum1 = squaresum[ney*width+nex];
        sum4 = squaresum[swy*width+swx];
        sum2 = squaresum[swy*width+nex];
        sum3 = squaresum[ney*width+swx];
        return ((sum1 + sum4) - sum2 - sum3);
    }

    @Override
    public void process(int width, int height) {
        this.width = width;
        this.height = height;
        sum = new int[width*height];
        squaresum = new int[width*height];
        // rows
        int p1=0, p2=0, p3=0, p4;
        int offset = 0, uprow=0, leftcol=0;
        int s=0;
        for(int row=0; row<height; row++ ) {
            offset = row*width;
            uprow = row-1;
            for(int col=0; col<width; col++) {
                leftcol=col-1;
                p1=image[offset]&0xff;// p(x, y)
                p2=(leftcol<0) ? 0:sum[offset-1]; // p(x-1, y)
                p3=(uprow<0) ? 0:sum[offset-width]; // p(x, y-1);
                p4=(uprow<0||leftcol<0) ? 0:sum[offset-width-1]; // p(x-1, y-1);
                s = sum[offset]= p1+p2+p3-p4;
                squaresum[offset]=s*s;
                // System.out.print("\t[" + offset+"]=" + s);
                offset++;
            }
            // System.out.println();
        }
    }

    public static void main(String[] args) {
        byte[] data = new byte[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        IntIntegralImage ii = new IntIntegralImage();
        ii.setImage(data);
        ii.process(7, 3);

        int sum = ii.getBlockSum(3, 2, 3, 3);
        System.out.println("sum = " + sum);
    }
}

後續應用相關博文會陸續出爐!