imwrite, what it does with fractions and negative values
8 views (last 30 days)
Show older comments
James Latshaw
on 13 Jul 2021
Commented: James Latshaw
on 13 Jul 2021
Hello,
This is an odd question, but how does imwrite handle fractions or negative inputs?
I am interested in reproducing this effect.
For example If I randomly generate some data and save it as an image and then read the saved image, I will see that matlab (somehow) assigned a uint8 value for each pixel. What modiciations do I need to perform to 'a' in order to get it to be equal to 'b' ?
Thank you for taking time to read this.
-James
clc
a = rand(2,10)-.5;
imwrite(a,'cake.jpg');
b=imread('cake.jpg');
a
b
0 Comments
Accepted Answer
Image Analyst
on 13 Jul 2021
Edited: Image Analyst
on 13 Jul 2021
Try this illustrative demo:
a = rand(2,5) - 0.5 % Range is between -0.5 and +0.5
fileName = 'Delete_me.jpg';
imwrite(a, fileName);
b=imread(fileName)
delete(fileName); % Delete temporary file.
fileName = 'Delete_me.png';
imwrite(a, fileName);
b=imread(fileName)
delete(fileName); % Delete temporary file.
a2 = a*255
imwrite(a2, fileName);
% If image is floating point, it expects it to be in the range 0-1.
% Then it will scale 0-1 to 0-255 and clip negative to 0 and more than 1 to 255.
b=imread(fileName)
delete(fileName); % Delete temporary file.
a3 = uint8(a*255) % Make uint8. This may clip values <0 or >255 to 0 and 255.
imwrite(a3, fileName);
% If image is floating point, it expects it to be in the range 0-1.
% Then it will scale 0-1 to 0-255 and clip negative to 0 and more than 1 to 255.
b=imread(fileName) % Comes back unchanged as it should with uint8 values.
delete(fileName); % Delete temporary file.
fileName = 'Delete_me.mat';
save(fileName, 'a');
s = load(fileName);
b = s.a
delete(fileName); % Delete temporary file.
As you can see if it's a floating point number, imwrite will expect the number to be in the range 0-1 and it will multiply it by 255 and then cast to uint8. Numbers below 0 get clipped to 0, and numbers above 1 get clipped to 1 (so they'll be recalled as 255).
Moreover, run it a few times and you will probably see that the JPG recalled numbers are slightly different than the PNG numbers because of the lossy compression. So that's why you should use PNG, which is pretty much now a de facto standard format that most people use, and not JPG (which image analysts hate because it changes the values).
If you want to save the fractional part of the numbers then you should use MATLAB's proprietary format .MAT which will recall the floating point image with all its original, unchanged values.
I'm not sure exactly what you mean when you say "I am interested in reproducing this effect." What effect? What does "reproduce" mean to you.
5 Comments
Image Analyst
on 13 Jul 2021
Actually that doesn't agree with me. Note the last few words of that sentence "but I doubt it".
JPG convert the image to the spectral domain and then does a kind of low pass filter. Not exactly like a Fourier filter though because it does it block-by-block. That's what gives JPG images such lousy appearance with the notorious "JPG block artifacts" where you have huge rectangular blocks of uniform color. It's expecially bad in slowly varying areas, like the sky.
If your ground truth has positive and negative values, then you'd want to scale your images to 0-255 before saving to an image file. Use the same scaling factors for all images, or, to simulate possible changes in brightness (like you'd get if you did augmentation) you can scale each independently.
More Answers (2)
Mohammad Sami
on 13 Jul 2021
You can change to using random integer function instead of rand function.
Also change the image format to png instead of jpg.
a = randi([0,255],2,10,'uint8');
imwrite(a,'cake.png');
b=imread('cake.png');
isequal(a,b)
Walter Roberson
on 13 Jul 2021
%approximately
function imwrite(data, filename)
if isfloat(data)
data = uint8(data * 255);
end
and so on
end
This is a simplification, as the 255 depends upon the bit depth options.
Notice that the range of the floating point data is not tested first. imwrite() does not check to see whether the data happens to be in the range 0.0 to 255.0 and if so then simply uint8() it: imwrite() multiplies all floating point data by 255. If the floating point data happened to have values greater than 1.0 then the result of the multiplication will be greater than 255.0, and no matter what range that turns out to be, it is then processed by uint8() [unless 16 bit output was requested.]
uint8() has its usual properties:
- any negative value "underflows" to 0.
- any value greater than 255 "saturates" to 255.
- any non-integer value is rounded to the nearest integer before being changed to integer data type.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!