1 /*********************************************************************
2 * pyramid.c
3 *
4 *********************************************************************/
5
6 /* Standard includes */
7 #include <stdlib.h> /* malloc() ? */
8 #include <string.h> /* memset() ? */
9 #include <math.h> /* */
10
11 /* Our includes */
12 #include "base.h"
13 #include "error.h"
14 #include "convolve.h" /* for computing pyramid */
15 #include "pyramid.h"
16
17
18 /*********************************************************************
19 *
20 */
21
_KLTCreatePyramid(int ncols,int nrows,int subsampling,int nlevels)22 _KLT_Pyramid _KLTCreatePyramid(
23 int ncols,
24 int nrows,
25 int subsampling,
26 int nlevels)
27 {
28 _KLT_Pyramid pyramid;
29 int nbytes = sizeof(_KLT_PyramidRec) +
30 nlevels * sizeof(_KLT_FloatImage *) +
31 nlevels * sizeof(int) +
32 nlevels * sizeof(int);
33 int i;
34
35 if (subsampling != 2 && subsampling != 4 &&
36 subsampling != 8 && subsampling != 16 && subsampling != 32)
37 KLTError("(_KLTCreatePyramid) Pyramid's subsampling must "
38 "be either 2, 4, 8, 16, or 32");
39
40
41 /* Allocate memory for structure and set parameters */
42 pyramid = (_KLT_Pyramid)malloc(nbytes);
43
44 /* Set parameters */
45 pyramid->subsampling = subsampling;
46 pyramid->nLevels = nlevels;
47 pyramid->img = (_KLT_FloatImage *) (pyramid + 1);
48 pyramid->ncols = (int *) (pyramid->img + nlevels);
49 pyramid->nrows = (int *) (pyramid->ncols + nlevels);
50
51 /* Allocate memory for each level of pyramid and assign pointers */
52 for (i = 0 ; i < nlevels ; i++) {
53 pyramid->img[i] = _KLTCreateFloatImage(ncols, nrows);
54 pyramid->ncols[i] = ncols;
55 pyramid->nrows[i] = nrows;
56 ncols /= subsampling;
57 nrows /= subsampling;
58 }
59
60 return pyramid;
61 }
62
63
64 /*********************************************************************
65 *
66 */
67
_KLTFreePyramid(_KLT_Pyramid pyramid)68 void _KLTFreePyramid(
69 _KLT_Pyramid pyramid)
70 {
71 int i;
72
73 /* Free images */
74 for (i = 0 ; i < pyramid->nLevels ; i++)
75 _KLTFreeFloatImage(pyramid->img[i]);
76
77 /* Free structure */
78 free(pyramid);
79 }
80
81
82 /*********************************************************************
83 *
84 */
85
_KLTComputePyramid(_KLT_FloatImage img,_KLT_Pyramid pyramid,float sigma_fact)86 void _KLTComputePyramid(
87 _KLT_FloatImage img,
88 _KLT_Pyramid pyramid,
89 float sigma_fact)
90 {
91 _KLT_FloatImage currimg, tmpimg;
92 int ncols = img->ncols, nrows = img->nrows;
93 int subsampling = pyramid->subsampling;
94 int subhalf = subsampling / 2;
95 float sigma = subsampling * sigma_fact; /* empirically determined */
96 int oldncols;
97 int i, x, y;
98
99 if (subsampling != 2 && subsampling != 4 &&
100 subsampling != 8 && subsampling != 16 && subsampling != 32)
101 KLTError("(_KLTComputePyramid) Pyramid's subsampling must "
102 "be either 2, 4, 8, 16, or 32");
103
104 /* Copy original image to level 0 of pyramid */
105 memcpy(pyramid->img[0]->data, img->data, ncols*nrows*sizeof(float));
106
107 currimg = img;
108 for (i = 1 ; i < pyramid->nLevels ; i++) {
109 tmpimg = _KLTCreateFloatImage(ncols, nrows);
110 _KLTComputeSmoothedImage(currimg, sigma, tmpimg);
111
112
113 /* Subsample */
114 oldncols = ncols;
115 ncols /= subsampling; nrows /= subsampling;
116 for (y = 0 ; y < nrows ; y++)
117 for (x = 0 ; x < ncols ; x++)
118 pyramid->img[i]->data[y*ncols+x] =
119 tmpimg->data[(subsampling*y+subhalf)*oldncols +
120 (subsampling*x+subhalf)];
121
122 /* Reassign current image */
123 currimg = pyramid->img[i];
124
125 _KLTFreeFloatImage(tmpimg);
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140