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.edges;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.combiner.ImageCombiner;
034
035/**
036 * Non-maximum suppression using X and Y gradient images.
037 * 
038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
039 */
040public class NonMaximumSuppressionTangent implements ImageCombiner<FImage, FImage, FImage> {
041
042        /**
043         * Perform non-maximum suppression.
044         * 
045         * @param dxImage
046         *            x-gradients
047         * @param dyImage
048         *            y-gradients
049         * @return non-maximum suppressed magnitude image.
050         */
051        public static FImage computeSuppressed(FImage dxImage, FImage dyImage) {
052                return computeSuppressed(dxImage, dyImage, null);
053        }
054
055        /**
056         * Perform non-maximum suppression.
057         * 
058         * @param dxImage
059         *            x-gradients
060         * @param dyImage
061         *            y-gradients
062         * @param magsOut
063         *            an image with the same dimensions as dxImage and dyImage for
064         *            holding the magnitudes before non-maximum suppression. May be
065         *            <code>null</code>.
066         * @return non-maximum suppressed magnitude image.
067         */
068        public static FImage computeSuppressed(FImage dxImage, FImage dyImage, FImage magsOut) {
069                final float[][] diffx = dxImage.pixels;
070                final float[][] diffy = dyImage.pixels;
071                final int width = dxImage.width;
072                final int height = dxImage.height;
073
074                final float[][] mag = magsOut == null ? new float[height][width] : magsOut.pixels;
075
076                for (int y = 0; y < height; y++)
077                        for (int x = 0; x < width; x++)
078                                mag[y][x] = (float) Math.sqrt(diffx[y][x] * diffx[y][x] + diffy[y][x] * diffy[y][x]);
079
080                final FImage outimg = new FImage(width, height);
081                final float[][] output = outimg.pixels;
082
083                for (int y = 1; y < height - 1; y++) {
084                        for (int x = 1; x < width - 1; x++) {
085                                int dx, dy;
086
087                                if (diffx[y][x] > 0)
088                                        dx = 1;
089                                else
090                                        dx = -1;
091
092                                if (diffy[y][x] > 0)
093                                        dy = -1;
094                                else
095                                        dy = 1;
096
097                                float a1, a2, b1, b2, A, B, point, val;
098                                if (Math.abs(diffx[y][x]) > Math.abs(diffy[y][x]))
099                                {
100                                        a1 = mag[y][x + dx];
101                                        a2 = mag[y - dy][x + dx];
102                                        b1 = mag[y][x - dx];
103                                        b2 = mag[y + dy][x - dx];
104                                        A = (Math.abs(diffx[y][x]) - Math.abs(diffy[y][x])) * a1 + Math.abs(diffy[y][x]) * a2;
105                                        B = (Math.abs(diffx[y][x]) - Math.abs(diffy[y][x])) * b1 + Math.abs(diffy[y][x]) * b2;
106                                        point = mag[y][x] * Math.abs(diffx[y][x]);
107                                        if (point >= A && point > B) {
108                                                val = Math.abs(diffx[y][x]);
109                                                output[y][x] = val;
110                                        }
111                                        else {
112                                                val = 0;
113                                                output[y][x] = val;
114                                        }
115                                }
116                                else
117                                {
118                                        a1 = mag[y - dy][x];
119                                        a2 = mag[y - dy][x + dx];
120                                        b1 = mag[y + dy][x];
121                                        b2 = mag[y + dy][x - dx];
122                                        A = (Math.abs(diffy[y][x]) - Math.abs(diffx[y][x])) * a1 + Math.abs(diffx[y][x]) * a2;
123                                        B = (Math.abs(diffy[y][x]) - Math.abs(diffx[y][x])) * b1 + Math.abs(diffx[y][x]) * b2;
124                                        point = mag[y][x] * Math.abs(diffy[y][x]);
125                                        if (point >= A && point > B) {
126                                                val = Math.abs(diffy[y][x]);
127                                                output[y][x] = val;
128                                        }
129                                        else {
130                                                val = 0;
131                                                output[y][x] = val;
132                                        }
133                                }
134                        }
135                }
136
137                return outimg;
138        }
139
140        /**
141         * Perform non-maximum suppression.
142         * 
143         * @param dxImage
144         *            x-gradients
145         * @param dyImage
146         *            y-gradients
147         * @return non-maximum suppressed magnitude image.
148         * 
149         * @see org.openimaj.image.combiner.ImageCombiner#combine(org.openimaj.image.Image,
150         *      org.openimaj.image.Image)
151         */
152        @Override
153        public FImage combine(FImage dxImage, FImage dyImage) {
154                return computeSuppressed(dxImage, dyImage);
155        }
156}