Clear Filters
Clear Filters

How to convert logical form to binary form image?

31 views (last 30 days)
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');
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.

Accepted Answer

DGM
DGM on 24 Jun 2023
Edited: DGM on 14 Jul 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.
  1 Comment
Dayangku Nur Faizah Pengiran Mohamad
Okay, noted Sir! I get what you meant now. I'm using GNU application to convert PNG format. Hopefully all is well. Thank you Sir for your explanation.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!