0x00b0
0x00b0

Reputation: 363

Image processing Low Pass Filter in Matlab

Iam trying to create low pass filter in matlab

I've a matrix with size 12x6

56  147 56  52  147 52;
50  146 46  56  141 53;
59  142 145 147 147 46;
60  147 147 145 145 47;
52  145 35  47  146 52;
54  142 46  50  145 45;
56  52  56  52  56  52;
54  142 146 147 145 45;
59  142 53  45  147 46;
60  147 53  45  145 47;
52  145 124 145 142 52;
35  146 50  51  53  141;

with mask 1/25 (those matrix values are one) enter image description here

first i checked the matrix to image

M = [
56  147 56  52  147 52;
50  146 46  56  141 53;
59  142 145 147 147 46;
60  147 147 145 145 47;
52  145 35  47  146 52;
54  142 46  50  145 45;
56  52  56  52  56  52;
54  142 146 147 145 45;
59  142 53  45  147 46;
60  147 53  45  145 47;
52  145 124 145 142 52;
35  146 50  51  53  141;
];
f=uint8(M);
figure;imshow(f);title('Image from matrix');

image looks very small cause matrix size also small.

then I did this iteration with this following code :

a=f;
b = size(M); % check size my matrix
n=5;
n1=ceil(n/2);

lpf=(1/n^2)*ones(n) % multiply with ones values of matrix;


c=0;
h=0;
for i=n1:b(1)-n1
    for j=n1:b(2)-n1
        p=1;
        for k=1:n
            for l=1:n
            c=c+a(i-n1+k,j-n1+l)*lpf(l,l);
            end
        end
        d(i,j)=c;
        c=0;
    end
end
e=uint8(d);
figure;imshow(e);title('low pass image');

the result stored in e variable , when i check it my matrix become more small than before, the size become 9x3 :

0   0   0
0   0   0
0   0   105
0   0   105
0   0   97
0   0   97
0   0   89
0   0   90
0   0   97

Am i missing something ?

Upvotes: 2

Views: 174

Answers (1)

user15388024
user15388024

Reputation:

You are applying the filter to the region of your image where all values are set. That means you're cropping two columns on the left, two columns on the right, two rows on the top and two rows on the bottom. There are two bugs in your code

  1. You're cropping one row and one column more than necessary.
  2. You're implicitly padding the result with zeros on the left and on the top.

Additionally, you should preallocate your matrices.

Here is a fix

clear;
M = [
56  147 56  52  147 52;
50  146 46  56  141 53;
59  142 145 147 147 46;
60  147 147 145 145 47;
52  145 35  47  146 52;
54  142 46  50  145 45;
56  52  56  52  56  52;
54  142 146 147 145 45;
59  142 53  45  147 46;
60  147 53  45  145 47;
52  145 124 145 142 52;
35  146 50  51  53  141;
];

f = uint8(M);

a=f;
b = size(M);

n=5;
n1=ceil(n/2);

lpf=(1/n^2)*ones(n);

c=0;
h=0;
d = zeros(b(1) - 2 * n1 + 2, b(2) - 2 * n1 + 2);
for i=n1:b(1)-n1+1
    for j=n1:b(2)-n1+1
        for k=1:n
            for l=1:n
                c=c+a(i-n1+k,j-n1+l)*lpf(l,l);
            end
        end
        d(i - n1 + 1, j - n1 + 1)=c;
        c=0;
    end
end
e=uint8(d);

If you want the result to keep the size of the original image you have to choose a strategy for the edges. Common strategies are zero-padding, padding with the last value or circular padding but the strategy strongly depends on the use-case.

Here you can find more details.

Upvotes: 3

Related Questions