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 /**
36 * Image processor for separable convolution of an FImage. Capable of doing
37 * convolution in either the vertical, horizontal or both directions.
38 *
39 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
40 */
41 public class FImageConvolveSeparable implements SinglebandImageProcessor<Float, FImage> {
42 float[] hkernel;
43 float[] vkernel;
44
45 /**
46 * Specify the horizontal kernel and vertical kernel separately.
47 *
48 * @param hkernel
49 * horizontal kernel
50 * @param vkernel
51 * vertical kernel
52 */
53 public FImageConvolveSeparable(float[] hkernel, float[] vkernel) {
54 this.hkernel = hkernel;
55 this.vkernel = vkernel;
56 }
57
58 /**
59 * Specify a single kernel to be used as the horizontal and vertical.
60 *
61 * @param kernel
62 * both kernels
63 */
64 public FImageConvolveSeparable(float[] kernel) {
65 this.hkernel = kernel;
66 this.vkernel = kernel;
67 }
68
69 /*
70 * (non-Javadoc)
71 *
72 * @see
73 * org.openimaj.image.processor.ImageProcessor#processImage(org.openimaj
74 * .image.Image)
75 */
76 @Override
77 public void processImage(FImage image) {
78 if (hkernel != null)
79 convolveHorizontal(image, hkernel);
80 if (vkernel != null)
81 convolveVertical(image, vkernel);
82 }
83
84 /*
85 * Convolve an array of data with a kernel. The data must be padded at each
86 * end by half the kernel width (with replicated data or zeros). The output
87 * is written back into the data buffer, starting at the beginning and is
88 * valid through buffer.length-kernel.length.
89 */
90 protected static void convolveBuffer(float[] buffer, float[] kernel)
91 {
92 final int l = buffer.length - kernel.length;
93 for (int i = 0; i < l; i++) {
94 float sum = 0.0f;
95
96 for (int j = 0, jj = kernel.length - 1; j < kernel.length; j++, jj--)
97 sum += buffer[i + j] * kernel[jj];
98
99 buffer[i] = sum;
100 }
101 }
102
103 /**
104 * Convolve the image in the horizontal direction with the kernel. Edge
105 * effects are handled by duplicating the edge pixels.
106 *
107 * @param image
108 * the image to convolve.
109 * @param kernel
110 * the convolution kernel.
111 */
112 public static void convolveHorizontal(FImage image, float[] kernel) {
113 final int halfsize = kernel.length / 2;
114
115 final float buffer[] = new float[image.width + kernel.length];
116
117 for (int r = 0; r < image.height; r++) {
118 for (int i = 0; i < halfsize; i++)
119 buffer[i] = image.pixels[r][0];
120 for (int i = 0; i < image.width; i++)
121 buffer[halfsize + i] = image.pixels[r][i];
122 for (int i = 0; i < halfsize; i++)
123 buffer[halfsize + image.width + i] = image.pixels[r][image.width - 1];
124
125 // convolveBuffer(buffer, kernel);
126 final int l = buffer.length - kernel.length;
127 for (int i = 0; i < l; i++) {
128 float sum = 0.0f;
129
130 for (int j = 0, jj = kernel.length - 1; j < kernel.length; j++, jj--)
131 sum += buffer[i + j] * kernel[jj];
132
133 buffer[i] = sum;
134 }
135 // end convolveBuffer(buffer, kernel);
136
137 for (int c = 0; c < image.width; c++)
138 image.pixels[r][c] = buffer[c];
139 }
140 }
141
142 /**
143 * Convolve the image in the vertical direction with the kernel. Edge
144 * effects are handled by duplicating the edge pixels.
145 *
146 * @param image
147 * the image to convolve.
148 * @param kernel
149 * the convolution kernel.
150 */
151 public static void convolveVertical(FImage image, float[] kernel) {
152 final int halfsize = kernel.length / 2;
153
154 final float buffer[] = new float[image.height + kernel.length];
155
156 for (int c = 0; c < image.width; c++) {
157 for (int i = 0; i < halfsize; i++)
158 buffer[i] = image.pixels[0][c];
159 for (int i = 0; i < image.height; i++)
160 buffer[halfsize + i] = image.pixels[i][c];
161 for (int i = 0; i < halfsize; i++)
162 buffer[halfsize + image.height + i] = image.pixels[image.height - 1][c];
163
164 // convolveBuffer(buffer, kernel);
165 final int l = buffer.length - kernel.length;
166 for (int i = 0; i < l; i++) {
167 float sum = 0.0f;
168
169 for (int j = 0, jj = kernel.length - 1; j < kernel.length; j++, jj--)
170 sum += buffer[i + j] * kernel[jj];
171
172 buffer[i] = sum;
173 }
174 // end convolveBuffer(buffer, kernel);
175
176 for (int r = 0; r < image.height; r++)
177 image.pixels[r][c] = buffer[r];
178 }
179 }
180
181 /**
182 * Fast convolution for separated 3x3 kernels. Only valid pixels are
183 * considered, so the output image bounds will be two pixels smaller than
184 * the input image on all sides (the response of the kernel to the source
185 * pixel at 1,1 is stored in the destination image at 0,0)
186 *
187 * @param source
188 * the source image
189 * @param dest
190 * the destination image
191 * @param kx
192 * the x-kernel (can be null, implying [0 1 0] )
193 * @param ky
194 * the y-kernel (can be null, implying [0 1 0])
195 * @param buffer
196 * the working buffer (can be null, but ideally the same width as
197 * the source image)
198 */
199 public static void fastConvolve3(FImage source, FImage dest, float[] kx, float[] ky, float[] buffer)
200 {
201 final int dst_width = source.width - 2;
202
203 if (kx == null)
204 kx = new float[] { 0, 1, 0 };
205 if (ky == null)
206 ky = new float[] { 0, 1, 0 };
207
208 if (buffer == null || buffer.length < source.width)
209 buffer = new float[source.width];
210
211 for (int y = 0; y <= source.height - 3; y++) {
212 final float[] src = source.pixels[y];
213 final float[] src2 = source.pixels[y + 1];
214 final float[] src3 = source.pixels[y + 2];
215
216 for (int x = 0; x < source.width; x++)
217 {
218 buffer[x] = ky[0] * src[x] + ky[1] * src2[x] + ky[2] * src3[x];
219 }
220
221 for (int x = 0; x < dst_width; x++)
222 {
223 dest.pixels[y][x] = kx[0] * buffer[x] + kx[1] * buffer[x + 1] + kx[2] * buffer[x + 2];
224 }
225 }
226 }
227 }