How to save a binary image

조회 수: 32 (최근 30일)
Laura Valeria Pérez Herrera
Laura Valeria Pérez Herrera 2019년 8월 11일
댓글: DGM 2022년 12월 21일
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
DGM
DGM 2022년 12월 21일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 8월 11일
편집: KALYAN ACHARJYA 2019년 8월 11일
imwrite(A,'file_name.tif');

Johannes Kalliauer
Johannes Kalliauer 2022년 12월 21일
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)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by