积分图算法简介

原文来自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代码实现如下:

  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);
    }
}

后续应用相关博文会陆续出炉!

猜你喜欢

转载自blog.csdn.net/bflong/article/details/80446419