imwrite problem value changes

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

回答(3 个)

Jan
Jan 2012-12-30
编辑:Jan 2012-12-30
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 个评论

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.
THANK VERY MUCH !!!!!!!!!!

请先登录,再进行评论。

That's had nothing to do with your correlation. Try
a==b

3 个评论

@Azzi: What do you expect as result?
The result should show that a and b are not equal
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.

请先登录,再进行评论。

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

类别

帮助中心File Exchange 中查找有关 Images 的更多信息

提问:

2012-12-30

回答:

2021-12-9

Community Treasure Hunt

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

Start Hunting!

Translated by