Reputation: 45
I am having a problem with the output. I am using 5*5 filter for making image smooth. As average filter smooth our image but this one is making the image more dull. Can anyone help me with this problem?
Input Image is:
img_2 = imread('White-Bars.png');
filter = ones(5 , 5)/25;
working_img = img_2(:,:,1);
img_4 = img_2(:,:,1);
[rows,cols] = size(working_img); sum=0;
for row=4:(rows-3)
for col=4:(cols-3)
for rowindex=(-2): (3)
for colindex=(-2): (3)
img_4(row,col) = mean2(double(working_img(row+rowindex,col+colindex)) * filter(1:5,1:5));
end
end
end
end
subplot(1,2,1);
imshow(working_img);
subplot(1,2,2);
imshow(img_4);
So, this is the output:
Upvotes: 2
Views: 2340
Reputation: 35525
You dont understand what you are doing!
This is clearly homework, so I am not providing full code. You are doing image convolution, which you can do with way better code, (fft
, conv2
, imfilter
, etc) but I assume you are doing this for learning.
First, sit down with your lecture notes and learn what filtering is. When you apply a filter over a pixel (the first 2 loops are to choose each pixel in the image) what you want to do is multiply your chosen filter over that pixel, and all the surrounding pixels, and accumulate the result of that in the output image.
Once you are in a specific pixel (row
,col
), then you iterate around all the neighboring pixels, (offset by rowindex
,colindex
). For each of these, you want to multiply the current pixel to the filter on that specific location, i.e. 1 value, and add it to the result. Depends on what the filter is, it will do something different. In your case, a filter with all the same value will do an average. You do not need to use the mean2
function, the mathematics you are applying do the filtering.
In short, this line is wrong:
img_4(row,col) = mean2(double(working_img(row+rowindex,col+colindex)) * filter(1:5,1:5));
Both the use of mean2
and the index on the variable filter
.
Video explaining convolution: https://www.youtube.com/watch?v=C_zFhWdM4ic
Upvotes: 3