Azzera filtri
Azzera filtri

Why imnoise function gives pictures with larger size than original image?

2 visualizzazioni (ultimi 30 giorni)
I have a dataset and after addding noise to images I write them using imwrite function to store them in my data set, but after looking at the generated pictures I see that their size are 2 or 3 times larger than original image, I thought they should be nearly same size(for example my original image is 322KB but my same image + gaussian noise is 780KB ... Can somone explain why?
Here is my code:
%%Speckle
var_speckle=0.05;
%%Salt & Pepper
d=0.05;
%%Gaussian
m=0;
var_gauss=0.01;
%%%%%%%%%%%%%%%
x=0;
imagefiles = dir('*.png');
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
%%currentimage = rgb2gray(currentimage);
%%adjusted = imadjust(currentimage);
gaussian = imnoise(currentimage,'gaussian',m,var_gauss);
poisson = imnoise(currentimage,'poisson');
salt = imnoise(currentimage,'salt & pepper',d);
speckle = imnoise(currentimage,'speckle',var_speckle);
imwrite(gaussian,'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'gaussian.png','WriteMode','append')
imwrite(poisson,'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'poisson.png','WriteMode','append')
imwrite(salt,'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'salt.png','WriteMode','append')
imwrite(speckle,'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'speckle.png','WriteMode','append')
x=x+1;
end
  4 Commenti
Geoff Hayes
Geoff Hayes il 4 Ott 2021
@Seyed Mousavikia try comparing (usng the debugger) the currentImage and the gaussian image. Do both have the same dimension? Do both have the same data type? If so, then both images should be the same size when saved to file.
Seyed Mousavikia
Seyed Mousavikia il 4 Ott 2021
Ok I will try Geoff.. yes they have both same size and same data type

Accedi per commentare.

Risposte (3)

yanqi liu
yanqi liu il 9 Ott 2021
imwrite(mat2gray(gaussian),'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'gaussian.png','WriteMode','append')
imwrite(mat2gray(poisson),'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'poisson.png','WriteMode','append')
imwrite(mat2gray(salt),'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'salt.png','WriteMode','append')
imwrite(mat2gray(speckle),'C:\Users\dell\Desktop\Shabake_Asli\Dataset\ExtendedDataset\Mirror\SleepNoiseAddMirror\'+string(x)+'speckle.png','WriteMode','append')

Walter Roberson
Walter Roberson il 9 Ott 2021
Modificato: Walter Roberson il 11 Ott 2021
png file size depends on the choice of filter algorithms. imwrite does not offer any choice and is not necessarily offering the best filters.

Seyed Mousavikia
Seyed Mousavikia il 11 Ott 2021
Modificato: Seyed Mousavikia il 11 Ott 2021
So you mean if I change the format of my images (e.g jpeg format) the imwrite result will be same size as original images?
  1 Commento
Walter Roberson
Walter Roberson il 11 Ott 2021
No. .jpeg uses a different compression method that loses information. Comparing the file size it can create with the file size of PNG (which does not lose information) is not fair.
You might not be able to use imwrite() to get back the original file size.
I would suggest to you that immediately after you imread() the original file, that you imwrite() it out to a new name as a PNG file. And then later you would compare the size of the written image-with-noise PNG to the size of the PNG you saved. It might not be the same size as the original PNG file, but the two would have been saved with the same compression algorithm so it would be fair to compare them.

Accedi per commentare.

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by