How to convert logical form to binary form image?
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dayangku Nur Faizah Pengiran Mohamad
le 24 Juin 2023
Modifié(e) : DGM
le 14 Juil 2023
My question, how to convert logical value to binary form? Here's my codes:-
%To convert the input image to pure black and white to highlight the abnormality region and its background
I2=imread('resizeLAP 1 OC -200001.jpg');
threshold=graythresh(I2);
bw=im2bw(I2,threshold);
%To display the pure black and white of the input image
imshow(bw);
imwrite(bw,'thresresizeLAP 1 OC -200001.jpg');
%To remove image noise which consists of extraneous pixels in the objects
%of interest (abnormality region) in order to obtain accurate calculation
bw=bwareaopen(bw,30);
se=strel('disk',2);
bw=imclose(bw,se);
imshow(bw);
imwrite(bw,'openresizeLAP 1 OC -200001.jpg');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1418403/image.png)
The bw is in logical value form 0 and 1 value when I open. But then, when I use this codes:-
J=im2uint8(bw);
imshow(J);
imwrite(J,'8openresizeLAP 1 OC -200001.jpg');
It shown not 0 and 1 value, but it shown the value between 0 to 255. Why is this possible occur? How can I make it to 0 and 1 value?
And the reason why I use uint8 code is for tally with my input data which in uint8 value.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1418408/image.png)
0 commentaires
Réponse acceptée
DGM
le 24 Juin 2023
Modifié(e) : DGM
le 14 Juil 2023
A binary image is an image with only two possible pixel values -- typically the values representing black and white in whatever numeric class the data is in.
A logical image has class 'logical'. A logical image is a binary image; a binary image is not necessarily a logical image.
JPG does not support logical data, only uint8 or uint16 data. So if you feed a logical image to imwrite(), it will be converted to full-scale uint8. This means that the data will no longer range from [0 1], but [0 255]. The values are not simply rescaled; the lossy compression used by JPG will also spread all the edges in the image. That means it will also no longer be a strictly binary image.
inpict = imread('cameraman.tif')>128; % a logical image
imwrite(inpict,'bad.jpg'); % write as JPG
bad = imread('bad.jpg'); % a uint8 image
% illustrate the entirely unnecessary errors caused by using JPG
D = imabsdiff(im2uint8(inpict),bad); % compare on similar scale
D = imadjust(D); % increase visibility against a white web page
imshow(D,'border','tight')
Do not use JPG for binary images. PNG is lossless, supports logical-class data, and will typically be more efficient than JPG for this type of image data. For the above example, a lossy JPG will be roughly 6x as large as a lossless PNG.
Generally, avoid JPG for any technical purposes unless you know and accept the amount of data you're throwing away. If you've been accustomed to using it in Photoshop or other applications, it's worth pointing out that the defaults there are usually 4:4:4 chroma subsampling (i.e. none), whereas in MATLAB, it's 4:2:0 and is not user-configurable. Anything with hard edges and steep color transitions will suffer far worse than you might expect.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!