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.MaxFilter;
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 dilation of connected components and (assumed binary) FImages.
45   * See {@link MaxFilter} for greyscale dilation.
46   *
47   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
48   */
49  public class Dilate 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 dilate operator with the given structuring element
58  	 *
59  	 * @param se
60  	 *            the structuring element
61  	 */
62  	public Dilate(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 dilate operator with a BOX structuring element
74  	 */
75  	public Dilate() {
76  		this(StructuringElement.BOX);
77  	}
78  
79  	@Override
80  	public void process(ConnectedComponent cc) {
81  		// Dilate a connected component
82  		final Rectangle cc_bb = cc.calculateRegularBoundingBox();
83  
84  		final Set<Pixel> newPixels = new HashSet<Pixel>();
85  		for (int j = (int) (cc_bb.y - sh); j <= cc_bb.y + sh + cc_bb.height; j++) {
86  			for (int i = (int) (cc_bb.x - sw); i <= cc_bb.x + sw + cc_bb.width; i++) {
87  				final Pixel p = new Pixel(i, j);
88  
89  				if (element.intersect(p, cc.getPixels()).size() >= 1) {
90  					newPixels.add(p);
91  				}
92  			}
93  		}
94  
95  		cc.getPixels().addAll(newPixels);
96  	}
97  
98  	@Override
99  	public int getKernelHeight() {
100 		return sh;
101 	}
102 
103 	@Override
104 	public int getKernelWidth() {
105 		return sw;
106 	}
107 
108 	@Override
109 	public Float processKernel(FImage patch) {
110 		for (final Pixel p : element.positive) {
111 			final int px = cx - p.x;
112 			final int py = cy - p.y;
113 			if (px >= 0 && py >= 0 && px < sw && py < sh && patch.pixels[py][px] == 1) {
114 				return 1f;
115 			}
116 		}
117 
118 		for (final Pixel p : element.negative) {
119 			final int px = cx - p.x;
120 			final int py = cy - p.y;
121 			if (px >= 0 && py >= 0 && px < sw && py < sh && patch.pixels[py][px] == 0)
122 				return 1f;
123 		}
124 
125 		return patch.pixels[cy][cx];
126 	}
127 
128 	/**
129 	 * Apply dilation some number of times to an image with the default
130 	 * {@link StructuringElement#BOX} element
131 	 *
132 	 * @param img
133 	 *            the image
134 	 * @param times
135 	 *            the number of times to apply the dilation
136 	 */
137 	public static void dilate(FImage img, int times) {
138 		final Dilate d = new Dilate();
139 		for (int i = 0; i < times; i++)
140 			img.processInplace(d);
141 	}
142 }