View Javadoc

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.feature.astheticode;
31  
32  import java.util.Arrays;
33  
34  import org.openimaj.image.contour.Contour;
35  
36  /**
37   * A detected Aestheticode
38   * 
39   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
40   */
41  public class Aestheticode {
42  	/**
43  	 * The actual code
44  	 */
45  	public int[] code;
46  
47  	/**
48  	 * The border object describing the shape of the code
49  	 */
50  	public Contour root;
51  
52  	/**
53  	 * Construct with a code
54  	 * 
55  	 * @param code
56  	 *            the code
57  	 */
58  	public Aestheticode(int[] code) {
59  		this.code = code;
60  		Arrays.sort(code);
61  	}
62  
63  	/**
64  	 * Construct with another {@link Aestheticode}
65  	 * 
66  	 * @param acode
67  	 *            copy this {@link Aestheticode}
68  	 */
69  	public Aestheticode(Aestheticode acode) {
70  		this.code = acode.code.clone();
71  		this.root = acode.root;
72  	}
73  
74  	/**
75  	 * Construct with a border
76  	 * 
77  	 * @param root
78  	 *            the detected borders to construct a code from
79  	 */
80  	public Aestheticode(Contour root) {
81  		this.root = root;
82  
83  		this.code = new int[root.children.size()];
84  		int i = 0;
85  		for (final Contour child : root.children) {
86  			this.code[i++] = child.children.size();
87  		}
88  		Arrays.sort(code);
89  	}
90  
91  	@Override
92  	public int hashCode() {
93  		return Arrays.hashCode(this.code);
94  	};
95  
96  	@Override
97  	public boolean equals(Object obj) {
98  		if (!(obj instanceof Aestheticode)) {
99  			return false;
100 		}
101 
102 		final Aestheticode that = (Aestheticode) obj;
103 		if (that.code.length != this.code.length)
104 			return false;
105 
106 		for (int i = 0; i < this.code.length; i++) {
107 			if (this.code[i] != that.code[i])
108 				return false;
109 		}
110 
111 		return true;
112 	};
113 
114 	@Override
115 	public String toString() {
116 		String ret = "" + this.code[0];
117 		for (int i = 1; i < code.length; i++) {
118 			ret += ":" + code[i];
119 		}
120 		return ret;
121 	};
122 }