简体   繁体   English

为什么图像不能完全从逻辑转换回来

[英]Why can an image not be completely converted back from logical

For example consider the image below, it is a jpeg in unint8 format.例如考虑下面的图像,它是 unint8 格式的 jpeg。

在此处输入图片说明

In my code I require to process the image as unint8 , then convert it to logical and then use it again in unint8 format.在我的代码中,我需要将图像处理为unint8 ,然后将其转换为logical格式,然后以unint8格式再次使用它。 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:它产生以下图像:

在此处输入图片说明

Whichy shows that im2bw worked perfectly at 0.1 threshold. whichy 显示im2bw在 0.1 阈值下完美运行。 But if I convert it to logical and then back to unint8 and then perform the above command, the result is very different.但是如果我将其转换为logical ,然后再转换回unint8 ,然后执行上述命令,结果就大不相同了。

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:在这种情况下产生的结果是:

在此处输入图片说明

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?是否可以根据我期望它如何随着阈值变化而增长和缩小,以与im2bw一起使用的方式将逻辑图像转换回?

No, it not possible to convert back from logical to uint8 directly while maintaining the grey level values needed for im2bw .不,不可能在保持im2bw所需的灰度值的同时直接从逻辑转换回 uint8 。 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.例如,原始 uint8 图像的值可以是从 0 到 255 的任何整数。对im2bw的调用选择某个阈值并将低于该阈值的任何数字转换为 0,并将高于该阈值的任何数字转换为 1。因此,您的逻辑图像现在包含 0 或 1 的值。然后,当您应用im2uint8 ,0 & 1 被映射到 0 & 255,但由于逻辑图像中只有两个可能的值,因此您只能在 uint8 图像中获得两个可能的值:每个像素要么是 0 要么是 255。因此,无论阈值如何,第二个im2bw都会给你相同的结果,因为 0 和 255 之间的任何阈值只会将 0 和 255 映射回 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.根据您的代码,您可以做的只是将图像作为 uint8 保存在一个单独的变量中,然后再将其转换为逻辑。 That way, you will always have the original uint8 data available for later processing.这样,您将始终拥有可用于以后处理的原始 uint8 数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM