How to save a binary image
33 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/233522/image.png)
While the image saved looks like.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/233523/image.png)
1 commentaire
DGM
le 21 Déc 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
% 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
... and yes, error will still exist, no matter what you set the 'quality' parameter to.
Réponses (2)
KALYAN ACHARJYA
le 11 Août 2019
Modifié(e) : KALYAN ACHARJYA
le 11 Août 2019
imwrite(A,'file_name.tif');
0 commentaires
Johannes Kalliauer
le 21 Déc 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)
0 commentaires
Voir également
Catégories
En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!