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.segmentation; 31 32 import gnu.trove.map.hash.TFloatObjectHashMap; 33 34 import java.util.Arrays; 35 import java.util.List; 36 37 import org.openimaj.image.FImage; 38 import org.openimaj.image.pixel.PixelSet; 39 import org.openimaj.image.processor.Processor; 40 41 /** 42 * Simple wrapper to make thresholding algorithms into {@link Segmenter}s by 43 * applying the thresholding operation and then gathering the pixel sets 44 * belonging to each segment. Note that class does not perform connected 45 * component analysis, and for example in the case of binary thresholding, there 46 * will only be two {@link PixelSet}s produced (i.e. foreground and background). 47 * 48 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 49 * 50 */ 51 public class ThresholdSegmenter implements Segmenter<FImage> { 52 Processor<FImage> thresholder; 53 54 /** 55 * Construct with the given thresholding algorithm implementation. 56 * 57 * @param thresholder 58 * the thresholding algorithm 59 */ 60 public ThresholdSegmenter(Processor<FImage> thresholder) { 61 this.thresholder = thresholder; 62 } 63 64 @Override 65 public List<? extends PixelSet> segment(FImage image) { 66 final FImage timg = image.process(thresholder); 67 final TFloatObjectHashMap<PixelSet> sets = new TFloatObjectHashMap<PixelSet>(); 68 69 for (int y = 0; y < timg.height; y++) { 70 for (int x = 0; x < timg.width; x++) { 71 final float p = image.getPixel(x, y); 72 73 PixelSet ps = sets.get(p); 74 if (ps == null) 75 sets.put(p, ps = new PixelSet()); 76 ps.addPixel(x, y); 77 } 78 } 79 80 return Arrays.asList(sets.values()); 81 } 82 }