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.processor.connectedcomponent.render; 031 032import java.util.EnumMap; 033import java.util.EnumSet; 034 035import org.openimaj.image.MBFImage; 036import org.openimaj.image.pixel.ConnectedComponent; 037import org.openimaj.image.pixel.ConnectedComponent.ConnectMode; 038import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor; 039import org.openimaj.math.geometry.shape.Polygon; 040 041/** 042 * This renderer encapsulates various other renderers in an easy to 043 * configure class, allowing multiple renderers to be called in one go. 044 * This class is designed for multiband RGB {@link MBFImage}s. 045 * 046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 047 * 048 */ 049public class ConfigurableRendererRGB implements ConnectedComponentProcessor 050{ 051 /** The image to draw into */ 052 protected MBFImage image; 053 054 /** The options to draw with */ 055 protected EnumSet<ConfigurableRenderOptions> options; 056 057 /** The colour to draw the various options in */ 058 EnumMap<ConfigurableRenderOptions, Float[]> colours = new EnumMap<ConfigurableRenderOptions, Float[]>(ConfigurableRenderOptions.class); 059 060 /** 061 * Protected constructor that takes a set of options to draw. 062 * 063 * @param options The options to draw 064 */ 065 protected ConfigurableRendererRGB(EnumSet<ConfigurableRenderOptions> options) { 066 this.options = options; 067 068 colours.put(ConfigurableRenderOptions.AXIS, new Float[] {0.8F, 0.0F, 0.0F}); 069 colours.put(ConfigurableRenderOptions.BLOB, new Float[] {0.5F, 0.5F, 0.5F}); 070 colours.put(ConfigurableRenderOptions.BORDER, new Float[] {0.7F, 0.0F, 0.0F}); 071 colours.put(ConfigurableRenderOptions.CENTROID, new Float[] {0.7F, 0.0F, 0.0F}); 072 colours.put(ConfigurableRenderOptions.CH_AXIS, new Float[] {0.9F, 0.0F, 0.0F}); 073 colours.put(ConfigurableRenderOptions.CH_BLOB, new Float[] {0.2F, 0.2F, 0.2F}); 074 colours.put(ConfigurableRenderOptions.CH_BORDER, new Float[] {1.0F, 0.0F, 0.0F}); 075 colours.put(ConfigurableRenderOptions.CH_CENTROID, new Float[] {1.0F, 0.0F, 0.0F}); 076 } 077 078 /** 079 * Constructor that takes an image to draw into and a set of options to draw. 080 * 081 * @param image The image to draw into 082 * @param options The options to draw 083 */ 084 public ConfigurableRendererRGB(MBFImage image, EnumSet<ConfigurableRenderOptions> options) { 085 this(options); 086 087 this.image = image; 088 } 089 090 /** 091 * Constructor that creates an image to draw into and takes a set of options 092 * to draw. 093 * 094 * @param rows The height of the image to create 095 * @param cols The width of the image to create 096 * @param options The set of options to draw 097 */ 098 public ConfigurableRendererRGB(int rows, int cols, EnumSet<ConfigurableRenderOptions> options) { 099 this(options); 100 101 image = new MBFImage(cols, rows, 3); 102 } 103 104 /** 105 * Get the rendered image. 106 * 107 * @return The rendered image. 108 */ 109 public MBFImage getImage() { 110 return image; 111 } 112 113 /** 114 * {@inheritDoc} 115 * @see org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor#process(org.openimaj.image.pixel.ConnectedComponent) 116 */ 117 @Override 118 public void process(ConnectedComponent cc) { 119 Polygon ch = cc.calculateConvexHull(); 120 ConnectedComponent chcc = new ConnectedComponent(ch); 121 122 if (options.contains(ConfigurableRenderOptions.CH_BLOB)) 123 chcc.process(new BlobRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.CH_BLOB))); 124 125 if (options.contains(ConfigurableRenderOptions.BLOB)) 126 cc.process(new BlobRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.BLOB))); 127 128 if (options.contains(ConfigurableRenderOptions.CH_BORDER)) 129 image.createRenderer().drawPolygon(ch, colours.get(ConfigurableRenderOptions.CH_BORDER)); 130 131 if (options.contains(ConfigurableRenderOptions.BORDER)) 132 cc.process(new BorderRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.BLOB), ConnectMode.CONNECT_8)); 133 134 if (options.contains(ConfigurableRenderOptions.CH_CENTROID)) 135 chcc.process(new CentroidRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.CH_CENTROID))); 136 137 if (options.contains(ConfigurableRenderOptions.CENTROID)) 138 cc.process(new CentroidRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.CENTROID))); 139 140 if (options.contains(ConfigurableRenderOptions.CH_AXIS)) 141 chcc.process(new AxisRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.CH_AXIS))); 142 143 if (options.contains(ConfigurableRenderOptions.AXIS)) 144 cc.process(new AxisRenderer<Float[]>(image, colours.get(ConfigurableRenderOptions.AXIS))); 145 } 146 147 /** 148 * Set the colour of a specific option to override the default. 149 * 150 * @param r The option to set the colour for 151 * @param colour The colour to draw that option in. 152 */ 153 public void setColour(ConfigurableRenderOptions r, Float[] colour) { 154 colours.put(r, colour); 155 } 156 157 /** 158 * Get the colour that a specific option will be drawn in. 159 * 160 * @param r The option to get the colour of. 161 * @return The colour the option will be drawn in. 162 */ 163 public Float[] getColour(ConfigurableRenderOptions r) { 164 return colours.get(r); 165 } 166}