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.analysis.pyramid.gaussian;
31  
32  import java.lang.reflect.Array;
33  
34  import org.openimaj.image.FImage;
35  import org.openimaj.image.Image;
36  import org.openimaj.image.analysis.pyramid.Octave;
37  import org.openimaj.image.processor.SinglebandImageProcessor;
38  
39  /**
40   * This class represents a Gaussian octave in the style of Lowe's SIFT paper.
41   * 
42   * The size of the image stack is controlled by the parameters scales and
43   * extraScaleSteps. The stack is constructed such that images[0] is the initial
44   * image, and images[scales] has twice the blur of the initial image. The sigma
45   * of the initial image is the parameter initialSigma.
46   * 
47   * Octaves are Iterable for easy access to each of the images in turn.
48   * 
49   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
50   * 
51   * @param <IMAGE>
52   *            Type of underlying image
53   */
54  public class GaussianOctave<IMAGE extends Image<?, IMAGE> & SinglebandImageProcessor.Processable<Float, FImage, IMAGE>>
55  		extends
56  		Octave<GaussianPyramidOptions<IMAGE>, GaussianPyramid<IMAGE>, IMAGE>
57  {
58  
59  	/**
60  	 * Construct a Gaussian octave with the provided parent Pyramid and
61  	 * octaveSize. The octaveSize parameter is the size of the octave's images
62  	 * compared to the original image used to construct the pyramid. An
63  	 * octaveSize of 1 means the same size as the original, 2 means half size, 4
64  	 * means quarter size, etc.
65  	 * 
66  	 * @param parent
67  	 *            the pyramid that this octave belongs to
68  	 * @param octaveSize
69  	 *            the size of the octave relative to the original image.
70  	 */
71  	public GaussianOctave(GaussianPyramid<IMAGE> parent, float octaveSize) {
72  		super(parent, octaveSize);
73  	}
74  
75  	/*
76  	 * (non-Javadoc)
77  	 * 
78  	 * @see
79  	 * org.openimaj.image.processing.pyramid.AbstractOctave#process(org.openimaj
80  	 * .image.Image)
81  	 */
82  	@Override
83  	@SuppressWarnings("unchecked")
84  	public void process(IMAGE image) {
85  		images = (IMAGE[]) Array.newInstance(image.getClass(), options.scales + options.extraScaleSteps + 1);
86  
87  		// we want to each level to be separated by a constant factor
88  		// k=2^(1/scales)
89  		final float k = (float) Math.pow(2.0, 1.0 / options.scales);
90  
91  		// image[0] of the octave is the input image
92  		images[0] = image;
93  
94  		// the intial (input) image is considered to have sigma initialSigma.
95  		float prevSigma = options.initialSigma;
96  
97  		for (int i = 1; i < options.scales + options.extraScaleSteps + 1; i++) {
98  			images[i] = images[i - 1].clone();
99  
100 			// compute the amount to increase from prevSigma to prevSigma*k
101 			final float increase = prevSigma * (float) Math.sqrt(k * k - 1.0);
102 
103 			images[i].processInplace(options.createGaussianBlur(increase));
104 
105 			prevSigma *= k;
106 		}
107 
108 		// if a processor is defined, apply it
109 		if (options.getOctaveProcessor() != null)
110 			options.getOctaveProcessor().process(this);
111 	}
112 
113 	/*
114 	 * (non-Javadoc)
115 	 * 
116 	 * @see
117 	 * org.openimaj.image.processing.pyramid.AbstractOctave#getNextOctaveImage()
118 	 */
119 	@Override
120 	public IMAGE getNextOctaveImage() {
121 		return images[options.scales];
122 	}
123 }