What is the different if the value of the image is 0 and 1, and if the value of the image is 0 and 255?

3 views (last 30 days)
Anyone can explain me on details what is exactly the different if the value of the image is 0 and 1,
then another image is 0 and 255? Is the same idea? Black and white?

Accepted Answer

Cris LaPierre
Cris LaPierre on 16 Nov 2023
The range of values, and more particularly, what value corresponds to white, is determined by the data type of your image. What are the data types of your images? If 255 is white, then it is likely you are working with an 8-bit image. A scale of 0-1 tends to be the case when you have converted your image to single or double in MATLAB.
Here, both are binary images. 0 corresponds to black, and 1 or 255 correspond to white.
  6 Comments
Dayangku Nur Faizah Pengiran Mohamad
OK, thanks for your help Sir! I got it 0 and 255 value. But I got some problem on how to save the last image which is the value in 0 and 255. Where, I saved it I use the code:
imwrite(I2,'bin8.jpg');
But then, I transfer the bin8.jpg into my folder on pc. And when I read back the image using
imread
Its turned into like this Sir:
Is there any codes should I apply so that the value won't change besides the value 0 and 255?
DGM
DGM on 16 Nov 2023
Edited: DGM on 16 Nov 2023
Do not save binary images as JPG
The content will be damaged, the datatype will be altered, and the filesize will be larger than using a lossless format like PNG. There's no reason to use JPG for a binarized image.
JPG is not lossless, and this isn't Photoshop or GIMP, where you're saving 90% 4:4:4 JPGs. That's not an option. Unless you have carefully considered the damage caused by using a low-quality 4:2:0 downsampled JPG, and you don't intend to ever do any further technical processing of an image, just don't use JPG for anything. That includes when you save figures and plots.
Another example:
% a logical image
A = imread('1qqq.jpg')>128;
imshow(A)
% save as JPG, read back
fname1 = 'junk.jpg';
imwrite(A,fname1);
B1 = imread(fname1);
% the image class is now uint8
% and the values are spread away from black/white
imhist(B1)
ylim([0 500])
% so we'd have to re-binarize it _every single time_
B1 = B1 > 128;
% save as PNG, read back
fname2 = 'junk.png';
imwrite(A,fname2);
B2 = imread(fname2);
% the image class is unchanged
% the values are exactly the same
% we don't have to do anything.
isequal(A,B2)
ans = logical
1
% the damaged JPG is 9x as large as a perfect PNG
S1 = imfinfo(fname1);
S2 = imfinfo(fname2);
sizerat = S1.FileSize/S2.FileSize
sizerat = 9.0731

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!