How to I convert a binary (logical) image back into double, uint8, etc, without changing what the image looks like in binary?
60 次查看(过去 30 天)
显示 更早的评论
I took a grayscale image (.tif file) and converted it into binary using imbinarize(). This gave me the desired visual, but now I want to save that visual and use it for processing. The issue is that my processing method does not take logical inputs and I can not change it. Is there a way to perserve the binary visual and convert it into a double or uint8? I could just screenshot it then convert it to .tif, but that would be cheaty. Thank you!
0 个评论
采纳的回答
DGM
2023-7-26
编辑:DGM
2023-7-26
If you want to change the numeric class of an image while retaining the image scale with respect to black/white, you normally use tools like im2uint8(), im2double(), etc. Those will also work on logical class images, where logical values become the class-dependent black/white values. For example:
A = logical([0 1 1 0]) % my logical image
B = im2uint8(A) % represented in uint8
C = im2double(A) % represented in double
Depending on the file format you're saving to, this conversion may be done automatically if you pass a logical array to imwrite(), though formats such as PNG will support logical arrays, so you may choose to use im2uint8() beforehand in such a case.
Without knowing what subsequent processing is going to happen, bear in mind that while the logical and uint8 representations both contain the same information and will be rendered identically, they may not be compatible with the same operations.
For instance, you couldn't use the uint8 image directly for logical indexing
outpict = inpict(A); % this is basic logical indexing using a mask
outpict = inpict(B); % but this will result in an error
Also, you can imagine other scenarios where they wouldn't be equivalent due to the change in scale.
outpict = inpict.*A; % this wouldn't be implausible, since A is unit-scale
outpict = inpict.*B; % but this would result either in an error or an improperly-scaled result
更多回答(1 个)
Walter Roberson
2023-7-26
img = imbinarize(imread('cameraman.tif'));
imshow(img); title('logical')
i8 = im2uint8(img);
imshow(i8); title('uint8');
id = im2double(img);
imshow(id); title('double');
另请参阅
类别
在 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!