在Matlab中实现IMFILTER [英] Implementing IMFILTER in matlab

查看:753
本文介绍了在Matlab中实现IMFILTER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用imfilter过滤掉图像.我应该得到与imfilter相同的结果,但是我不断得到不同的结果.有人可以告诉我我哪里出问题了吗?

I am trying to filter an image with out using imfilter. I should get the same results as imfilter but I keep getting diffrent results. Can someone tell me where I went wrong?

orignal=imread('obj6__17.png');
filter=1/9*[-1 -1 -1 ; -1 17 -1 ; -1 -1 -1];
s=size(orignal);
r=zeros(s(1));
temp = zeros(3);

for i= 2: s(1)-1
for j = 2: s(2)-1

    for n= 1: 3
        for m= 1:3
            temp(n,m)=orignal(i+2-n,j+2-m)*filter(n,m);
        end
    end
    r(i,j)=sum(single(sum(temp)));
end
end

推荐答案

r的大小应与我认为的原始大小相同.而且我不明白为什么您要使用single转换为单精度.无论如何,我认为您想执行以下操作:

The size of r should be the same as the original I think. And I don't understand why you convert to single precision using single. Anyway, I think you want to do the following:

%# Let's first create a small test image from the built-in peppers image
original = im2double(imread('peppers.png'));
original = original(1:5,1:8,1);

filter = 1/9 * [-1 -1 -1 ; -1 17 -1 ; -1 -1 -1];
s = size(original);
r = zeros(s);

for i = 2:s(1)-1
    for j = 2:s(2)-1
        temp = original(i-1:i+1,j-1:j+1) .* filter;
        r(i,j) = sum(temp(:));
    end
end

结果如下:

r =

         0         0         0         0         0         0         0         0
         0    0.2336    0.2157    0.2514    0.2436    0.2257    0.2344         0
         0    0.2453    0.2444    0.2671    0.2693    0.2418    0.2240         0
         0    0.2741    0.2728    0.2397    0.2505    0.2375    0.2436         0
         0         0         0         0         0         0         0         0

imfilter是:

r2 = imfilter(original, filter)

r2 =

    0.3778    0.3325    0.3307    0.3442    0.3516    0.3312    0.3163    0.3856
    0.3298    0.2336    0.2157    0.2514    0.2436    0.2257    0.2344    0.3386
    0.3434    0.2453    0.2444    0.2671    0.2693    0.2418    0.2240    0.3512
    0.3272    0.2741    0.2728    0.2397    0.2505    0.2375    0.2436    0.3643
    0.3830    0.3181    0.3329    0.3403    0.3508    0.3272    0.3412    0.4035

如您所见,除边框上的结果外,结果是相同的.有几种策略可以在边界上进行计算,例如将图像镜像到边界之外,保持边界不变等.请阅读

As you see, the results are the same except the ones on the borders. There are a few strategies to compute the ones on the borders as mirroring the image to the out of the borders, keeping them the same, etc. Please read the documentation of imfilter and choose one strategy.

请注意,由于滤波器在两个方向上都是对称的,因此我在这里没有翻转filter.我建议您避免循环!您的代码中有深度为4的嵌套循环!

Note that I didn't flipped filter here since the filter is symmetric in both directions. And I recommend you to avoid loops! There are nested loops of depth four in your code!

最后,您可以使用2-D卷积与imfilter相同:

Lastly, you can use 2-D convolution to do the same as imfilter:

r3 = conv2(original, filter, 'same');

r3 =

    0.3778    0.3325    0.3307    0.3442    0.3516    0.3312    0.3163    0.3856
    0.3298    0.2336    0.2157    0.2514    0.2436    0.2257    0.2344    0.3386
    0.3434    0.2453    0.2444    0.2671    0.2693    0.2418    0.2240    0.3512
    0.3272    0.2741    0.2728    0.2397    0.2505    0.2375    0.2436    0.3643
    0.3830    0.3181    0.3329    0.3403    0.3508    0.3272    0.3412    0.4035

这篇关于在Matlab中实现IMFILTER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆