1. 程式人生 > >影象處理之積分圖應用一(半徑無關的快速模糊演算法)

影象處理之積分圖應用一(半徑無關的快速模糊演算法)

影象處理之積分影象應用一(半徑無關的快速模糊演算法)

一:基本原理概述

傳統的影象空間域卷積模糊演算法,當視窗大小改變時卷積模糊時間也會變化,而且隨著視窗尺寸越大計算量也越大,演算法執行時間約越長。在很多時候無法滿足實時性要求。而基於積分影象可以實現對視窗區域和大小的快速計算,把傳統卷積模糊計算受視窗大小影響消除,把卷積模糊變成一個與視窗大小半徑無關的常量時間完成的操作。關於如何從影象本身得到積分影象的演算法請看上一篇文章《影象處理之積分影象演算法》

二:詳細解釋

以5x5的視窗大小為例,假設影象I、積分影象II、處理之後模糊影象BI、則傳統空間域卷積實現的影象均值模糊對每個畫素點公式表示如下:



基於積分影象計算每個畫素點模糊公式表示如下:

上述基於傳統的均值模糊計算得到模糊之後的結果要計算24次加法和一次除法共計25次計算,而基於積分影象則只需要一次加法兩次減法和一次除法共計四次計算,而且基於傳統卷積均值模糊計算當視窗大小越大計算次數也越多,而基於積分影象則計算次數保持常量不變,是一個半徑無關的均值模糊演算法。

三:程式碼實現

積分影象演算法實現參見:http://blog.csdn.net/jia20003/article/details/52710751

傳統模式的卷積模糊程式碼如下:


  
  1. package
    com.gloomyfish.ii.demo;
  2. import java.awt.image.BufferedImage;
  3. public class Convolution2DFilter extends AbstractImageOptionFilter
    {
  4. // 視窗半徑大小
  5. private int xr;
  6. private int yr;
  7. public Convolution2DFilter() {
  8. xr = 1;
  9. yr = 1;
  10. }
  11. public int getXr() {
  12. return xr;
  13. }
  14. public void setXr(int xr) {
  15. this.xr = xr;
  16. }
  17. public int getYr() {
  18. return yr;
  19. }
  20. public void setYr(int yr) {
  21. this.yr = yr;
  22. }
  23. @Override
  24. public BufferedImage process(BufferedImage image) {
  25. long time = System.currentTimeMillis();
  26. int width = image.getWidth();
  27. int height = image.getHeight();
  28. int[] pixels = new int[width * height];
  29. int[] outPixels = new int[width * height];
  30. getRGB(image, 0, 0, width, height, pixels);
  31. int size = (xr * 2 + 1) * (yr * 2 + 1);
  32. int r = 0, g = 0, b = 0;
  33. for ( int row = yr; row < height - yr; row++) {
  34. for ( int col = xr; col < width - xr; col++) {
  35. int sr = 0, sg = 0, sb = 0;
  36. // 鍗風Н錼嶄綔/妯℃澘璁$畻
  37. for ( int i = -yr; i <= yr; i++) {
  38. int roffset = row + i;
  39. for ( int j = -xr; j <= xr; j++) {
  40. int coffset = col + j;
  41. sr += ((pixels[roffset * width + coffset] >> 16) & 0xff);
  42. sg += (pixels[roffset * width + coffset] >> 8) & 0xff;
  43. sb += (pixels[roffset * width + coffset] & 0xff);
  44. }
  45. }
  46. r = sr / size;
  47. g = sg / size;
  48. b = sb / size;
  49. outPixels[row * width + col] = ( 0xff << 24) | (r << 16) | (g << 8) | b;
  50. }
  51. }
  52. System.out.println( "Convolution2DFilter ->> time duration : " + (System.currentTimeMillis() - time));
  53. BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  54. setRGB(dest, 0, 0, width, height, outPixels);
  55. return dest;
  56. }
  57. }
基於積分影象的快速模糊程式碼如下:


  
  1. package com.gloomyfish.ii.demo;
  2. import java.awt.image.BufferedImage;
  3. public class FastBlurFilter extends AbstractImageOptionFilter {
  4. // 視窗半徑大小
  5. private int xr;
  6. private int yr;
  7. public FastBlurFilter() {
  8. xr = 1;
  9. yr = 1;
  10. }
  11. public int getXr() {
  12. return xr;
  13. }
  14. public void setXr(int xr) {
  15. this.xr = xr;
  16. }
  17. public int getYr() {
  18. return yr;
  19. }
  20. public void setYr(int yr) {
  21. this.yr = yr;
  22. }
  23. @Override
  24. public BufferedImage process(BufferedImage image) {
  25. long time = System.currentTimeMillis();
  26. int width = image.getWidth();
  27. int height = image.getHeight();
  28. // get image data
  29. int[] pixels = new int[width * height];
  30. int[] outPixels = new int[width * height];
  31. getRGB(image, 0, 0, width, height, pixels);
  32. int size = (xr * 2 + 1) * (yr * 2 + 1);
  33. int r = 0, g = 0, b = 0;
  34. // per-calculate integral image
  35. byte[] R = new byte[width*height];
  36. byte[] G = new byte[width*height];
  37. byte[] B = new byte[width*height];
  38. getRGB(width, height, pixels, R, G, B);
  39. IntIntegralImage rii = new IntIntegralImage();
  40. rii.setImage(R);
  41. rii.process(width, height);
  42. IntIntegralImage gii = new IntIntegralImage();
  43. gii.setImage(G);
  44. gii.process(width, height);
  45. IntIntegralImage bii = new IntIntegralImage();
  46. bii.setImage(B);
  47. bii.process(width, height);
  48. for ( int row = yr; row < height - yr; row++) {
  49. for ( int col = xr; col < width - xr; col++) {
  50. int sr = rii.getBlockSum(col, row, (yr * 2 + 1), (xr * 2 + 1));
  51. int sg = gii.getBlockSum(col, row, (yr * 2 + 1), (xr * 2 + 1));
  52. int sb = bii.getBlockSum(col, row, (yr * 2 + 1), (xr * 2 + 1));
  53. r = sr / size;
  54. g = sg / size;
  55. b = sb / size;
  56. outPixels[row * width + col] = ( 0xff << 24) | (r << 16) | (g << 8) | b;
  57. }
  58. }
  59. System.out.println( "FastBlurFilter ->> time duration : " + (System.currentTimeMillis() - time));
  60. BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  61. setRGB(dest, 0, 0, width, height, outPixels);
  62. return dest;
  63. }
  64. /** Returns the red, green and blue planes as 3 byte arrays. */
  65. public void getRGB(int width, int height, int[] pixels, byte[] R, byte[] G, byte[] B) {
  66. int c, r, g, b;
  67. for ( int i= 0; i < width*height; i++) {
  68. c = pixels[i];
  69. r = (c& 0xff0000)>> 16;
  70. g = (c& 0xff00)>> 8;
  71. b = c& 0xff;
  72. R[i] = ( byte)r;
  73. G[i] = ( byte)g;
  74. B[i] = ( byte)b;
  75. }
  76. }
  77. }

四:效率之比

分別把視窗半徑調整到1、3、10、20的情況下,對同一張影象做模糊處理,CPU耗時直方圖如下:


可見在其它條件不改變的情況下,視窗半徑越大,兩者之間執行時間差距越大。各位國慶節快樂!

轉載自:https://blog.csdn.net/jia20003/article/details/52719832