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.analysis.pyramid.gaussian; 031 032import org.openimaj.image.FImage; 033import org.openimaj.image.Image; 034import org.openimaj.image.analyser.ImageAnalyser; 035import org.openimaj.image.analysis.pyramid.Pyramid; 036import org.openimaj.image.processing.resize.ResizeProcessor; 037import org.openimaj.image.processor.SinglebandImageProcessor; 038 039/** 040 * A Gaussian image pyramid consisting of a stack of octaves where the image 041 * halves its size. The pyramid is of the style described in Lowe's SIFT paper. 042 * 043 * Octaves are processed by an OctaveProcessor as they are created if the 044 * processor is set in the options object. 045 * 046 * The pyramid will only hold onto its octaves if either the keepOctaves option 047 * is set to true, or if a PyramidProcessor is set in the options. The 048 * PyramidProcessor will called after all the octaves are created. 049 * 050 * Pyramids are Iterable for easy access to the octaves; however this will only 051 * work if the pyramid has already been populated with the octaves retained. 052 * 053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 054 * 055 * @param <I> 056 * Type of underlying image 057 */ 058public class GaussianPyramid<I extends Image<?, I> & SinglebandImageProcessor.Processable<Float, FImage, I>> 059 extends 060 Pyramid<GaussianPyramidOptions<I>, GaussianOctave<I>, I> 061 implements 062 ImageAnalyser<I>, Iterable<GaussianOctave<I>> 063{ 064 /** 065 * Construct a Pyramid with the given options. 066 * 067 * @param options 068 * the options 069 */ 070 public GaussianPyramid(GaussianPyramidOptions<I> options) { 071 super(options); 072 } 073 074 /* 075 * (non-Javadoc) 076 * 077 * @see 078 * org.openimaj.image.processing.pyramid.AbstractPyramid#process(org.openimaj 079 * .image.Image) 080 */ 081 @Override 082 public void process(I img) { 083 if (img.getWidth() <= 1 || img.getHeight() <= 1) 084 throw new IllegalArgumentException("Image is too small"); 085 086 // the octave image size: 1 means same as input, 0.5 is twice as big as 087 // input, 2 is half input, 4 is quarter input, etc 088 float octaveSize = 1.0f; 089 090 // if doubleInitialImage is set, then the initial image should be scaled 091 // to 092 // twice its original size and the 093 I image; 094 if (options.doubleInitialImage) { 095 image = ResizeProcessor.doubleSize(img); 096 octaveSize *= 0.5; 097 } else 098 image = img.clone(); 099 100 // Lowe's IJCV paper (P.10) suggests that if you double the size of the 101 // initial image then it has a sigma of 1.0; if the image is not doubled 102 // the sigma is 0.5 103 final float currentSigma = (options.doubleInitialImage ? 1.0f : 0.5f); 104 if (options.initialSigma > currentSigma) { 105 // we now need to bring the starting image to a sigma of 106 // initialSigma 107 // in order to start building the pyramid (every octave starts at 108 // initialSigma sigmas). 109 final float sigma = (float) Math.sqrt(options.initialSigma * options.initialSigma - currentSigma 110 * currentSigma); 111 image.processInplace(this.options.createGaussianBlur(sigma)); 112 } 113 114 // the minimum size image in the pyramid must be bigger than 115 // two pixels + whatever border is required by the options 116 // (on both sides). 117 final int minImageSize = 2 + (2 * options.getBorderPixels()); 118 119 while (image.getHeight() > minImageSize && image.getWidth() > minImageSize) { 120 // construct empty octave 121 final GaussianOctave<I> currentOctave = new GaussianOctave<I>(this, octaveSize); 122 123 // populate the octave with images; once the octave 124 // is complete any OctaveProcessor specified in the 125 // options will be applied. 126 currentOctave.process(image); 127 128 // get the image with 2*sigma from the octave and 129 // half its size ready for the next octave 130 image = ResizeProcessor.halfSize(currentOctave.getNextOctaveImage()); 131 132 octaveSize *= 2.0; // the size of the octave increases by a factor 133 // of two each iteration 134 135 // if the octaves array is not null we want to retain each octave. 136 if (octaves != null) 137 octaves.add(currentOctave); 138 } 139 140 // if a PyramidProcessor was specified in the options it should 141 // be applied now all the octaves are complete. 142 if (options.getPyramidProcessor() != null) { 143 options.getPyramidProcessor().process(this); 144 } 145 } 146}