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.morphology;
31  
32  import java.util.HashSet;
33  import java.util.Set;
34  
35  import org.openimaj.image.FImage;
36  import org.openimaj.image.pixel.ConnectedComponent;
37  import org.openimaj.image.pixel.Pixel;
38  import org.openimaj.image.processing.algorithm.MinFilter;
39  import org.openimaj.image.processor.KernelProcessor;
40  import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor;
41  import org.openimaj.math.geometry.shape.Rectangle;
42  
43  /**
44   * Morphological erosion of connected components and (assumed binary) FImages.
45   * See {@link MinFilter} for greyscale erosion.
46   *
47   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
48   */
49  public class Erode implements ConnectedComponentProcessor, KernelProcessor<Float, FImage> {
50  	protected StructuringElement element;
51  	protected int cx;
52  	protected int cy;
53  	protected int sw;
54  	protected int sh;
55  
56  	/**
57  	 * Construct the erode operator with the given structuring element
58  	 *
59  	 * @param se
60  	 *            the structuring element
61  	 */
62  	public Erode(StructuringElement se) {
63  		this.element = se;
64  
65  		final int[] sz = se.size();
66  		sw = sz[0];
67  		sh = sz[1];
68  		cx = sw / 2;
69  		cy = sh / 2;
70  	}
71  
72  	/**
73  	 * Construct the erode operator with a BOX structuring element
74  	 */
75  	public Erode() {
76  		this(StructuringElement.BOX);
77  	}
78  
79  	@Override
80  	public void process(ConnectedComponent cc) {
81  		// Erode a connected component
82  		final Set<Pixel> retain = new HashSet<Pixel>();
83  		final Set<Pixel> pixels = cc.getPixels();
84  		final int[] se_size = element.size();
85  		final Rectangle cc_bb = cc.calculateRegularBoundingBox();
86  		for (int j = (int) (cc_bb.y - se_size[1]); j <= cc_bb.y + se_size[1] + cc_bb.height; j++) {
87  			for (int i = (int) (cc_bb.x - se_size[0]); i <= cc_bb.x + se_size[0] + cc_bb.width; i++) {
88  				final Pixel p = new Pixel(i, j);
89  
90  				if (element.matches(p, pixels)) {
91  					retain.add(p);
92  				}
93  			}
94  		}
95  
96  		cc.getPixels().retainAll(retain);
97  	}
98  
99  	@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 }