No, it not possible to convert back from logical to uint8 directly while maintaining the grey level values needed for im2bw
. The information is lost during the initial transform to logical.
For example, the original uint8 image has values which can be any integer spanning from 0 to 255. The call to im2bw
picks some threshold and converts any number below that threshold to 0, and any number above it to 1. So, your logical image now only contains values of 0 or 1. Then, when you apply im2uint8
, 0 & 1 get mapped to 0 & 255, but since there were only two possible values in the logical image, you only get two possible values in the uint8 image: every pixel will either be 0 or 255. Hence, the second im2bw
will give you the same result regardless of threshold because any threshold between 0 & 255 would just map 0 & 255 back to 0 & 1.
What you could do instead, depending on your code, is just save the image as uint8 in a separate variable before you convert it to logical. That way, you will always have the original uint8 data available for later processing.