图像处理之积分图算法

图像处理之积分图算法

一:积分图来源与发展

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

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

转载自:https://blog.csdn.net/jia20003/article/details/52710751

猜你喜欢

转载自blog.csdn.net/baidu_38172402/article/details/83691215