How to save a binary image
32 ビュー (過去 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.
While the image saved looks like.
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 件)
KALYAN ACHARJYA
2019 年 8 月 11 日
編集済み: KALYAN ACHARJYA
2019 年 8 月 11 日
imwrite(A,'file_name.tif');
0 件のコメント
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 で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!