Question

For example consider the image below, it is a jpeg in unint8 format.

enter image description here

In my code I require to process the image as unint8, then convert it to logical and then use it again in unint8 format. But apparently there is no way to get it back to its "as was" state. For example consider the following code:

image = imread('W1\Writer1_03_02.jpg');

bw_normal = im2bw(image, 0.1);
imshow(bw_normal);

It produces the following image:

enter image description here

Whichy shows that im2bw worked perfectly at 0.1 threshold. But if I convert it to logical and then back to unint8 and then perform the above command, the result is very different.

image = imread('W1\Writer1_03_02.jpg');
image = im2bw(image,graythresh(image));
image = im2uint8(image);

bw_normal = im2bw(image, 0.1);
imshow(bw_normal);

The result produced in this case was:

enter image description here

basically it did not work as intended. That is at every threshold increment or decrement the word shown in my image should grow or shrink respectively. But it does not happen that way once it is converted to logical and then back.

Is it possible to convert a logical image back in a way that works with im2bw according to how I expect it to like grow and shrink with threshold change?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top