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.MaxFilter; 039import org.openimaj.image.processor.KernelProcessor; 040import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor; 041import org.openimaj.math.geometry.shape.Rectangle; 042 043/** 044 * Morphological dilation of connected components and (assumed binary) FImages. 045 * See {@link MaxFilter} for greyscale dilation. 046 * 047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 048 */ 049public class Dilate 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 dilate operator with the given structuring element 058 * 059 * @param se 060 * the structuring element 061 */ 062 public Dilate(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 dilate operator with a BOX structuring element 074 */ 075 public Dilate() { 076 this(StructuringElement.BOX); 077 } 078 079 @Override 080 public void process(ConnectedComponent cc) { 081 // Dilate a connected component 082 final Rectangle cc_bb = cc.calculateRegularBoundingBox(); 083 084 final Set<Pixel> newPixels = new HashSet<Pixel>(); 085 for (int j = (int) (cc_bb.y - sh); j <= cc_bb.y + sh + cc_bb.height; j++) { 086 for (int i = (int) (cc_bb.x - sw); i <= cc_bb.x + sw + cc_bb.width; i++) { 087 final Pixel p = new Pixel(i, j); 088 089 if (element.intersect(p, cc.getPixels()).size() >= 1) { 090 newPixels.add(p); 091 } 092 } 093 } 094 095 cc.getPixels().addAll(newPixels); 096 } 097 098 @Override 099 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}