View Javadoc

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.processing.algorithm.FourierTransform;
34  import org.openimaj.image.processor.SinglebandImageProcessor;
35  
36  import edu.emory.mathcs.jtransforms.fft.FloatFFT_2D;
37  
38  /**
39   * {@link FImage} convolution performed in the fourier domain.
40   *
41   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
42   *
43   */
44  public class FourierConvolve implements SinglebandImageProcessor<Float, FImage> {
45  	private float[][] kernel;
46  
47  	/**
48  	 * Construct the convolution operator with the given kernel
49  	 *
50  	 * @param kernel
51  	 *            the kernel
52  	 */
53  	public FourierConvolve(float[][] kernel) {
54  		this.kernel = kernel;
55  	}
56  
57  	/**
58  	 * Construct the convolution operator with the given kernel
59  	 *
60  	 * @param kernel
61  	 *            the kernel
62  	 */
63  	public FourierConvolve(FImage kernel) {
64  		this.kernel = kernel.pixels;
65  	}
66  
67  	@Override
68  	public void processImage(FImage image) {
69  		convolve(image, kernel, true);
70  	}
71  
72  	/**
73  	 * Convolve an image with a kernel using an FFT.
74  	 *
75  	 * @param image
76  	 *            The image to convolve
77  	 * @param kernel
78  	 *            The kernel
79  	 * @param inplace
80  	 *            if true, then output overwrites the input, otherwise a new image
81  	 *            is created.
82  	 * @return convolved image
83  	 */
84  	public static FImage convolve(FImage image, float[][] kernel, boolean inplace) {
85  		final int cols = image.getCols();
86  		final int rows = image.getRows();
87  
88  		final FloatFFT_2D fft = new FloatFFT_2D(rows, cols);
89  
90  		final float[][] preparedImage = FourierTransform.prepareData(image.pixels, rows, cols, false);
91  		fft.complexForward(preparedImage);
92  
93  		final float[][] preparedKernel = FourierTransform.prepareData(kernel, rows, cols, false);
94  		fft.complexForward(preparedKernel);
95  
96  		for (int y = 0; y < rows; y++) {
97  			for (int x = 0; x < cols; x++) {
98  				final float reImage = preparedImage[y][x * 2];
99  				final float imImage = preparedImage[y][1 + x * 2];
100 
101 				final float reKernel = preparedKernel[y][x * 2];
102 				final float imKernel = preparedKernel[y][1 + x * 2];
103 
104 				final float re = reImage * reKernel - imImage * imKernel;
105 				final float im = reImage * imKernel + imImage * reKernel;
106 
107 				preparedImage[y][x * 2] = re;
108 				preparedImage[y][1 + x * 2] = im;
109 			}
110 		}
111 
112 		fft.complexInverse(preparedImage, true);
113 
114 		FImage out = image;
115 		if (!inplace)
116 			out = new FImage(cols, rows);
117 
118 		FourierTransform.unprepareData(preparedImage, out, false);
119 
120 		return out;
121 	}
122 
123 	/**
124 	 * Convolve an image with a pre-prepared frequency domain filter. The filter
125 	 * must have the same height as the image and twice the width (to account for
126 	 * the imaginary components). Real and imaginary components should be interlaced
127 	 * across the rows.
128 	 *
129 	 * @param image
130 	 *            The image to convolve
131 	 * @param filter
132 	 *            the prepared frequency domain filter
133 	 * @param centered
134 	 *            true if the prepared filter has the highest frequency in the
135 	 *            centre.
136 	 * @return convolved image
137 	 */
138 	public static FImage convolvePrepared(FImage image, FImage filter, boolean centered) {
139 		final int cols = image.getCols();
140 		final int rows = image.getRows();
141 
142 		final FloatFFT_2D fft = new FloatFFT_2D(rows, cols);
143 
144 		final float[][] preparedImage = FourierTransform.prepareData(image.pixels, rows, cols, centered);
145 		fft.complexForward(preparedImage);
146 
147 		final float[][] preparedKernel = filter.pixels;
148 
149 		for (int y = 0; y < rows; y++) {
150 			for (int x = 0; x < cols; x++) {
151 				final float reImage = preparedImage[y][x * 2];
152 				final float imImage = preparedImage[y][1 + x * 2];
153 
154 				final float reKernel = preparedKernel[y][x * 2];
155 				final float imKernel = preparedKernel[y][1 + x * 2];
156 
157 				final float re = reImage * reKernel - imImage * imKernel;
158 				final float im = reImage * imKernel + imImage * reKernel;
159 
160 				preparedImage[y][x * 2] = re;
161 				preparedImage[y][1 + x * 2] = im;
162 			}
163 		}
164 
165 		fft.complexInverse(preparedImage, true);
166 
167 		final FImage out = new FImage(cols, rows);
168 		FourierTransform.unprepareData(preparedImage, out, centered);
169 		return out;
170 	}
171 }