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.dense.gradient.dsift;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.io.PrintWriter;
036import java.util.Scanner;
037import java.util.StringTokenizer;
038
039import org.openimaj.feature.FloatFV;
040
041/**
042 * Dense SIFT keypoint with a location and float feature vector. Also includes
043 * the energy of the feature prior to normalisation in case low-contrast
044 * features need removing.
045 *
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 *
048 */
049public class FloatDSIFTKeypoint
050                extends
051                        AbstractDSIFTKeypoint<FloatFV, float[]>
052{
053        static final long serialVersionUID = 12345545L;
054
055        /**
056         * Construct with the default feature vector length for SIFT (128).
057         */
058        public FloatDSIFTKeypoint() {
059                this(DEFAULT_LENGTH);
060        }
061
062        /**
063         * Construct with the given feature vector length.
064         *
065         * @param length
066         *            the length of the feature vector
067         */
068        public FloatDSIFTKeypoint(int length) {
069                this.descriptor = new float[length];
070        }
071
072        /**
073         * Construct with the given parameters.
074         *
075         * @param x
076         *            the x-ordinate of the keypoint
077         * @param y
078         *            the y-ordinate of the keypoint
079         * @param descriptor
080         *            the feature vector of the keypoint
081         * @param energy
082         *            the energy of the keypoint
083         */
084        public FloatDSIFTKeypoint(final float x, final float y, final float[] descriptor, final float energy) {
085                this.x = x;
086                this.y = y;
087                this.descriptor = descriptor;
088                this.energy = energy;
089        }
090
091        @Override
092        public FloatFV getFeatureVector() {
093                return new FloatFV(descriptor);
094        }
095
096        @Override
097        public String toString() {
098                return ("FloatDSIFTKeypoint(" + this.x + ", " + this.y + ")");
099        }
100
101        @Override
102        public void writeBinary(DataOutput out) throws IOException {
103                out.writeFloat(x);
104                out.writeFloat(y);
105                out.writeFloat(energy);
106
107                for (final float f : descriptor)
108                        out.writeFloat(f);
109        }
110
111        @Override
112        public void writeASCII(PrintWriter out) throws IOException {
113                /* Output data for the keypoint. */
114                out.write(x + " " + y + " " + energy + "\n");
115
116                for (int i = 0; i < descriptor.length; i++) {
117                        if (i > 0 && i % 20 == 0)
118                                out.println();
119                        out.format(" %f", descriptor[i]);
120                }
121                out.println();
122        }
123
124        @Override
125        public void readBinary(DataInput in) throws IOException {
126                x = in.readFloat();
127                y = in.readFloat();
128                energy = in.readFloat();
129                for (int i = 0; i < descriptor.length; i++)
130                        descriptor[i] = in.readFloat();
131        }
132
133        @Override
134        public void readASCII(Scanner in) throws IOException {
135                x = in.nextFloat();
136                y = in.nextFloat();
137                energy = in.nextFloat();
138
139                int i = 0;
140                while (i < descriptor.length) {
141                        final String line = in.nextLine();
142                        final StringTokenizer st = new StringTokenizer(line);
143
144                        while (st.hasMoreTokens()) {
145                                descriptor[i] = Float.parseFloat(st.nextToken());
146                                i++;
147                        }
148                }
149        }
150}