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개)
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.
Eshtaiwy Abdeen
2021년 12월 7일
THANK VERY MUCH !!!!!!!!!!
Azzi Abdelmalek
2012년 12월 30일
편집: Azzi Abdelmalek
2012년 12월 30일
That's had nothing to do with your correlation. Try
a==b
댓글 수: 3
Jan
2012년 12월 30일
@Azzi: What do you expect as result?
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.
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)
카테고리
도움말 센터 및 File Exchange에서 Images에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!