imwrite problem value changes

9 views (last 30 days)
Saahil
Saahil on 30 Dec 2012
Answered: yanqi liu on 9 Dec 2021
Sir,
i am working on some project in which i need to use an image file as a matrix then do some processing on it again save it as an image file but if i again use 'imread', matrix value changes. Please provide solution to this problem?
For example:
a=rgb2gray(imread('Note.jpg')); size(a)
ans =
95 56
imwrite(a,'Note1.jpg','jpg'); b=imread('Note1.jpg'); size(b)
ans =
95 56
corr2(a,b)
ans =
0.9994
%correlation should be 'one' in this case
%both a and b are uint8 of size 95x56

Answers (3)

Jan
Jan on 30 Dec 2012
Edited: Jan on 30 Dec 2012
JPEG is a lossy compression format. Uncompressing a JPEG file and store it again as JPEG will not lead to an equal result. This holds also true, if you select the 100% quality for creating the JPEG, because the used transformations have round-off errors. imwrite allows to set the 'Mode' to 'lossless', but the resulting files is not compatible with some programs.
If you need exact copies of the picture, either let copyfile duplicate the original JPEG, or use a loss-less graphics file format as e.g. PNG, TIFF, BMP or GIF.
Try this:
a = rgb2gray(imread('Note.jpg'));
imwrite(a, 'Note1.jpg', 'Quality', 100);
imwrite(a, 'Note2.jpg', 'Mode', 'lossless');
imwrite(a, 'Note3.png');
b = imread('Note1.jpg');
c = imread('Note2.jpg');
d = imread('Note3.png');
max(abs(a(:) - b(:)))
max(abs(a(:) - c(:)))
max(abs(a(:) - d(:)))
  2 Comments
Image Analyst
Image Analyst on 30 Dec 2012
Jan's absolutely right. If you want a recommendation, I'd suggest PNG - it has lossless compression so the files are about a third the size of uncompressed TIFF, and BMP. GIF seems to be on the way out, as well as BMP and TIFF, while PNG is rapidly becoming more and more popular. If you are in the medical field, then DICOM seems to be very common.
Eshtaiwy Abdeen
Eshtaiwy Abdeen on 7 Dec 2021
THANK VERY MUCH !!!!!!!!!!

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 30 Dec 2012
Edited: Azzi Abdelmalek on 30 Dec 2012
That's had nothing to do with your correlation. Try
a==b
  3 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 30 Dec 2012
Edited: Azzi Abdelmalek on 30 Dec 2012
The result should show that a and b are not equal
Jan
Jan on 30 Dec 2012
Exactly, not equal but highly correlated. And this is exactly what the OP found out already using Matlab. The only problem is, that he expects the correlation to be 'one' and does not find the source of the difference.

Sign in to comment.


yanqi liu
yanqi liu on 9 Dec 2021
yes,sir,may be use some parameter in imwrite,such as
clc
clear all
close all
a=rgb2gray(imread('football.jpg')); size(a)
ans = 1×2
256 320
imwrite(a,'Note1.png','png'); b=imread('Note1.png'); size(b)
ans = 1×2
256 320
corr2(a,b)
ans = 1
imwrite(a,'Note2.tif','tif'); b=imread('Note2.tif'); size(b)
ans = 1×2
256 320
corr2(a,b)
ans = 1
imwrite(a,'Note3.jpg','jpg'); b=imread('Note3.jpg'); size(b)
ans = 1×2
256 320
corr2(a,b)
ans = 0.9894
imwrite(a,'Note4.jpg','jpg','Quality',100); b=imread('Note4.jpg'); size(b)
ans = 1×2
256 320
corr2(a,b)
ans = 1.0000
imwrite(a,'Note5.jpg','jpg','Mode','lossless'); b=imread('Note5.jpg'); size(b)
ans = 1×2
256 320
corr2(a,b)
ans = 1

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!