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

2 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2021-10-4
@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.

请先登录,再进行评论。

回答(3 个)

yanqi liu
yanqi liu 2021-10-9
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 2021-10-9
编辑:Walter Roberson 2021-10-11
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 2021-10-11
编辑:Seyed Mousavikia 2021-10-11
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 个评论
Walter Roberson
Walter Roberson 2021-10-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by