Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IMFILTER in matlab

Tags:

matlab

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
like image 622
Aya Abdelsalam Avatar asked May 20 '12 09:05

Aya Abdelsalam


People also ask

How does Imfilter work in Matlab?

The imfilter function computes the value of each output pixel using double-precision, floating-point arithmetic. If the result exceeds the range of the data type, then imfilter truncates the result to the allowed range of the data type. If it is an integer data type, then imfilter rounds fractional values.

How do you make a motion filter in Matlab?

Create Various Filters and Filter an Image Create a motion filter and use it to blur the image. Display the blurred image. H = fspecial('motion',20,45); MotionBlur = imfilter(I,H,'replicate'); imshow(MotionBlur); Create a disk filter and use it to blur the image.

How do you use 2d filter in Matlab?

Y = filter2( H , X ) applies a finite impulse response filter to a matrix of data X according to coefficients in a matrix H . Y = filter2( H , X , shape ) returns a subsection of the filtered data according to shape . For example, Y = filter2(H,X,'valid') returns only filtered data computed without zero-padded edges.

How do you add salt and pepper sound to an image in Matlab?

J = imnoise( I ,'salt & pepper') adds salt and pepper noise, with default noise density 0.05. This affects approximately 5% of pixels. J = imnoise( I ,'salt & pepper', d ) adds salt and pepper noise, where d is the noise density. This affects approximately d*numel(I) pixels.


1 Answers

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

The result is as follows:

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

And with imfilter, it is:

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.

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!

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
like image 166
petrichor Avatar answered Sep 30 '22 13:09

petrichor