图像处理滤波器(六)——频率滤波器(Frequency Filter)

描述:频率滤波器是一种常见的滤波器,它是基于Fourier 变换演变过来的,它主要分为高通滤波器和低通滤波器。所谓高通滤波器就是只通过高频,滤除低频。反之低通滤波器就是滤除高频,保留低频。


现举例一个常见的频率滤波器——FFT Filter


Code:


  /** 
   * Constructor to set up an FFT object and then automatically 
   * apply the FFT algorithm.
   *
   * @param pixels  int array containing the image data.
   * @param w  The width of the image in pixels.
   * @param h  The height of the image in pixels.
   */
  public FFT(int [] pixels, int w, int h){
    input = new TwoDArray(pixels,w,h);
    intermediate = new TwoDArray(pixels,w,h);
    output = new TwoDArray(pixels,w,h);
    transform();
  }
  
  /**
   * Method to recursively apply the 1D FFT to a ComplexNumber array.
   *
   * @param  x  A ComplexNumber array containing a row or a column of
   * image data.
   * @return A ComplexNumber array containing the result of the 1D FFT.
   */
  static ComplexNumber [] recursiveFFT (ComplexNumber [] x){
    ComplexNumber z1,z2,z3,z4,tmp,cTwo;
    int n = x.length;
    int m = n/2;
    ComplexNumber [] result = new ComplexNumber [n];
    ComplexNumber [] even = new ComplexNumber [m];
    ComplexNumber [] odd = new ComplexNumber [m];
    ComplexNumber [] sum = new ComplexNumber [m];
    ComplexNumber [] diff = new ComplexNumber [m];
    cTwo = new ComplexNumber(2,0);
    if(n==1){
      result[0] = x[0];
    }else{
      z1 = new ComplexNumber(0.0, -2*(Math.PI)/n);
      tmp = ComplexNumber.cExp(z1);
      z1 = new ComplexNumber(1.0, 0.0);
      for(int i=0;i<m;++i){
	z3 = ComplexNumber.cSum(x[i],x[i+m]);
	sum[i] = ComplexNumber.cDiv(z3,cTwo);
	
	z3 = ComplexNumber.cDif(x[i],x[i+m]);
	z4 = ComplexNumber.cMult(z3,z1);
	diff[i] = ComplexNumber.cDiv(z4,cTwo);
	
	z2 = ComplexNumber.cMult(z1,tmp);
	z1 = new ComplexNumber(z2);
      }
      even = recursiveFFT(sum);
      odd = recursiveFFT(diff);
      
      for(int i=0;i<m;++i){
	result[i*2] = new ComplexNumber(even[i]);
	result[i*2 + 1] = new ComplexNumber(odd[i]);
      }
    }
    return result;
  }
   
  /**
   * Method to apply the 2D FFT by applying the recursive 1D FFT to the
   * columns and then the rows of image data.
   */ 
  void transform(){
      
      for(int i=0;i<input.size;++i){
	  intermediate.putColumn(i, recursiveFFT(input.getColumn(i)));
      }
      for(int i=0;i<intermediate.size;++i){
	  output.putRow(i, recursiveFFT(intermediate.getRow(i))); 
      }
      for(int j=0;j<output.values.length;++j){
	  for(int i=0;i<output.values[0].length;++i){
	      intermediate.values[i][j] = output.values[i][j];
	      input.values[i][j] = output.values[i][j];
	  }
      }
  }
}


Input Image:

图像处理滤波器(六)——频率滤波器(Frequency Filter)_第1张图片


Output Image:


1/3 cut-off frequency

图像处理滤波器(六)——频率滤波器(Frequency Filter)_第2张图片


1/2 cut-off frequency

图像处理滤波器(六)——频率滤波器(Frequency Filter)_第3张图片


总结:在此只举例一个低通滤波器的情形,如果在图像处理中仅仅使用低通滤波器来处理噪声是远远不够的,低通滤波器不仅处理了噪声中得高频信号也处理了图像有用信息的高频信号。因此,有待于发展后续更加的精细的处理工作。

你可能感兴趣的:(image,filter,Constructor,output,图像处理,fft)