img=im2double(imread('cameraman.tif'));
yourFilterSize=[3 5] %3 rows, 5 columns, can be anything
%two 1D Gaussians
g_x=fspecial('gaussian',[1 yourFilterSize(2)]);
g_y=fspecial('gaussian',[yourFilterSize(1) 1]);
%applying 1D gaussian in X-direction to the original image
img_X=imfilter(img,g_x);
%applying 1D gaussian in Y-direction to img_X
img_XY=imfilter(img_X,g_y); %DONE
%verifying that the result is correct
g_xy=fspecial('gaussian',yourFilterSize);
img_XY2D=imfilter(img,g_xy);
max(max(abs(img_XY-img_XY2D))) %this should be very small,
%of the order of machine precision
%for the result to be correct
1 回答
使用2D高斯的卷积可以使用两个1D高斯执行:
您可以在MATLAB中执行以下操作:
补充阅读:
在Stackoverflow.com上回答
This,它向您展示了如何确定给定内核是否可分离 . 例如,Gaussian是可分离的,而磁盘内核则不是 .
Original article上述问题 .
为什么你更喜欢两个1D卷积而不是一个2D卷积(也在上面提到的链接2中给出):
假设您有一个大小为
MxN
的图像和一个大小为PxQ
的滤镜,那么对于2D卷积,您需要~ M*P*N*Q
乘法和加法 . 对于两个1D过滤器(大小为P
和Q
),您只需要~ MNP+MNQ = MN(P+Q)
操作 . 因此,您获得了PQ/(P+Q)
的加速 .