![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307862/image.png)
image processing problem in Salt and Pepper noise
13 views (last 30 days)
Show older comments
i try to add Salt and Pepper noise after that try to use median filter to denoise it in this code but has a problem
code ;
I = imread('PicQ1.png');
figure ,imshow(I)
J = imnoise(I,'salt & pepper',0.05);
figure ,imshow(J)
M = medfilt2(J);
imshowpair(J,M,'montage')
--------------------------------------------
the proplem shown in pic attached ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307331/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307331/image.jpeg)
0 Comments
Answers (2)
Image Analyst
on 1 Jun 2020
A better method is to use a modified median filter where you only replace the noise pixels with the median, not ALL pixels. This will prevent blurring and shape changing.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307862/image.png)
0 Comments
Sugar Daddy
on 1 Jun 2020
Edited: Sugar Daddy
on 1 Jun 2020
You are feeding a coloured image which is three dimensional ( RGB) while medfilt2 needs 2D data.
For example
I = imread('cameraman.tif');%default image
figure ,imshow(I);
J = imnoise(I,'salt & pepper',0.05);
M = medfilt2(J);% Now this will work because I is a gray scaled image
imshowpair(J,M,'montage')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307427/image.png)
Either change your rgb image to grayscale or apply medfilt2 on all three colours sequentialy
see the answer of image analyst below about how to apply median filter on coloured image
2 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!