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.analysis.watershed; 031 032import java.util.HashMap; 033import java.util.Map; 034 035import org.apache.log4j.Logger; 036import org.openimaj.image.analysis.watershed.event.ComponentStackMergeListener; 037import org.openimaj.util.tree.TreeNode; 038import org.openimaj.util.tree.TreeNodeImpl; 039 040 041/** 042 * A listener that listens to the watershed algorithm progress and 043 * creates a region tree as the processing takes place. 044 * 045 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 047 * 048 */ 049public class MergeTreeBuilder implements ComponentStackMergeListener 050{ 051 Logger logger = Logger.getLogger(MergeTreeBuilder.class); 052 053 /** This is the tree we build */ 054 private TreeNode<Component> tree = null; 055 056 /** 057 * Because we're creating components to store the history, 058 * the components that are being processed by the algorithm 059 * are not the same as those in our tree, so we must provide 060 * a map so that we can join up the tree afterwards. 061 */ 062 private Map<Component,TreeNode<Component>> map = null; 063 064 /** 065 * Default constructor 066 */ 067 public MergeTreeBuilder() 068 { 069 map = new HashMap<Component, TreeNode<Component>>(); 070 } 071 072 /** 073 * {@inheritDoc} 074 * @see org.openimaj.image.analysis.watershed.event.ComponentStackMergeListener#componentsMerged(org.openimaj.image.analysis.watershed.Component, org.openimaj.image.analysis.watershed.Component) 075 */ 076 @Override 077 public void componentsMerged( Component c1, Component c2 ) 078 { 079 logger.debug( "Map: "+map ); 080 logger.debug( "Component c1: "+c1 ); 081 logger.debug( "Component c2: "+c2 ); 082 083 // Create a tree node for the component if it doesn't 084 // exist already 085 TreeNode<Component> c1xtn = map.get( c1 ); 086 if( c1xtn == null ) 087 { 088 logger.debug( "c1 not found" ); 089 c1xtn = new TreeNodeImpl<Component>(); 090 Component c1x = c1.clone(); 091 c1xtn.setValue( c1x ); 092 map.put( c1, c1xtn ); 093 } 094 095 // Add all the pixels from c2 into our copy of c1 096 c1xtn.getValue().merge( c2 ); 097 098 // Create a tree node for the second component 099 // if it doesn't exist already 100 TreeNode<Component> c2xtn = map.get( c2 ); 101 if( c2xtn == null ) 102 { 103 logger.debug( "c2 not found" ); 104 c2xtn = new TreeNodeImpl<Component>(); 105 Component c2x = c2.clone(); 106 c2xtn.setValue( c2x ); 107 map.put( c2, c2xtn ); 108 } 109 110 logger.debug("Linking "+c1xtn+" and "+c2xtn ); 111 112 // Link the tree nodes 113 c1xtn.addChild( c2xtn ); 114 this.tree = c1xtn; 115 } 116 117 /** 118 * {@inheritDoc} 119 * @see org.openimaj.image.analysis.watershed.event.ComponentStackMergeListener#componentPromoted(org.openimaj.image.analysis.watershed.Component) 120 */ 121 @Override 122 public void componentPromoted( Component c1 ) 123 { 124 TreeNode<Component> c1xtn_old = map.get( c1 ); 125 126 Component c1x = c1.clone(); 127 128 TreeNode<Component> c1xtn = new TreeNodeImpl<Component>(); 129 c1xtn.setValue( c1x ); 130 131 map.put( c1, c1xtn ); 132 133 if( c1xtn_old != null ) 134 c1xtn.addChild( c1xtn_old ); 135 } 136 137 /** 138 * Return the tree that has been built. 139 * @return the tree 140 */ 141 public TreeNode<Component> getTree() 142 { 143 return tree; 144 } 145}