imwrite problem value changes
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(3 个)
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 个评论
Image Analyst
2012-12-30
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.
Azzi Abdelmalek
2012-12-30
编辑:Azzi Abdelmalek
2012-12-30
That's had nothing to do with your correlation. Try
a==b
3 个评论
Azzi Abdelmalek
2012-12-30
编辑:Azzi Abdelmalek
2012-12-30
The result should show that a and b are not equal
Jan
2012-12-30
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.
yanqi liu
2021-12-9
yes,sir,may be use some parameter in imwrite,such as
clc
clear all
close all
a=rgb2gray(imread('football.jpg')); size(a)
imwrite(a,'Note1.png','png'); b=imread('Note1.png'); size(b)
corr2(a,b)
imwrite(a,'Note2.tif','tif'); b=imread('Note2.tif'); size(b)
corr2(a,b)
imwrite(a,'Note3.jpg','jpg'); b=imread('Note3.jpg'); size(b)
corr2(a,b)
imwrite(a,'Note4.jpg','jpg','Quality',100); b=imread('Note4.jpg'); size(b)
corr2(a,b)
imwrite(a,'Note5.jpg','jpg','Mode','lossless'); b=imread('Note5.jpg'); size(b)
corr2(a,b)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!