有没有办法在下面的代码中使用低通和高通滤波器并将它们组合成一个内核并应用一个conv2()函数?
注意:length(lfilter)= 21,length(hfilter)= 81。
我们在最后一步中基本上做的是从图像中删除大对象(在已经用高斯模糊去除非常小的对象之后)。
properties (Constant)
minStar = 2; % min star radius
maxStar = 8; % max star radius
threshold = 12;
end
function filter2(this)
normalize = @(x) x/sum(x);
lfilter = normalize(exp(-((-ceil(5*this.minStar):ceil(5*this.minStar))/(2*this.minStar)).^2));
hfilter = normalize(exp(-((-ceil(5*this.maxStar):ceil(5*this.maxStar))/(2*this.maxStar)).^2));
this.low = conv2(lfilter',lfilter,this.raw,'same');
this.high = conv2(hfilter',hfilter,this.raw,'same');
this.filtered = this.low - this.high;
this.foreground = this.filtered > this.threshold;
end
答案 0 :(得分:3)
由于卷积运算符是关联的:
conv( a, conv(b,c) ) == conv( conv(a,b), c )
你应该能够将两个内核合并为一个,只需将它们相互卷积即可。
在你的情况下,这样的事情应该有效:
new_kernel = conv2(lfilter',lfilter, conv2(hfilter',hfilter), 'same');
卷积也是可交换的,所以你执行卷积的顺序不应该是重要的。
编辑:正如我在下面的评论中解释的那样,提问者执行四次1D卷积的方法最终比单个2D卷积更快。
答案 1 :(得分:1)
我刚在matlab论坛上得到了答案。 http://www.mathworks.com/matlabcentral/answers/169713-combine-convolution-filters-bandpass-into-a-single-kernel
要点是你必须使用填充来填充较短过滤器的两侧,然后你可以组合向量。
卷积是线性操作所以是的,您可以将两个过滤操作合并为一个。只需使过滤器具有相同的大小并添加/减去它们即可。例如:
lfilter = normalize(exp(-((-ceil(5*minStar):ceil(5*minmax))/(2*this.minStar)).^2));
hfilter = normalize(exp(-((-ceil(5*maxStar):ceil(5*minmax))/(2*this.maxStar)).^2));
padlength = (length(hfilter) - length(lfilter))/2;
lfilter = padarray(lfilter, [0 padlength]);
lhfilter = lfilter - hfilter;
this.filtered = conv2(lhfilter',lhfilter,this.raw,'same');