how is imwrite() in Matlab2020a saving RGB?
7 views (last 30 days)
Show older comments
I tried to add noise to one of the three color channels, say GREEN, save the new noisy image with imwrite(), open it again with imread(), and compare against the original. The image Ia has noise only on GREEn channel. Why is the noise in image Ib over all color channels after using imwrite() and reading the noisy image with imread()?
Here is the code:
=======
clear all;
close all;
clc;
I=imread('strawberries.jpg');
greenChannel=I(:,:,2);
%add noise to green channel
Ia=I;
Ia(:,:,2)=imnoise(greenChannel,'salt & pepper',0.01);
%save noisy image
imwrite(Ia,'./strawberries_noise1.jpg');
%read noisy image
Ib=imread('./strawberries_noise1.jpg');
%compare original and noisy image before imwrite()
I1a=double(I(:,:,1))-double(Ia(:,:,1));
I2a=double(I(:,:,2))-double(Ia(:,:,2));
I3a=double(I(:,:,3))-double(Ia(:,:,3));
%compare original and noisy image after imwrite()
I1b=double(I(:,:,1))-double(Ib(:,:,1));
I2b=double(I(:,:,2))-double(Ib(:,:,2));
I3b=double(I(:,:,3))-double(Ib(:,:,3));
figure;
subplot(2,3,1);imshow(mat2gray(I1a));title('no noise on RED')
subplot(2,3,2);imshow(mat2gray(I2a));title('noise on GREEN')
subplot(2,3,3);imshow(mat2gray(I3a));title('no noise on BLUE')
subplot(2,3,4);imshow(mat2gray(I1b));
subplot(2,3,5);imshow(mat2gray(I2b));
subplot(2,3,6);imshow(mat2gray(I3b));
0 Comments
Answers (1)
Cedric
on 24 Apr 2020
Edited: Cedric
on 24 Apr 2020
You are saving as a jpeg file, which is lossy by default.
If you save as .png for example (lossless), there will be no difference in the R and B components:
imwrite(Ia,'./strawberries_noise1.png');
Ib=imread('./strawberries_noise1.png');
You can also keep the jpeg format but set the mode to lossless:
imwrite(Ia,'./strawberries_noise1.jpg', 'Mode','lossless');
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!