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.morphology;
031
032import java.util.HashSet;
033import java.util.Set;
034
035import org.openimaj.image.FImage;
036import org.openimaj.image.pixel.ConnectedComponent;
037import org.openimaj.image.pixel.Pixel;
038import org.openimaj.image.processing.algorithm.MinFilter;
039import org.openimaj.image.processor.KernelProcessor;
040import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor;
041import org.openimaj.math.geometry.shape.Rectangle;
042
043/**
044 * Morphological erosion of connected components and (assumed binary) FImages.
045 * See {@link MinFilter} for greyscale erosion.
046 *
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 */
049public class Erode implements ConnectedComponentProcessor, KernelProcessor<Float, FImage> {
050        protected StructuringElement element;
051        protected int cx;
052        protected int cy;
053        protected int sw;
054        protected int sh;
055
056        /**
057         * Construct the erode operator with the given structuring element
058         *
059         * @param se
060         *            the structuring element
061         */
062        public Erode(StructuringElement se) {
063                this.element = se;
064
065                final int[] sz = se.size();
066                sw = sz[0];
067                sh = sz[1];
068                cx = sw / 2;
069                cy = sh / 2;
070        }
071
072        /**
073         * Construct the erode operator with a BOX structuring element
074         */
075        public Erode() {
076                this(StructuringElement.BOX);
077        }
078
079        @Override
080        public void process(ConnectedComponent cc) {
081                // Erode a connected component
082                final Set<Pixel> retain = new HashSet<Pixel>();
083                final Set<Pixel> pixels = cc.getPixels();
084                final int[] se_size = element.size();
085                final Rectangle cc_bb = cc.calculateRegularBoundingBox();
086                for (int j = (int) (cc_bb.y - se_size[1]); j <= cc_bb.y + se_size[1] + cc_bb.height; j++) {
087                        for (int i = (int) (cc_bb.x - se_size[0]); i <= cc_bb.x + se_size[0] + cc_bb.width; i++) {
088                                final Pixel p = new Pixel(i, j);
089
090                                if (element.matches(p, pixels)) {
091                                        retain.add(p);
092                                }
093                        }
094                }
095
096                cc.getPixels().retainAll(retain);
097        }
098
099        @Override
100        public int getKernelHeight() {
101                return sh;
102        }
103
104        @Override
105        public int getKernelWidth() {
106                return sw;
107        }
108
109        @Override
110        public Float processKernel(FImage patch) {
111                int count = 0;
112
113                for (final Pixel p : element.positive) {
114                        final int px = cx - p.x;
115                        final int py = cy - p.y;
116                        if (px >= 0 && py >= 0 && px < sw && py < sh && patch.pixels[py][px] == 1)
117                                count++;
118                }
119
120                for (final Pixel p : element.negative) {
121                        final int px = cx - p.x;
122                        final int py = cy - p.y;
123                        if (px >= 0 && py >= 0 && px < sw && py < sh && patch.pixels[py][px] == 0)
124                                count++;
125                }
126
127                return (count == element.positive.size() + element.negative.size() ? patch.pixels[cy][cx] : 0);
128        }
129
130        /**
131         * Apply erosion some number of times to an image with the default
132         * {@link StructuringElement#BOX} element
133         *
134         * @param img
135         *            the image
136         * @param times
137         *            the number of times to apply the erosion
138         */
139        public static void erode(FImage img, int times) {
140                final Erode e = new Erode();
141                for (int i = 0; i < times; i++)
142                        img.processInplace(e);
143        }
144}