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.math.geometry.transforms.estimation.sampling; 031 032import java.util.ArrayList; 033import java.util.Collection; 034import java.util.List; 035import java.util.Random; 036 037import org.openimaj.citation.annotation.Reference; 038import org.openimaj.citation.annotation.ReferenceType; 039import org.openimaj.math.geometry.point.Point2d; 040import org.openimaj.util.CollectionSampler; 041import org.openimaj.util.pair.IndependentPair; 042 043/** 044 * Implementation of the bucketing sampling strategy proposed by Zhang et al to 045 * try and ensure a good spatial distribution of point-pairs for estimation of 046 * geometric transforms and the fundamental matrix. 047 * <p> 048 * Works by splitting the space of first image points into a number of buckets. 049 * When selecting the sample, a bucket is chosen at random, with the probability 050 * of the bucket being picked weighted towards buckets with more points. Then a 051 * point pair is picked using a uniform random from within the bucket. 052 * Additionally, the algorithm attempts to pick unique buckets for each point by 053 * attempting to discount previously selected buckets if possible. 054 * 055 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 056 */ 057@Reference( 058 type = ReferenceType.Article, 059 author = { "Zhengyou Zhang", "Rachid Deriche", "Olivier Faugeras", "Quang-Tuan Luong" }, 060 title = "A robust technique for matching two uncalibrated images through the recovery of the unknown epipolar geometry ", 061 year = "1995", 062 journal = "Artificial Intelligence ", 063 pages = { "87 ", " 119" }, 064 url = "http://www.sciencedirect.com/science/article/pii/0004370295000224", 065 note = "Special Volume on Computer Vision ", 066 number = "1--2", 067 volume = "78", 068 customData = { 069 "issn", "0004-3702", 070 "doi", "http://dx.doi.org/10.1016/0004-3702(95)00022-4", 071 "keywords", "Correlation " 072 }) 073public class BucketingSampler2d implements CollectionSampler<IndependentPair<Point2d, Point2d>> { 074 private class Bucket { 075 private List<IndependentPair<Point2d, Point2d>> buckets = new ArrayList<IndependentPair<Point2d, Point2d>>(); 076 private double interval; 077 } 078 079 /** 080 * Default number of buckets per dimension 081 */ 082 public final static int DEFAULT_N_BUCKETS_PER_DIM = 8; 083 084 /** 085 * Maximum allowed number of trials in picking a bucket that has not been 086 * previously picked. 087 */ 088 public static int NUM_TRIALS = 100; 089 090 private Random rng; 091 private Bucket[] bucketList; 092 093 private int nBucketsX; 094 095 private int nBucketsY; 096 097 /** 098 * Construct the sampler with the default number of buckets in the x and y 099 * dimensions (8). 100 */ 101 public BucketingSampler2d() { 102 this(DEFAULT_N_BUCKETS_PER_DIM, DEFAULT_N_BUCKETS_PER_DIM); 103 } 104 105 /** 106 * Construct the sampler with the given number of buckets in each dimension. 107 * 108 * @param nBucketsX 109 * number of buckets in the x dimension 110 * @param nBucketsY 111 * number of buckets in the y dimension 112 */ 113 public BucketingSampler2d(int nBucketsX, int nBucketsY) { 114 this.nBucketsX = nBucketsX; 115 this.nBucketsY = nBucketsY; 116 this.rng = new Random(); 117 } 118 119 @Override 120 public void setCollection(Collection<? extends IndependentPair<Point2d, Point2d>> collection) { 121 // find max, max 122 float minx = Float.MAX_VALUE; 123 float maxx = -Float.MAX_VALUE; 124 float miny = Float.MAX_VALUE; 125 float maxy = -Float.MAX_VALUE; 126 127 for (final IndependentPair<Point2d, Point2d> pair : collection) { 128 final Point2d first = pair.firstObject(); 129 final float x = first.getX(); 130 final float y = first.getY(); 131 132 if (x < minx) 133 minx = x; 134 if (x > maxx) 135 maxx = x; 136 if (y < miny) 137 miny = y; 138 if (y > maxy) 139 maxy = y; 140 } 141 142 minx -= 0.001; 143 maxx += 0.001; 144 miny -= 0.001; 145 maxy += 0.001; 146 147 // reset buckets 148 final Bucket[][] buckets = new Bucket[nBucketsY][nBucketsX]; 149 150 // build buckets 151 final double bucketWidth = (maxx - minx) / (double) (buckets[0].length); 152 final double bucketHeight = (maxy - miny) / (double) (buckets.length); 153 int numNonEmptyBuckets = 0; 154 155 for (final IndependentPair<Point2d, Point2d> pair : collection) { 156 final Point2d first = pair.firstObject(); 157 final float x = first.getX(); 158 final float y = first.getY(); 159 160 final int bx = (int) ((x - minx) / bucketWidth); 161 final int by = (int) ((y - miny) / bucketHeight); 162 163 if (buckets[by][bx] == null) { 164 buckets[by][bx] = new Bucket(); 165 numNonEmptyBuckets++; 166 } 167 168 buckets[by][bx].buckets.add(pair); 169 } 170 171 // compute intervals and assign buckets to the list 172 bucketList = new Bucket[numNonEmptyBuckets]; 173 for (int y = 0, i = 0; y < buckets.length; y++) { 174 for (int x = 0; x < buckets.length; x++) { 175 if (buckets[y][x] != null) { 176 buckets[y][x].interval = (double) buckets[y][x].buckets.size() / (double) collection.size(); 177 bucketList[i++] = buckets[y][x]; 178 } 179 } 180 } 181 } 182 183 @Override 184 public List<IndependentPair<Point2d, Point2d>> sample(int nItems) { 185 final List<IndependentPair<Point2d, Point2d>> sample = 186 new ArrayList<IndependentPair<Point2d, Point2d>>(nItems); 187 final boolean[] selected = new boolean[bucketList.length]; 188 int nSelectedBuckets = 0; 189 190 for (int i = 0; i < nItems; i++) { 191 // attempt to pick a bucket that hasn't been picked already 192 int selectedBucketIdx = 0; 193 for (int j = 0; j < NUM_TRIALS; j++) { 194 final double r = rng.nextDouble(); 195 double sum = 0; 196 selectedBucketIdx = -1; 197 198 do { 199 sum += bucketList[++selectedBucketIdx].interval; 200 } while (sum < r); 201 202 if (!selected[j] || nSelectedBuckets >= selected.length) { 203 nSelectedBuckets++; 204 break; 205 } 206 } 207 208 // now pick a value from that bucket 209 selected[selectedBucketIdx] = true; 210 final int selectedPairIdx = rng.nextInt(bucketList[selectedBucketIdx].buckets.size()); 211 sample.add(bucketList[selectedBucketIdx].buckets.get(selectedPairIdx)); 212 } 213 214 return sample; 215 } 216}