-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyramid.m
318 lines (204 loc) · 9.69 KB
/
Pyramid.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(* ::Package:: *)
(************************************************************************)
(* This file was generated automatically by the Mathematica front end. *)
(* It contains Initialization cells from a Notebook file, which *)
(* typically will have the same name as this file except ending in *)
(* ".nb" instead of ".m". *)
(* *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs. Doing so is equivalent *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end. *)
(* *)
(* DO NOT EDIT THIS FILE. This entire file is regenerated *)
(* automatically each time the parent Notebook file is saved in the *)
(* Mathematica front end. Any changes you make to this file will be *)
(* overwritten. *)
(************************************************************************)
(* ::Input::Initialization:: *)
(* :Title: Image Pyramid Package *)
(* :Context: ImageTools`Pyramid` *)
(* :Author: Flip Phillips
Recent modifications of $Date: 1999/04/11 21:46:38 $ by $Author: cvs $ *)
(* :Summary:
This package adds various image pyramid operations to Mathematica.
*)
(* :Package Version: $Revision: 1.4 $ *)
(* :Mathematica Version: 4.0 *)
(* :Copyright: Copyright 1999, Flip Phillips, All Rights Reserved. *)
(* :History:
$Log: Pyramid.nb,v $
Revision 1.4 1999/04/11 21:46:38 cvs
made ImagePyramid data structure
added filter bank stuff inc. convolution
Revision 1.3 1999/04/11 03:31:01 cvs
Optimized the recombine function
Created Gaussian->Laplacian and orthogonal function
Added ShowPyramid utility
Revision 1.2 1999/04/06 22:05:06 cvs
changed downsampling procedure to do a better job
also made filter 9x9 instead of 3x3, turns out that the convolution routine in mma4 is quite good so speed hit isn't that bad
Revision 1.1 1999/04/06 17:07:46 cvs
initial check-in
*)
(* :Keywords:
Image Pyramid, Gaussian Pyramids
*)
(* :Limitations:
Needs the ListConvolve stuff from MMa 4.0
*)
(* :Discussion: *)
(* ::Input::Initialization:: *)
BeginPackage["ImageTools`Pyramid`",
"Statistics`NormalDistribution`",
"Statistics`Common`DistributionsCommon`",
"ExpTools`LineTools`"];
(* ::Input::Initialization:: *)
Pyramid::usage="Pyramid.m is a package which provides a few tools for doing image pyramid decompositions."
(* ::Input::Initialization:: *)
SameSizeConvolve::usage="SameSizeConvolve[image,kernel] convolves image with kernel and returns an image the same size as the input (i.e. trimmed). It, by default, wraps, since we're doing texture calculations and we'd expect the frequency information to be consistent across the texel (so to speak)."
(* ::Input::Initialization:: *)
DownSample2::usage="DownSample2[image] returns an image that is down sampled by a factor of two."
(* ::Input::Initialization:: *)
UpSample2::usage="UpSample2[image] returns an image that is up sampled by a factor of two."
(* ::Input::Initialization:: *)
MakeConvolutionPyramid::usage="MakeConvolutionPyramid[image,kernel] returns an image pyramid in which each image is convolved with kernel and down-sampled."
(* ::Input::Initialization:: *)
MakeDifferencePyramid::usage="MakeDifferencePyramid[pyramid] creates a difference pyramid where each level contains \!\(\*SubscriptBox[\(p\), \(i\)]\)-UpSample2[\!\(\*SubscriptBox[\(p\), \(i + 1\)]\)]."
(* ::Input::Initialization:: *)
MakeGaussianPyramid::usage="MakeGaussianPyramid[image] creates a Gaussian pyramid for image."
(* ::Input::Initialization:: *)
MakeLaplacianPyramid::usage="MakeLaplacianPyramid[image] creates a Laplacian pyramid for image."
(* ::Input::Initialization:: *)
MakeLaplacianPyramidFromGaussianPyramid::usage="MakeLaplacianPyramidFromGaussianPyramid[pyramid] creates a Laplacian pyramid from the supplied Gaussian pyramid."
(* ::Input::Initialization:: *)
ExpandPyramid::usage="ExpandPyramid[pyramid] creates a stack of images all at the same resolution as the highest res image in the pyramid."
(* ::Input::Initialization:: *)
RecombineLaplacianPyramid::usage="RecombineLaplacianPyramid[image] recombines a pyramid of Laplacians into one image."
(* ::Input::Initialization:: *)
MakeGaussianPyramidFromLaplacianPyramid::usage="MakeGaussianPyramidFromLaplacianPyramid[pyramid] makes a Gaussian pyramid from a given Laplacian pyramid."
(* ::Input::Initialization:: *)
ShowPyramid::usage="ShowPyramid[pyramid] draws the levels of a given pyramid."
(* ::Input::Initialization:: *)
ImagePyramid::usage="ImagePyramid[data] represents an image pyramid data structure."
(* ::Input::Initialization:: *)
ConvolvePyramid::usage="ConvolvePyramid[pyramid,kernel] convolves each level of pyramid with kernel."
(* ::Input::Initialization:: *)
ConvolvePyramidWithBank::usage="ConvolvePyramidWithBank[pyramid,kernelbank] returns a list of pyramids, each pyramid is convolved with one of the kernels in the bank"
(* ::Input::Initialization:: *)
ConvolveImageWithBank::usage="ConvolveImageWithBank[image,kernelbank] returns a list of images, each is convolved with one of the kernels from the bank."
(* ::Input::Initialization:: *)
MakeOrientedKernelBank::usage="MakeOrientedKernelBank[dim,f] returns a list of 8 oriented kernels of size 2dim-1, generated using f[x] where x is the signed distance from the center line."
(* ::Input::Initialization:: *)
Begin["`Private`"]
(* ::Input::Initialization:: *)
Unprotect[{SameSizeConvolve,DownSample2,UpSample2,MakeConvolutionPyramid,MakeDifferencePyramid,MakeGaussianPyramid,MakeLaplacianPyramid,MakeLaplacianPyramidFromGaussianPyramid,ExpandPyramid,RecombineLaplacianPyramid,MakeGaussianPyramidFromLaplacianPyramid,ShowPyramid,ImagePyramid,
ConvolvePyramid,ConvolvePyramidWithBank,ConvolveImageWithBank,
MakeOrientedKernelBank}];
(* ::Input::Initialization:: *)
Format[t_ImagePyramid]:="ImagePyramid[ <> ]"
(* ::Input::Initialization:: *)
normalize2D[k_]:=Module[{sf},
sf=1/Plus@@Flatten[k];
Map[#*sf&,k,{2}]]
(* ::Input::Initialization:: *)
Kgauss=normalize2D[Table[N[PDF[NormalDistribution[0,1],VectorLength[{x,y}]]],{x,-3,3,1},{y,-3,3,1}]];
(* ::Input::Initialization:: *)
ImagePyramid::notpyr="Not a valid image pyramid";
(* ::Input::Initialization:: *)
SameSizeConvolve[kernel_,image_]:=Module[{i,trim,dims},
i=ListConvolve[kernel,image,{1,-1}];
trim=(Dimensions[kernel]-1)/2;
dims=Dimensions[i];
Take[i,{1+trim[[1]],dims[[1]]-trim[[1]]},{1+trim[[2]],dims[[2]]-trim[[2]]}]]
(* ::Input::Initialization:: *)
DownSample2[m_]:=(Take[m,{1,Length[m],2},{1,Length[m],2}]+
Take[m,{1,Length[m],2},{2,Length[m],2}]+
Take[m,{2,Length[m],2},{1,Length[m],2}]+
Take[m,{2,Length[m],2},{2,Length[m],2}])/4;
(* ::Input::Initialization:: *)
UpSample2[m_]:=Module[{},
thicken[l_]:=Module[{x},
x=Flatten[Map[Replace[#,x_:>{x,x}]&,l]];
{x,x}];
Flatten[Map[thicken,m,{1}],1]]
(* ::Input::Initialization:: *)
MakeConvolutionPyramid[image_,kernel_]:=Module[{p},
p={image};
MakePyramid[i_,k_]:=Module[{s,t},
If[Length[i]>1,
s=SameSizeConvolve[k,i];
t=DownSample2[s];
AppendTo[p,t];
MakePyramid[t,k],
Return[i]]];
MakePyramid[image,kernel];
ImagePyramid[Reverse[p],"Convolution"]
]
(* ::Input::Initialization:: *)
MakeDifferencePyramid[pyramid_ImagePyramid]:=Module[{i,ip},
ip=pyramid[[1]];
ImagePyramid[
Prepend[Table[ip[[i+1]]-UpSample2[ip[[i]]],{i,1,Length[ip]-1}],ip[[1]]],
"Difference"]
]
(* ::Input::Initialization:: *)
MakeGaussianPyramid[image_]:=
MakeConvolutionPyramid[image,Kgauss]
(* ::Input::Initialization:: *)
MakeLaplacianPyramidFromGaussianPyramid[gp_]:=
MakeDifferencePyramid[gp]
(* ::Input::Initialization:: *)
MakeLaplacianPyramid[image_]:=
MakeLaplacianPyramidFromGaussianPyramid[MakeGaussianPyramid[image]]
(* ::Input::Initialization:: *)
ExpandPyramid[pyramid_ImagePyramid]:=Module[{i,p},
p=pyramid[[1]];
ImagePyramid[
{Table[Nest[UpSample2,p[[i]],Length[p]-i],{i,1,Length[p]}],
"Expanded"}]
]
(* ::Input::Initialization:: *)
RecombineLaplacianPyramid[pyramid_ImagePyramid]:=Module[{lp},
lp=pyramid[[1]];
Fold[(UpSample2[#1]+#2)&,First[lp],Rest[lp]]
]
(* ::Input::Initialization:: *)
MakeGaussianPyramidFromLaplacianPyramid[pyramid_ImagePyramid]:=Module[{lp},
lp=pyramid[[1]];
ImagePyramid[
FoldList[(UpSample2[#1]+#2)&,First[lp],Rest[lp]],
"Gaussian"]
]
(* ::Input::Initialization:: *)
ShowPyramid[pyramid_ImagePyramid]:=Module[{ip},
ip=pyramid[[1]];
Map[ListDensityPlot[#,Mesh->False,Frame->False]&,ip]]
(* ::Input::Initialization:: *)
ImagePyramid[ip_,type_][i_,x_,y_]:=ip[[i,x,y]]
(* ::Input::Initialization:: *)
ConvolvePyramid[pyramid_ImagePyramid,kernel_]:=Module[{ip},
ip=pyramid[[1]];
ImagePyramid[
Map[SameSizeConvolve[kernel,#]&,ip],
"Convolve"]
]
(* ::Input::Initialization:: *)
ConvolvePyramidWithBank[pyramid_ImagePyramid,bank_]:=Map[ConvolvePyramid[pyramid,#]&,bank]
(* ::Input::Initialization:: *)
ConvolveImageWithBank[i_,bank_]:=
Map[SameSizeConvolve[#,i]&,bank]
(* ::Input::Initialization:: *)
MakeOrientedKernelBank[dim_,f_]:=Module[{x,y,u,v,kernels},
kernels=
Table[Table[N[f[{u,v}.{x,y}]],{x,-dim,dim,1},{y,-dim,dim,1}],{u,-1,1,1},{v,-1,1,1}];
Drop[Flatten[kernels,1],{5}]
]
(* ::Input::Initialization:: *)
End[]
(* ::Input::Initialization:: *)
Protect[{SameSizeConvolve,DownSample2,UpSample2,MakeConvolutionPyramid,MakeDifferencePyramid,MakeGaussianPyramid,MakeLaplacianPyramid,MakeLaplacianPyramidFromGaussianPyramid,ExpandPyramid,RecombineLaplacianPyramid,MakeGaussianPyramidFromLaplacianPyramid,ShowPyramid,ImagePyramid,
ConvolvePyramid,ConvolvePyramidWithBank,ConvolveImageWithBank,
MakeOrientedKernelBank}];
(* ::Input::Initialization:: *)
EndPackage[]