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.feature.astheticode;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import org.openimaj.image.contour.Contour;
036import org.openimaj.util.function.Function;
037import org.openimaj.util.function.Predicate;
038
039/**
040 * Aestheticode detection algorithm. Only supports simple codes (one parent,
041 * many children, many grandchildren with no children of their own).
042 * 
043 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
044 */
045public class AestheticodeDetector implements Function<Contour, List<Aestheticode>>, Predicate<Contour> {
046
047        private static final int MAX_CHILDLESS_CHILDREN_DEFAULT = 0;
048        private static final int MAX_CHILDREN_DEFAULT = 5;
049        private static final int MIN_CHILDREN_DEFAULT = 5;
050        private int minChildren;
051        private int maxChildren;
052        private int maxChildlessChildren;
053
054        /**
055         * Constructs with the min and max children to the default (both are 5) and
056         * max childless children to 0
057         */
058        public AestheticodeDetector() {
059                this.minChildren = MIN_CHILDREN_DEFAULT;
060                this.maxChildren = MAX_CHILDREN_DEFAULT;
061                this.maxChildlessChildren = MAX_CHILDLESS_CHILDREN_DEFAULT;
062        }
063
064        /**
065         * Construct with the given parameters
066         * 
067         * @param minChildren
068         *            the minimum number of children allowed in a root
069         * @param maxChildren
070         *            the maximum number of children allowed in a root
071         * @param maxChildless
072         *            the maximum number of childless children
073         */
074        public AestheticodeDetector(int minChildren, int maxChildren, int maxChildless) {
075                this.minChildren = minChildren;
076                this.maxChildren = maxChildren;
077                this.maxChildlessChildren = maxChildless;
078        }
079
080        @Override
081        public List<Aestheticode> apply(Contour in) {
082                final List<Aestheticode> found = new ArrayList<Aestheticode>();
083                detectCode(in, found);
084                return found;
085        }
086
087        private void detectCode(Contour root, List<Aestheticode> found) {
088                if (test(root)) {
089                        found.add(new Aestheticode(root));
090                }
091                else {
092                        for (final Contour child : root.children) {
093                                detectCode(child, found);
094                        }
095                }
096        }
097
098        @Override
099        public boolean test(Contour in) {
100                // has at least one child
101                if (in.children.size() < minChildren || in.children.size() > maxChildren) {
102                        return false;
103                }
104
105                int childlessChild = 0;
106                // all grandchildren have no children
107                for (final Contour child : in.children) {
108                        if (child.children.size() == 0)
109                                childlessChild++;
110
111                        if (childlessChild > maxChildlessChildren)
112                                return false;
113                        for (final Contour grandchildren : child.children) {
114                                if (grandchildren.children.size() != 0)
115                                        return false;
116                        }
117                }
118                return true;
119        }
120}