View Javadoc

1   /**
2    * This source code file is part of a direct port of Stan Birchfield's implementation
3    * of a Kanade-Lucas-Tomasi feature tracker. The original implementation can be found
4    * here: http://www.ces.clemson.edu/~stb/klt/
5    *
6    * As per the original code, the source code is in the public domain, available
7    * for both commercial and non-commercial use.
8    */
9   package org.openimaj.video.tracking.klt;
10  
11  import org.openimaj.image.FImage;
12  import org.openimaj.image.processing.convolution.FGaussianConvolve;
13  
14  /**
15   * A simple Gaussian pyramid
16   * 
17   * @author Stan Birchfield
18   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
19   */
20  public class Pyramid {
21  	int subsampling;
22  	int nLevels;
23  	FImage [] img;
24  	int [] ncols, nrows;
25  
26  	/**
27  	 * @param ncols
28  	 * @param nrows
29  	 * @param subsampling
30  	 * @param nlevels
31  	 */
32  	public Pyramid(int ncols, int nrows, int subsampling, int nlevels) {
33  		if (subsampling != 2 && subsampling != 4 && 
34  				subsampling != 8 && subsampling != 16 && subsampling != 32)
35  			throw new RuntimeException("(_KLTCreatePyramid)  Pyramid's subsampling must be either 2, 4, 8, 16, or 32");
36  
37  		img = new FImage[nlevels];
38  		this.ncols = new int[nlevels];
39  		this.nrows = new int[nlevels];
40  
41  		/* Set parameters */
42  		this.subsampling = subsampling;
43  		this.nLevels = nlevels;
44  
45  		/* Allocate memory for each level of pyramid and assign pointers */
46  		for (int i = 0 ; i < nlevels ; i++) {
47  			this.img[i] =  new FImage(ncols, nrows);
48  			this.ncols[i] = ncols;  
49  			this.nrows[i] = nrows;
50  			ncols /= subsampling;  
51  			nrows /= subsampling;
52  		}
53  	}
54  
55  	/*********************************************************************
56  	 * 
57  	 */
58  	void computePyramid(FImage img, float sigma_fact) {
59  		FImage currimg, tmpimg;
60  		int ncols = img.width, nrows = img.height;
61  		int subsampling = this.subsampling;
62  		int subhalf = subsampling / 2;
63  		float sigma = subsampling * sigma_fact;  /* empirically determined */
64  		int i, x, y;
65  
66  		if (subsampling != 2 && subsampling != 4 && 
67  				subsampling != 8 && subsampling != 16 && subsampling != 32)
68  			throw new RuntimeException("(_KLTComputePyramid)  Pyramid's subsampling must be either 2, 4, 8, 16, or 32");
69  
70  		/* Copy original image to level 0 of pyramid */
71  		this.img[0] = img.clone();
72  
73  		currimg = img;
74  		for (i = 1 ; i < this.nLevels ; i++)  {
75  			//tmpimg = FImage(nrows, ncols);
76  			//_KLTComputeSmoothedImage(currimg, sigma, tmpimg);
77  			tmpimg = currimg.process(new FGaussianConvolve(sigma));
78  
79  			/* Subsample */
80  			ncols /= subsampling;  nrows /= subsampling;
81  			for (y = 0 ; y < nrows ; y++)
82  				for (x = 0 ; x < ncols ; x++)
83  					this.img[i].pixels[y][x] = tmpimg.pixels[(subsampling*y+subhalf)][(subsampling*x+subhalf)];
84  
85  			/* Reassign current image */
86  			currimg = this.img[i];
87  		}
88  	}
89  }