1 /** 2 * Copyright (c) 2011, The University of Southampton and the individual contributors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * * Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * * Neither the name of the University of Southampton nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package org.openimaj.image.processing.convolution; 31 32 import org.openimaj.image.FImage; 33 import org.openimaj.image.processor.SinglebandImageProcessor; 34 35 import edu.emory.mathcs.jtransforms.fft.FloatFFT_2D; 36 37 /** 38 * From the matlab implementation of DISCGAUSSFFT which uses an FFT to apply a gaussian kernel. 39 * The matlab docs: 40 * 41 % DISCGAUSSFFT(pic, sigma2) -- Convolves an image by the 42 % (separable) discrete analogue of the Gaussian kernel by 43 % performing the convolution in the Fourier domain. 44 % The parameter SIGMA2 is the variance of the kernel. 45 46 % Reference: Lindeberg "Scale-space theory in computer vision", Kluwer, 1994. 47 * 48 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 49 * 50 */ 51 public class FDiscGausConvolve implements SinglebandImageProcessor<Float, FImage> { 52 private float sigma2; 53 54 /** 55 * Construct with given variance 56 * @param sigma2 variance of the kernel 57 */ 58 public FDiscGausConvolve(float sigma2) { 59 this.sigma2 = sigma2; 60 // this.fft = new FastFourierTransformer(); 61 } 62 63 @Override 64 public void processImage(FImage image) { 65 int cs = image.getCols(); 66 int rs = image.getRows(); 67 FloatFFT_2D fft = new FloatFFT_2D(rs,cs); 68 float[][] prepared = new float[rs][cs*2]; 69 for(int r = 0; r < rs ; r++){ 70 for(int c = 0; c < cs; c++){ 71 prepared[r][c*2] = image.pixels[r][c]; 72 prepared[r][1 + c*2] = 0; 73 } 74 } 75 fft.complexForward(prepared); 76 for(int y = 0; y < rs; y++){ 77 for(int x = 0; x < cs; x++){ 78 double xcos = Math.cos(2 * Math.PI * ((float)x/cs)); 79 double ycos = Math.cos(2 * Math.PI * ((float)y/rs)); 80 float multiply = (float) Math.exp(sigma2 * (xcos + ycos - 2)); 81 prepared[y][x*2] = prepared[y][x*2] * multiply; 82 prepared[y][1 + x*2] = prepared[y][1 + x*2] * multiply; 83 } 84 } 85 fft.complexInverse(prepared, true); 86 for(int r = 0; r < rs ; r++){ 87 for(int c = 0; c < cs; c++){ 88 image.pixels[r][c] = prepared[r][c*2]; 89 } 90 } 91 } 92 }