1
2
3
4
5
6
7
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
16
17
18
19
20 public class Pyramid {
21 int subsampling;
22 int nLevels;
23 FImage [] img;
24 int [] ncols, nrows;
25
26
27
28
29
30
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
42 this.subsampling = subsampling;
43 this.nLevels = nlevels;
44
45
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;
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
71 this.img[0] = img.clone();
72
73 currimg = img;
74 for (i = 1 ; i < this.nLevels ; i++) {
75
76
77 tmpimg = currimg.process(new FGaussianConvolve(sigma));
78
79
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
86 currimg = this.img[i];
87 }
88 }
89 }