how to remove noise from an image ?
11 ビュー (過去 30 日間)
古いコメントを表示
i know that's very basic but i don't know anything about matlab. i just need a code that can remove noise. i have a set of images downloaded from Google. i have seen some codes but the problem is , code consists of taking pepper.png and adding salt and pepper noise to the perfect image. later removing the noise using the filters. can i get the code that removes noise simply and displays the various channels and restored imgae. Thankyou.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 4, 1);
imshow(rgbImage);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the individual red, green, and blue color channels.
subplot(3, 4, 2);
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
imshow(blueChannel);
title('Blue Channel', 'FontSize', fontSize);
% Generate a noisy image. This has salt and pepper noise independently on
% each color channel so the noise may be colored.
noisyRGB = imnoise(rgbImage,'salt & pepper', 0.05);
subplot(3, 4, 5);
imshow(noisyRGB);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = noisyRGB(:, :, 1);
greenChannel = noisyRGB(:, :, 2);
blueChannel = noisyRGB(:, :, 3);
% Display the noisy channel images.
subplot(3, 4, 6);
imshow(redChannel);
title('Noisy Red Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
imshow(greenChannel);
title('Noisy Green Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
imshow(blueChannel);
title('Noisy Blue Channel', 'FontSize', fontSize);
% Median Filter the channels:
redMF = medfilt2(redChannel, [5 5]);
greenMF = medfilt2(greenChannel, [3 3]);
blueMF = medfilt2(blueChannel, [3 3]);
% Find the noise in the red.
noiseImage = (redChannel == 0 | redChannel == 255);
% Get rid of the noise in the red by replacing with median.
noiseFreeRed = redChannel;
noiseFreeRed(noiseImage) = redMF(noiseImage);
% Find the noise in the green.
noiseImage = (greenChannel == 0 | greenChannel == 255);
% Get rid of the noise in the green by replacing with median.
noiseFreeGreen = greenChannel;
noiseFreeGreen(noiseImage) = greenMF(noiseImage);
% Find the noise in the blue.
noiseImage = (blueChannel == 0 | blueChannel == 255);
% Get rid of the noise in the blue by replacing with median.
noiseFreeBlue = blueChannel;
noiseFreeBlue(noiseImage) = blueMF(noiseImage);
% Reconstruct the noise free RGB image
rgbFixed = cat(3, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
subplot(3, 4, 9);
imshow(rgbFixed);
title('Restored Image', 'FontSize', fontSize);
1 件のコメント
KALYAN ACHARJYA
2021 年 1 月 22 日
If the question is specific, it leads to better chance to get the answers.
回答 (1 件)
Sourabh
2025 年 2 月 17 日 10:08
Hey @Hemanth kumar
I understand that you want to remove noise from a set of real-world images downloaded from Google, not from an artificially noisy image.
Assuming you already have a noise image as input, noise removal can be done in three ways. The following methods use “filter2()” for averaging, “medfilt2()” for median filtering and “wiener2()” for adaptive filtering in MATLAB to remove noise.
1. Noise Removal using Averaging filter
denoisedImg = filter2(fspecial('average',3),noisyImg)/255; %uses a 3x3 box filter
2. Noise Removal using Median filter
denoisedImg = medfilt2(noisyImg);
3. Noise Removal using Adaptive filter
denoisedImg = wiener2(noisyImg,[5 5]); %uses a 5x5 neighborhood
Now, split the “denoisedImg” into 3 channels as follows:
redDenoised = denoisedImg(:, :, 1);
imshow(redDenoised);
greenDenoised = denoisedImg(:, :, 2);
imshow(greenDenoised);
blueDenoised= denoisedImg(:, :, 3);
imshow(blueDenoised);
Finally, display the denoised image using:
imshow(denoisedImg);
For more information on noise removal, kindly refer to the following MATLAB documentation:
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Adaptive Filters についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!