mean filter special matrix apply to image. Salt and pepper noise

7 views (last 30 days)
How can I apply mean filter like 3x3 square neigboorhod following matrix?
mean4=[0,1,0;1,1,1;0,1,0];
I would like to apply with noise image this method. Here is my code:
orgim=imread('coins.png');
figure
imshow(orgim)
saltpeppern = imnoise(orgim,'salt & pepper');
figure
imshow(saltpeppern)

Answers (3)

DGM
DGM on 20 Dec 2021
Normalize the filter sum and apply it with imfilter().
originalimg = imread('coins.png');
noisyimg = imnoise(originalimg,'salt & pepper');
fk = [0,1,0;1,1,1;0,1,0]/5; % unit sum
avgfiltimg = imfilter(noisyimg,fk);
imshow(originalimg)
figure; imshow(noisyimg)
figure; imshow(avgfiltimg)

yanqi liu
yanqi liu on 20 Dec 2021
clc; clear all; close all;
mean4=[0,1,0;1,1,1;0,1,0];
% i would like to apply with noise image this method.here is my code;
orgim=imread('coins.png');
figure
imshow(orgim)
saltpeppern = imnoise(orgim,'salt & pepper');
figure
imshow(saltpeppern)
% use imfilter
im1 = imfilter(double(saltpeppern), mean4, 'replicate');
% use conv2
im2 = conv2(double(saltpeppern), mean4, 'same');
% use fft
im3 = real(ifft2(fft2(double(saltpeppern)).*fft2(mean4, size(saltpeppern,1), size(saltpeppern,2))));
% display
figure; imshow(im1, []); title('use imfilter');
figure; imshow(im2, []); title('use conv2');
figure; imshow(im3, []); title('use fft');

Image Analyst
Image Analyst on 20 Dec 2021
See my attached salt and pepper demos. It adds noise to the original image, then takes the median filter of the image. Then it identified the location of the noise and replaces only those noise pixels with the median filtered value.

Community Treasure Hunt

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

Start Hunting!