How to find the local mean of an image?

23 ビュー (過去 30 日間)
SHILPI GUPTA
SHILPI GUPTA 2019 年 4 月 9 日
コメント済み: Rik 2022 年 6 月 10 日
img = imread('b.png');
img = rgb2gray(img);
[m,n] = size(img);
img1 = padarray(img,[1,1],'both');
m=m+2;
n=n+2;
img2 = zeros(m,n);
for i=2:m-1
for j=2:n-1
img2(i,j) = uint64(img1(i+1,j)+img1(i+1,j-1)+img1(i+1,j+1)+img1(i,j-1)+img1(i,j+1)+img1(i-1,j-1)+img1(i-1,j)+img1(i-1,j+1)+img1(i,j))/9;
end
end
disp(img2);
I'm using this code, still its not giving mw the right answer.
  4 件のコメント
shelan naveed
shelan naveed 2022 年 6 月 10 日
CAN YOU EXPLAIN IT WITH AN EXAMPLE PLEASE
Rik
Rik 2022 年 6 月 10 日
@shelan naveed what exactly do you want an explanation of?

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

採用された回答

Rik
Rik 2019 年 4 月 9 日
編集済み: Rik 2019 年 4 月 9 日
The fastest way to get a local average is to do a convolution with a flat structuring element:
%load example image
A=imread(['https://www.google.com/images/branding/googlelogo/',...
'1x/googlelogo_color_272x92dp.png']);
A=rgb2gray(A);
%define flat 3x3 structuring element
SE=ones(3,3);SE=SE/sum(SE(:));
B=conv2(A,SE,'same');
B=uint8(B);%cast back to uint8, this will round values
figure(1)
subplot(1,2,1)
imshow(A)
title('original')
subplot(1,2,2)
imshow(B)
title('local mean')
  1 件のコメント
SHILPI GUPTA
SHILPI GUPTA 2019 年 4 月 9 日
Thanks. It helped!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by