How to coded this filter in Matlab

clc
clear all
img=double(imread('cameraman.tif'));
imshow(uint8(img))
[m n]=size(img);
w = ones(3)
for i=0:m-3
for j=0:n-3
sum=0;
for k=1:3
for l=1:3
sum = sum + img(i+k,j+l)*w(k,l);
end
end
img1(i+1,j+1) = sum/9;
end
end
img2 = uint8(img1);
figure
imshow(img2)
%imwrite(img2,‘output.png','png');
I coded for 1/9*(ones(1) but i can't write for 1/16*[1,2,1;2,4,2;1,2,1] in Matlab. I don't know how to do this. I need same coded for 1/16*[1,2,1;2,4,2;1,2,1]. and if we take i and j = 0, how should the code be? Would you help me please?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 3 日

0 投票

Write the code like this
clc
clear all
img=double(imread('cameraman.tif'));
imshow(uint8(img))
[m n]=size(img);
w = 1/9*ones(3); % <---- ones(3) is divided by 9
for i=0:m-3
for j=0:n-3
sum=0;
for k=1:3
for l=1:3
sum = sum + img(i+k,j+l)*w(k,l);
end
end
img1(i+1,j+1) = sum; % <---- division by 9 is removed.
end
end
img2 = uint8(img1);
figure
imshow(img2)
Then you can simply replace 'w' with
w = 1/16*[1,2,1;2,4,2;1,2,1];
and it will display correct image.

7 件のコメント

Rooter Boy
Rooter Boy 2020 年 11 月 3 日
編集済み: Rooter Boy 2020 年 11 月 3 日
Sir, are you sure? Isn't it necessary to change the for loops?
I think your answer is full false.
Ameer Hamza
Ameer Hamza 2020 年 11 月 3 日
What is wrong in this? For w = ones(3), it is equivalent to your code.
Rooter Boy
Rooter Boy 2020 年 11 月 3 日
編集済み: Rooter Boy 2020 年 11 月 3 日
Sir, if we take i and j = 1, how should the code be? Would you help me please?
Ameer Hamza
Ameer Hamza 2020 年 11 月 3 日
What do you mean by i=1 and j=1. In your current code, i and j also take value of 1.
Rooter Boy
Rooter Boy 2020 年 11 月 3 日
We coded using i=0, j=0
for i=0:m-3
for j=0:n-3
if we take i and j = 1, how should the code be?
Ameer Hamza
Ameer Hamza 2020 年 11 月 3 日
You can just use
for i=1:m-3
for j=1:n-3
Rooter Boy
Rooter Boy 2020 年 11 月 19 日
Sir, could you help me when you are available?
https://www.mathworks.com/matlabcentral/answers/652808-nwc-and-lcm-code-block-in-matlab

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

質問済み:

2020 年 11 月 3 日

コメント済み:

2020 年 11 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by