How to convert logical form to binary form image?
조회 수: 37 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
DGM
2023년 6월 24일
편집: DGM
2023년 7월 14일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Modify Image Colors에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!