How can I do images averaging after applying different noises?

4 views (last 30 days)
I need to add different random noises to an original gray level image, each time, and these new images with noise I have to averaging them, on purpose to return to the original image without using filters. I did the noises and now I have 8 different images, what's next?
clc
clear all;
close all;
I = imread("C:\Users\HP\Downloads\xrayhip.jpg");
imshow(I);
J1 = imnoise(I,'gaussian');
imshow(J1);
J2 = imnoise(I,'gaussian',0.5);
imshow(J2);
J3= imnoise(I,'poisson');
imshow(J3);
J4= imnoise(I, 'gaussian', 0.011);
imshow(J4);
J5= imnoise(I, 'gaussian', 0.96);
imshow(J5);
J6= imnoise(I,'salt & pepper');
imshow(J6);
J7= imnoise(I,'salt & pepper',0.362);
imshow(J7);
J8= imnoise(I,'speckle');
imshow(J8);

Answers (1)

DGM
DGM on 24 Mar 2024
Edited: DGM on 24 Mar 2024
There are two problems. One is that you're creating a bunch of numbered variables, which is always a great way to complicate things. Since they're all just loose arrays, you can't process them using mean() or any convenient tools unless you concatenate them all first, in which case there wasn't any point in creating them as loose numbered variables to begin with. You're stuck either concatenating them or just adding them together and dividing by 8.
The second issue is that you're working in uint8. This means that you can't just add them together and divide by 8. An 8-bit integer cannot store the sum, so the result will be garbage (the entire image will be a gray field with a pixel value of round(255/8)=32). You also can't really work around this issue by dividing first, since that compounds the rounding error to a non-negligible degree. You either have to not work in uint8, or you'll have to cast and scale each one of the loose numbered variables before adding them.
I suggest either not working in uint8, or not working with a pile of numbered variables. At least one of the two.
Here are two examples:
% cast I and work in double instead of uint8
I = imread('cameraman.tif'); % uint8
I = im2double(I); % unit-scale double
J1 = imnoise(I,'gaussian');
J2 = imnoise(I,'gaussian',0.5);
J3 = imnoise(I,'poisson');
J4 = imnoise(I,'gaussian', 0.011);
J5 = imnoise(I,'gaussian', 0.96);
J6 = imnoise(I,'salt & pepper');
J7 = imnoise(I,'salt & pepper',0.362);
J8 = imnoise(I,'speckle');
% all the J variables inherit their class from I, so we can add them
meanpict = (J1 + J2 + J3 + J4 + J5 + J6 + J7 + J8)/8;
imshow(meanpict,'border','tight')
% just use one array
I = imread('cameraman.tif'); % uint8
J = zeros([size(I,1:3) 8],class(I)); % preallocate
J(:,:,:,1) = imnoise(I,'gaussian');
J(:,:,:,2) = imnoise(I,'gaussian',0.5);
J(:,:,:,3) = imnoise(I,'poisson');
J(:,:,:,4) = imnoise(I,'gaussian', 0.011);
J(:,:,:,5) = imnoise(I,'gaussian', 0.96);
J(:,:,:,6) = imnoise(I,'salt & pepper');
J(:,:,:,7) = imnoise(I,'salt & pepper',0.362);
J(:,:,:,8) = imnoise(I,'speckle');
% mean() will return a uint8-scale double array
% which needs to be cast to the appropriate class
meanpict = cast(mean(J,4),class(I));
imshow(meanpict,'border','tight')

Community Treasure Hunt

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

Start Hunting!