001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.image.processing.algorithm; 031 032import org.openimaj.image.FImage; 033import org.openimaj.image.processor.SinglebandImageProcessor; 034 035import edu.emory.mathcs.jtransforms.fft.FloatFFT_2D; 036 037/** 038 * {@link FImage} correlation performed using an FFT. 039 * 040 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 041 * 042 */ 043public class FourierCorrelation implements SinglebandImageProcessor<Float, FImage> { 044 /** 045 * The template image 046 */ 047 public FImage template; 048 049 /** 050 * Construct the correlation operator with the given template 051 * @param template the template 052 */ 053 public FourierCorrelation(FImage template) { 054 this.template = template; 055 } 056 057 @Override 058 public void processImage(FImage image) { 059 correlate(image, template, true); 060 } 061 062 /** 063 * Correlate an image with a kernel using an FFT. 064 * @param image The image 065 * @param template The template to correlate with the image 066 * @param inplace if true, then output overwrites the input, otherwise a new image is created. 067 * @return correlation map 068 */ 069 public static FImage correlate(FImage image, FImage template, boolean inplace) { 070 final int cols = image.getCols(); 071 final int rows = image.getRows(); 072 073 FloatFFT_2D fft = new FloatFFT_2D(rows, cols); 074 075 float[][] preparedImage = FourierTransform.prepareData(image.pixels, rows, cols, false); 076 fft.complexForward(preparedImage); 077 078 float[][] preparedKernel = FourierTransform.prepareData(template.pixels, rows, cols, false); 079 fft.complexForward(preparedKernel); 080 081 for(int y = 0; y < rows; y++) { 082 for(int x = 0; x < cols; x++) { 083 float reImage = preparedImage[y][x*2]; 084 float imImage = preparedImage[y][1 + x*2]; 085 086 float reKernel = preparedKernel[y][x*2]; 087 float imKernelConj = -1 * preparedKernel[y][1 + x*2]; 088 089 float re = reImage * reKernel - imImage * imKernelConj; 090 float im = reImage * imKernelConj + imImage * reKernel; 091 092 preparedImage[y][x*2] = re; 093 preparedImage[y][1 + x*2] = im; 094 } 095 } 096 097 fft.complexInverse(preparedImage, true); 098 099 FImage out = image; 100 if (!inplace) 101 out = new FImage(cols, rows); 102 103 FourierTransform.unprepareData(preparedImage, out, false); 104 105 return out; 106 } 107}