How to save a binary image
43 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
% 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.
回答(2 个)
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!