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.convolution;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.processor.SinglebandImageProcessor;
034
035/**
036 * Image processor for separable convolution of an FImage. Capable of doing
037 * convolution in either the vertical, horizontal or both directions.
038 * 
039 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
040 */
041public class FImageConvolveSeparable implements SinglebandImageProcessor<Float, FImage> {
042        float[] hkernel;
043        float[] vkernel;
044
045        /**
046         * Specify the horizontal kernel and vertical kernel separately.
047         * 
048         * @param hkernel
049         *            horizontal kernel
050         * @param vkernel
051         *            vertical kernel
052         */
053        public FImageConvolveSeparable(float[] hkernel, float[] vkernel) {
054                this.hkernel = hkernel;
055                this.vkernel = vkernel;
056        }
057
058        /**
059         * Specify a single kernel to be used as the horizontal and vertical.
060         * 
061         * @param kernel
062         *            both kernels
063         */
064        public FImageConvolveSeparable(float[] kernel) {
065                this.hkernel = kernel;
066                this.vkernel = kernel;
067        }
068
069        /*
070         * (non-Javadoc)
071         * 
072         * @see
073         * org.openimaj.image.processor.ImageProcessor#processImage(org.openimaj
074         * .image.Image)
075         */
076        @Override
077        public void processImage(FImage image) {
078                if (hkernel != null)
079                        convolveHorizontal(image, hkernel);
080                if (vkernel != null)
081                        convolveVertical(image, vkernel);
082        }
083
084        /*
085         * Convolve an array of data with a kernel. The data must be padded at each
086         * end by half the kernel width (with replicated data or zeros). The output
087         * is written back into the data buffer, starting at the beginning and is
088         * valid through buffer.length-kernel.length.
089         */
090        protected static void convolveBuffer(float[] buffer, float[] kernel)
091        {
092                final int l = buffer.length - kernel.length;
093                for (int i = 0; i < l; i++) {
094                        float sum = 0.0f;
095
096                        for (int j = 0, jj = kernel.length - 1; j < kernel.length; j++, jj--)
097                                sum += buffer[i + j] * kernel[jj];
098
099                        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}