Clear Filters
Clear Filters

How to save a binary image

53 views (last 30 days)
Laura Valeria Pérez Herrera
Commented: DGM on 21 Dec 2022
I want to save an image with only black and white values, as my image is uint8 the image has only two values, 0 and 255. When I display the image on maltab it is what I wants but when I enter the folder where it is saved it isn't the same image.
A = imread('i000qa-fn.jpg');
for vv = 1: 640
for vr = 1:480
if (A(vv,vr) <= 200)
A(vv,vr) = 0;
else
A(vv,vr) = 255;
end
end
end
imwrite(uint8(A),'/Users/victorjavierper/Documents/MATLAB/IMAGENES CARA/LabeledF/i000qa-fn.jpg');
figure
imshow(A)
The desire image looks like this, shown with imshow.
While the image saved looks like.
  1 Comment
DGM
DGM on 21 Dec 2022
Don't use JPG for anything other than visualization -- especially not for binarized images that need to be processed later. If you have images dominated by flat areas of solid color and hard edges, JPG will do nothing but ruin them, all while providing a file size that's larger than better options.
A = imread('cameraman.tif')>130;
imshow(A)
% you could use PNG
imwrite(A,'binary.png')
Bpng = imread('binary.png');
imshow(imfuse(A,Bpng,'diff')) % ZERO ERROR
S = imfinfo('binary.png');
S.FileSize % 3kB for a lossless file
ans = 3070
% or you could use JPG
imwrite(A,'binary.jpg')
Bjpg = imread('binary.jpg');
imshow(imfuse(A,Bjpg,'diff')) % fidelity is garbage
S = imfinfo('binary.jpg');
S.FileSize % you're paying 5.7x as much to ruin your data
ans = 17635
... and yes, error will still exist, no matter what you set the 'quality' parameter to.

Sign in to comment.

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 11 Aug 2019
Edited: KALYAN ACHARJYA on 11 Aug 2019
imwrite(A,'file_name.tif');

Johannes Kalliauer
Johannes Kalliauer on 21 Dec 2022
imwrite(logical(B),'output_binary.png');
% getting example file from internet
rgb = webread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/233522/image.png');
imwrite(rgb,'input.png')
% inizalizing variables
A = imread('input.png');
Dim = size(A);
B = false(Dim(1),Dim(2));
%everything bright should be with and everything else black
for vv = 1: 422
for vr = 1:311
if (median([A(vv,vr,1),A(vv,vr,2),A(vv,vr,3)]) <= 200)
A(vv,vr,1) = 0;
A(vv,vr,2) = 0;
A(vv,vr,3) = 0;
else
A(vv,vr,1) = 255;
A(vv,vr,2) = 255;
A(vv,vr,3) = 255;
B(vv,vr)=true;
end
end
end
% 8-bit-figure
imwrite(uint8(A),'output_8bit.png');
figure(5)
imshow(A)
% 1-bit-figure
imwrite(logical(B),'output_binary.png');
figure(6)
imshow(B)
A working example would be:
% getting example file from internet
rgb = webread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/233522/image.png');
imwrite(rgb,'input.png')
% inizalizing variables
A = imread('input.png');
Dim = size(A);
B = false(Dim(1),Dim(2));
%everything bright should be with and everything else black
for vv = 1: 422
for vr = 1:311
if (median([A(vv,vr,1),A(vv,vr,2),A(vv,vr,3)]) <= 200)
A(vv,vr,1) = 0;
A(vv,vr,2) = 0;
A(vv,vr,3) = 0;
else
A(vv,vr,1) = 255;
A(vv,vr,2) = 255;
A(vv,vr,3) = 255;
B(vv,vr)=true;
end
end
end
% 8-bit-figure
imwrite(uint8(A),'output_8bit.png');
figure(5)
imshow(A)
% 1-bit-figure
imwrite(logical(B),'output_binary.png');
figure(6)
imshow(B)

Categories

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

Community Treasure Hunt

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

Start Hunting!