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.demos;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.Arrays;
35  
36  import org.openimaj.feature.local.list.LocalFeatureList;
37  import org.openimaj.feature.local.list.MemoryLocalFeatureList;
38  import org.openimaj.image.FImage;
39  import org.openimaj.image.ImageUtilities;
40  import org.openimaj.image.analysis.pyramid.SimplePyramid;
41  import org.openimaj.image.feature.dense.gradient.dsift.ApproximateDenseSIFT;
42  import org.openimaj.image.feature.dense.gradient.dsift.ByteDSIFTKeypoint;
43  import org.openimaj.image.feature.dense.gradient.dsift.DenseSIFT;
44  import org.openimaj.image.feature.dense.gradient.dsift.FloatDSIFTKeypoint;
45  import org.openimaj.io.IOUtils;
46  import org.openimaj.util.function.Operation;
47  import org.openimaj.util.parallel.Parallel;
48  
49  public class FVFWDSift {
50  	static interface DSFactory {
51  		DenseSIFT create();
52  	}
53  
54  	private static void extractPDSift(final File indir, final File outDir, final DSFactory factory) throws IOException
55  	{
56  		Parallel.forEach(Arrays.asList(indir.listFiles()), new Operation<File>() {
57  
58  			@Override
59  			public void perform(File dir) {
60  				try {
61  					if (!dir.isDirectory())
62  						return;
63  
64  					final DenseSIFT sift = factory.create();
65  
66  					for (final File imgfile : dir.listFiles()) {
67  						if (!imgfile.getName().endsWith(".jpg"))
68  							continue;
69  
70  						final File outfile = new File(outDir, imgfile.getAbsolutePath().replace(indir.getAbsolutePath(),
71  								"").replace(".jpg", ".bin"));
72  						outfile.getParentFile().mkdirs();
73  
74  						final FImage image = ImageUtilities.readF(imgfile);
75  
76  						final SimplePyramid<FImage> pyr = new SimplePyramid<FImage>((float) Math.sqrt(2), 5);
77  						pyr.processImage(image);
78  
79  						final LocalFeatureList<FloatDSIFTKeypoint> allKeys = new MemoryLocalFeatureList<FloatDSIFTKeypoint>();
80  						for (final FImage img : pyr) {
81  							sift.analyseImage(img);
82  
83  							final double scale = 160.0 / img.height;
84  							final LocalFeatureList<ByteDSIFTKeypoint> kps = sift.getByteKeypoints();
85  							for (final ByteDSIFTKeypoint kp : kps) {
86  								kp.x *= scale;
87  								kp.y *= scale;
88  
89  								final float[] descriptor = new float[128];
90  								float sumsq = 0;
91  
92  								// reorder to make comparision with matlab
93  								// easier; add offset
94  								for (int i = 0; i < 16; i++) {
95  									descriptor[i * 8] = kp.descriptor[i * 8] + 128;
96  									descriptor[i * 8 + 1] = kp.descriptor[i * 8 + 7] + 128;
97  									descriptor[i * 8 + 2] = kp.descriptor[i * 8 + 6] + 128;
98  									descriptor[i * 8 + 3] = kp.descriptor[i * 8 + 5] + 128;
99  									descriptor[i * 8 + 4] = kp.descriptor[i * 8 + 4] + 128;
100 									descriptor[i * 8 + 5] = kp.descriptor[i * 8 + 3] + 128;
101 									descriptor[i * 8 + 6] = kp.descriptor[i * 8 + 2] + 128;
102 									descriptor[i * 8 + 7] = kp.descriptor[i * 8 + 1] + 128;
103 								}
104 								// rootsift
105 								for (int i = 0; i < 128; i++) {
106 									descriptor[i] = (float) Math.sqrt(descriptor[i]);
107 									sumsq += descriptor[i] * descriptor[i];
108 								}
109 								sumsq = (float) Math.sqrt(sumsq);
110 								final float norm = 1f / Math.max(Float.MIN_NORMAL, sumsq);
111 								for (int i = 0; i < 128; i++) {
112 									descriptor[i] *= norm;
113 								}
114 
115 								allKeys.add(new FloatDSIFTKeypoint(kp.x, kp.y, descriptor, kp.energy));
116 							}
117 						}
118 
119 						IOUtils.writeBinary(outfile, allKeys);
120 
121 						System.out.println(imgfile + " " + allKeys.size());
122 					}
123 				} catch (final Exception e) {
124 					e.printStackTrace();
125 					System.err.println(e);
126 				}
127 			}
128 		});
129 	}
130 
131 	/**
132 	 * @param args
133 	 * @throws IOException
134 	 */
135 	public static void main(String[] args) throws IOException {
136 		final DSFactory factory = new DSFactory() {
137 			@Override
138 			public DenseSIFT create() {
139 				return new ApproximateDenseSIFT(1, 6);
140 			}
141 		};
142 
143 		extractPDSift(
144 				new File("/Volumes/Raid/face_databases/lfw-centre-affine-matlab/"),
145 				new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift/"),
146 				factory);
147 	}
148 
149 }