-
Notifications
You must be signed in to change notification settings - Fork 7
/
separableConv.m
42 lines (37 loc) · 1.2 KB
/
separableConv.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
function result = separableConv(im, xkernels, ykernels)
%
% Two-dimensional convolution with X-Y separable kernels.
%
% im is the input matric. im is reflected on all sides before
% convolution.
% xkernels and ykernels are both row vectors. If ykernels is not
% given, then use xkernels as ykernels.
% If xkernels and ykernels are matrices, each row is taken as
% one convolution kernel and convolved with the image, and the
% sum of the results is returned.
%
% This function is useful when a 2-D filter can be represented by
% a linear combination of several separable filters. If the number
% of separable filters that form the 2-D filter is not too large,
% this can be substantially faster than conv2.
%
% From (put citation here)
%
% Xuemei Zhang 3/1/96
if (nargin < 3)
ykernels = xkernels;
end
imsize = size(im);
w1 = pad4conv(im, size(xkernels,2), 2);
result = 0;
for j=1:size(xkernels,1)
% first convovle in the horizontal direction
p = conv2(w1, xkernels(j,:));
p = resize(p, imsize);
% then the vertical direction
w2 = pad4conv(p, size(ykernels,2), 1);
p = conv2(w2, (ykernels(j,:))');
p = resize(p, imsize);
% result is sum of several separable convolutions
result = result + p;
end