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.dense.gradient.dsift;
31  
32  import java.io.DataInput;
33  import java.io.DataOutput;
34  import java.io.IOException;
35  import java.io.PrintWriter;
36  import java.util.Scanner;
37  import java.util.StringTokenizer;
38  
39  import org.openimaj.feature.ByteFV;
40  
41  /**
42   * Dense SIFT keypoint with a location and byte feature vector. Also includes
43   * the energy of the feature prior to normalisation in case low-contrast
44   * features need removing.
45   *
46   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
47   *
48   */
49  public class ByteDSIFTKeypoint
50  		extends
51  			AbstractDSIFTKeypoint<ByteFV, byte[]>
52  {
53  	static final long serialVersionUID = 12345545L;
54  
55  	/**
56  	 * Construct with the default feature vector length for SIFT (128).
57  	 */
58  	public ByteDSIFTKeypoint() {
59  		this(DEFAULT_LENGTH);
60  	}
61  
62  	/**
63  	 * Construct with the given feature vector length.
64  	 *
65  	 * @param length
66  	 *            the length of the feature vector
67  	 */
68  	public ByteDSIFTKeypoint(final int length) {
69  		this.descriptor = new byte[length];
70  	}
71  
72  	/**
73  	 * Construct with the given parameters.
74  	 *
75  	 * @param x
76  	 *            the x-ordinate of the keypoint
77  	 * @param y
78  	 *            the y-ordinate of the keypoint
79  	 * @param descriptor
80  	 *            the feature vector of the keypoint
81  	 * @param energy
82  	 *            the energy of the keypoint
83  	 */
84  	public ByteDSIFTKeypoint(final float x, final float y, final byte[] descriptor, final float energy) {
85  		this.x = x;
86  		this.y = y;
87  		this.descriptor = descriptor;
88  		this.energy = energy;
89  	}
90  
91  	/**
92  	 * Construct with the given parameters. The float version of the descriptor
93  	 * is converted to bytes by multiplying each bin by 512, clipping to 255 and
94  	 * then subtracting 128.
95  	 *
96  	 * @param x
97  	 *            the x-ordinate of the keypoint
98  	 * @param y
99  	 *            the y-ordinate of the keypoint
100 	 * @param fdescriptor
101 	 *            the flaot version of feature vector of the keypoint
102 	 * @param energy
103 	 *            the energy of the keypoint
104 	 */
105 	public ByteDSIFTKeypoint(final float x, final float y, final float[] fdescriptor, final float energy) {
106 		this.x = x;
107 		this.y = y;
108 		this.energy = energy;
109 		this.descriptor = new byte[fdescriptor.length];
110 
111 		for (int i = 0; i < descriptor.length; i++) {
112 			final int intval = (int) (512.0 * fdescriptor[i]);
113 			descriptor[i] = (byte) (Math.min(255, intval) - 128);
114 		}
115 	}
116 
117 	@Override
118 	public ByteFV getFeatureVector() {
119 		return new ByteFV(descriptor);
120 	}
121 
122 	@Override
123 	public String toString() {
124 		return ("ByteDSIFTKeypoint(" + this.x + ", " + this.y + ")");
125 	}
126 
127 	@Override
128 	public void writeBinary(DataOutput out) throws IOException {
129 		out.writeFloat(x);
130 		out.writeFloat(y);
131 		out.writeFloat(energy);
132 
133 		out.write(this.descriptor);
134 	}
135 
136 	@Override
137 	public void writeASCII(PrintWriter out) throws IOException {
138 		/* Output data for the keypoint. */
139 		out.write(x + " " + y + " " + energy + "\n");
140 
141 		for (int i = 0; i < descriptor.length; i++) {
142 			if (i > 0 && i % 20 == 0)
143 				out.println();
144 			out.print(" " + (descriptor[i] + 128));
145 		}
146 		out.println();
147 	}
148 
149 	@Override
150 	public void readBinary(DataInput in) throws IOException {
151 		x = in.readFloat();
152 		y = in.readFloat();
153 		energy = in.readFloat();
154 		in.readFully(descriptor);
155 	}
156 
157 	@Override
158 	public void readASCII(Scanner in) throws IOException {
159 		x = in.nextFloat();
160 		y = in.nextFloat();
161 		energy = in.nextFloat();
162 
163 		int i = 0;
164 		while (i < descriptor.length) {
165 			final String line = in.nextLine();
166 			final StringTokenizer st = new StringTokenizer(line);
167 
168 			while (st.hasMoreTokens()) {
169 				descriptor[i] = (byte) (Integer.parseInt(st.nextToken()) - 128);
170 				i++;
171 			}
172 		}
173 	}
174 }