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

Risposte (3)

Jan
Jan il 30 Dic 2012
Modificato: Jan il 30 Dic 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 Commenti

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 !!!!!!!!!!

Accedi per commentare.

Azzi Abdelmalek
Azzi Abdelmalek il 30 Dic 2012
Modificato: Azzi Abdelmalek il 30 Dic 2012
That's had nothing to do with your correlation. Try
a==b

3 Commenti

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

Accedi per commentare.

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

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Richiesto:

il 30 Dic 2012

Risposto:

il 9 Dic 2021

Community Treasure Hunt

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

Start Hunting!

Translated by