필터 지우기
필터 지우기

how do i automatically select the kernel size like 5*5, 7*7... instead of the 3*3 in the code below

조회 수: 9 (최근 30일)
clc;
clear all;
close all;
es=imread('cameraman.Tif');
es=imnoise(es,'salt & pepper',0.5);
b=es;
[r,c]=size(es);
for i=2:r-1
for j=2:c-1
mat=[es(i-1,j-1),es(i-1,j),es(i-1,j+1),es(i,j-1),es(i,j),es(i,j+1),es(i+1,j-1),es(i+1,j),es(i+1,j+1)];
mat=sort(mat);
b(i,j)=mat(5);
end
end
figure;
imshow(es);
title('Image corrupted with Salt & Pepper Noise');
figure;
imshow(b);
title('Image after filtering');
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 1월 11일
What should the criteria be for deciding whether a 5*5 or 7*7 is better or more justified than a 3*3 ? What should the code be looking for in order to decide which size to use ?

댓글을 달려면 로그인하십시오.

답변 (2개)

Rik
Rik 2022년 1월 6일
This is a terrible way to write a median filter. Since the use of imnoise indicates that you already have the Image Processing toolbox, why not use medfilt2?
  댓글 수: 2
Baththama Alhassan
Baththama Alhassan 2022년 1월 11일
what i am asking of how to choose different window size not the median filte, so that with the knowledge I can use it in another algorithm.
Rik
Rik 2022년 1월 11일
Your code is applying a median filter with a 3*3 kernel. That makes it equivalent to this:
es=imread('cameraman.tif');
es=imnoise(es,'salt & pepper',0.5);
es = padarray(es,[1 1],0,'both');%add padding since your code doesn't filter the edges
isequal( b , medfilt2(es,[3 3]) )
ans = logical
1
As you can see, this way it is trivial to change the kernel size to 5*5 or 7*7.
Why do you want to use your double loop?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2022년 1월 11일
window_size = randi([2 6]) * 2 - 1
window_size = 7
hws = (window_size - 1)/2;
es = imread('cameraman.tif');
assert(ndims(es) == 2, 'Grayscale images only!');
es = imnoise(es,'salt & pepper',0.5);
b = es;
[r,c]=size(es);
for i=1+hws:r-hws
for j=1+hws:c-hws
mat = es(i-hws:i+hws, j-hws:j+hws);
mat = sort(mat(:));
b(i,j)= mat((end-1)/2);
end
end
figure;
imshow(es);
title('Image corrupted with Salt & Pepper Noise');
figure;
imshow(b);
title( sprintf('Image after filtering window size %d', window_size) );

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by