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 static org.junit.Assert.assertArrayEquals;
33  import static org.junit.Assert.assertEquals;
34  
35  import java.io.IOException;
36  
37  import org.apache.commons.lang.ArrayUtils;
38  import org.junit.Test;
39  import org.openimaj.OpenIMAJ;
40  import org.openimaj.feature.local.list.LocalFeatureList;
41  import org.openimaj.image.ImageUtilities;
42  import org.openimaj.image.MBFImage;
43  import org.openimaj.image.colour.ColourSpace;
44  
45  /**
46   * Tests for {@link ColourDenseSIFT}
47   * 
48   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
49   */
50  public class ColourDenseSIFTTest {
51  	/**
52  	 * Test with the opponent colour space
53  	 * 
54  	 * @throws IOException
55  	 */
56  	@Test
57  	public void testOpponent() throws IOException {
58  		final MBFImage img = ImageUtilities.readMBF(OpenIMAJ.getLogoAsStream());
59  
60  		final ColourDenseSIFT cdsift = new ColourDenseSIFT(new DenseSIFT(), ColourSpace.MODIFIED_OPPONENT);
61  		final DenseSIFT luminance_dsift = new DenseSIFT();
62  		final DenseSIFT o1_dsift = new DenseSIFT();
63  		final DenseSIFT o2_dsift = new DenseSIFT();
64  
65  		final MBFImage oppImg = ColourSpace.MODIFIED_OPPONENT.convertFromRGB(img);
66  
67  		cdsift.analyseImage(img);
68  		luminance_dsift.analyseImage(oppImg.getBand(0));
69  		o1_dsift.analyseImage(oppImg.getBand(1));
70  		o2_dsift.analyseImage(oppImg.getBand(2));
71  
72  		assertEquals(cdsift.descriptors.length, luminance_dsift.descriptors.length);
73  		assertEquals(cdsift.descriptors.length, o1_dsift.descriptors.length);
74  		assertEquals(cdsift.descriptors.length, o2_dsift.descriptors.length);
75  
76  		assertEquals(cdsift.descriptors[0].length, 3 * luminance_dsift.descriptors[0].length);
77  
78  		final LocalFeatureList<ByteDSIFTKeypoint> cdbyte = cdsift.getByteKeypoints();
79  		final LocalFeatureList<ByteDSIFTKeypoint> ldbyte = luminance_dsift.getByteKeypoints();
80  		final LocalFeatureList<ByteDSIFTKeypoint> o1dbyte = o1_dsift.getByteKeypoints();
81  		final LocalFeatureList<ByteDSIFTKeypoint> o2dbyte = o2_dsift.getByteKeypoints();
82  
83  		final LocalFeatureList<FloatDSIFTKeypoint> cdfloat = cdsift.getFloatKeypoints();
84  		final LocalFeatureList<FloatDSIFTKeypoint> ldfloat = luminance_dsift.getFloatKeypoints();
85  		final LocalFeatureList<FloatDSIFTKeypoint> o1dfloat = o1_dsift.getFloatKeypoints();
86  		final LocalFeatureList<FloatDSIFTKeypoint> o2dfloat = o2_dsift.getFloatKeypoints();
87  
88  		for (int i = 0; i < cdsift.descriptors.length; i++) {
89  			assertArrayEquals(luminance_dsift.descriptors[i], ArrayUtils.subarray(cdsift.descriptors[i], 0, 128), 0f);
90  			assertArrayEquals(o1_dsift.descriptors[i], ArrayUtils.subarray(cdsift.descriptors[i], 128, 256), 0f);
91  			assertArrayEquals(o2_dsift.descriptors[i], ArrayUtils.subarray(cdsift.descriptors[i], 256, 384), 0f);
92  
93  			assertEquals(cdbyte.get(i).x, ldbyte.get(i).x, 0);
94  			assertEquals(cdbyte.get(i).y, ldbyte.get(i).y, 0);
95  			assertEquals(cdbyte.get(i).energy, ldbyte.get(i).energy, 0);
96  			assertArrayEquals(ArrayUtils.subarray(cdbyte.get(i).descriptor, 0, 128), ldbyte.get(i).descriptor);
97  			assertArrayEquals(ArrayUtils.subarray(cdbyte.get(i).descriptor, 128, 256), o1dbyte.get(i).descriptor);
98  			assertArrayEquals(ArrayUtils.subarray(cdbyte.get(i).descriptor, 256, 384), o2dbyte.get(i).descriptor);
99  
100 			assertEquals(cdfloat.get(i).x, ldfloat.get(i).x, 0);
101 			assertEquals(cdfloat.get(i).y, ldfloat.get(i).y, 0);
102 			assertEquals(cdfloat.get(i).energy, ldfloat.get(i).energy, 0);
103 			assertArrayEquals(ArrayUtils.subarray(cdfloat.get(i).descriptor, 0, 128), ldfloat.get(i).descriptor, 0f);
104 			assertArrayEquals(ArrayUtils.subarray(cdfloat.get(i).descriptor, 128, 256), o1dfloat.get(i).descriptor, 0f);
105 			assertArrayEquals(ArrayUtils.subarray(cdfloat.get(i).descriptor, 256, 384), o2dfloat.get(i).descriptor, 0f);
106 		}
107 	}
108 
109 	/**
110 	 * Test with the opponent colour space
111 	 * 
112 	 * @throws IOException
113 	 */
114 	@Test
115 	public void testOpponentFast() throws IOException {
116 		final MBFImage img = ImageUtilities.readMBF(OpenIMAJ.getLogoAsStream());
117 
118 		final ColourDenseSIFT cdsift = new ColourDenseSIFT(new ApproximateDenseSIFT(), ColourSpace.MODIFIED_OPPONENT);
119 		final DenseSIFT luminance_dsift = new ApproximateDenseSIFT();
120 		final DenseSIFT o1_dsift = new ApproximateDenseSIFT();
121 		final DenseSIFT o2_dsift = new ApproximateDenseSIFT();
122 
123 		final MBFImage oppImg = ColourSpace.MODIFIED_OPPONENT.convertFromRGB(img);
124 
125 		cdsift.analyseImage(img);
126 		luminance_dsift.analyseImage(oppImg.getBand(0));
127 		o1_dsift.analyseImage(oppImg.getBand(1));
128 		o2_dsift.analyseImage(oppImg.getBand(2));
129 
130 		assertEquals(cdsift.descriptors.length, luminance_dsift.descriptors.length);
131 		assertEquals(cdsift.descriptors.length, o1_dsift.descriptors.length);
132 		assertEquals(cdsift.descriptors.length, o2_dsift.descriptors.length);
133 
134 		assertEquals(cdsift.descriptors[0].length, 3 * luminance_dsift.descriptors[0].length);
135 
136 		final LocalFeatureList<ByteDSIFTKeypoint> cdbyte = cdsift.getByteKeypoints();
137 		final LocalFeatureList<ByteDSIFTKeypoint> ldbyte = luminance_dsift.getByteKeypoints();
138 		final LocalFeatureList<ByteDSIFTKeypoint> o1dbyte = o1_dsift.getByteKeypoints();
139 		final LocalFeatureList<ByteDSIFTKeypoint> o2dbyte = o2_dsift.getByteKeypoints();
140 
141 		final LocalFeatureList<FloatDSIFTKeypoint> cdfloat = cdsift.getFloatKeypoints();
142 		final LocalFeatureList<FloatDSIFTKeypoint> ldfloat = luminance_dsift.getFloatKeypoints();
143 		final LocalFeatureList<FloatDSIFTKeypoint> o1dfloat = o1_dsift.getFloatKeypoints();
144 		final LocalFeatureList<FloatDSIFTKeypoint> o2dfloat = o2_dsift.getFloatKeypoints();
145 
146 		for (int i = 0; i < cdsift.descriptors.length; i++) {
147 			assertArrayEquals(luminance_dsift.descriptors[i], ArrayUtils.subarray(cdsift.descriptors[i], 0, 128), 0f);
148 			assertArrayEquals(o1_dsift.descriptors[i], ArrayUtils.subarray(cdsift.descriptors[i], 128, 256), 0f);
149 			assertArrayEquals(o2_dsift.descriptors[i], ArrayUtils.subarray(cdsift.descriptors[i], 256, 384), 0f);
150 
151 			assertEquals(cdbyte.get(i).x, ldbyte.get(i).x, 0);
152 			assertEquals(cdbyte.get(i).y, ldbyte.get(i).y, 0);
153 			assertEquals(cdbyte.get(i).energy, ldbyte.get(i).energy, 0);
154 			assertArrayEquals(ArrayUtils.subarray(cdbyte.get(i).descriptor, 0, 128), ldbyte.get(i).descriptor);
155 			assertArrayEquals(ArrayUtils.subarray(cdbyte.get(i).descriptor, 128, 256), o1dbyte.get(i).descriptor);
156 			assertArrayEquals(ArrayUtils.subarray(cdbyte.get(i).descriptor, 256, 384), o2dbyte.get(i).descriptor);
157 
158 			assertEquals(cdfloat.get(i).x, ldfloat.get(i).x, 0);
159 			assertEquals(cdfloat.get(i).y, ldfloat.get(i).y, 0);
160 			assertEquals(cdfloat.get(i).energy, ldfloat.get(i).energy, 0);
161 			assertArrayEquals(ArrayUtils.subarray(cdfloat.get(i).descriptor, 0, 128), ldfloat.get(i).descriptor, 0f);
162 			assertArrayEquals(ArrayUtils.subarray(cdfloat.get(i).descriptor, 128, 256), o1dfloat.get(i).descriptor, 0f);
163 			assertArrayEquals(ArrayUtils.subarray(cdfloat.get(i).descriptor, 256, 384), o2dfloat.get(i).descriptor, 0f);
164 		}
165 	}
166 }