简体   繁体   English

Matlab读功能混乱

[英]Matlab imread function confusion

I'm implementing an ant colony algorithm in a grayscale image, and I'm confused in getting the right heuristic values. 我正在用灰度图像实现蚁群算法,但对于获得正确的启发式值感到困惑。 I always get 0 or 1. 我总是得到0或1。

Here's the part of the code: 这是代码的一部分:

x = imread('test.jpg');

y = max(x(:));

For example, I get 226 as the max intensity value. 例如,我得到226作为最大强度值。 I try x/y and I get a matrix with values of 1's and mostly 0's on single digit intensity values. 我尝试x / y,然后得到一个单位强度值为1且大多数为0的矩阵。 Is there a part here I'm missing? 我缺少这里的一部分吗?

I tried creating a zero 4x4 matrix with 255 in the diagonal. 我尝试创建对角线为255的零4x4矩阵。 Then I divide it with y. 然后我将其除以y。 I get some "same integer class error". 我收到一些“相同的整数类错误”。

This is because the image type of the image you are reading in with imread is one of an unsigned integer type, most likely uint8 and so when you are performing division, it is actually performing integer division and so the "decimal values" are truncated or removed. 这是因为你与阅读的图像的图像类型imread是无符号整数类型之一,最有可能uint8 ,所以当你执行除法,它实际上是执行整数除法,并因此“十进制值”将被截断或删除。 Here's a reproducible example. 这是一个可复制的示例。 Let's say I had the following 2 x 2 image in uint8 format: 假设我有uint8格式的以下2 x 2图像:

>> A = uint8([1 2; 3 4])

A =

    1    2
    3    4

Now if I tried to divide each intensity image by 4, I get: 现在,如果我尝试将每个强度图像除以4,我将得到:

>> B = A / 4

B =

    0    1
    1    1

This is because the image type is uint8 and so when division happens and the result is a floating-point number, the result is rounded , and this is most likely what is happening in your situation. 这是因为图像类型是uint8 ,所以当除法发生并且结果是浮点数时,结果将被舍入 ,这很可能是您所处的情况。


If you want to maintain floating point precision, you need to explicitly cast the image to double : 如果要保持浮点精度,则需要将图像显式转换为double

x = imread('test.jpg');
x = double(x); %// Change
y = max(x(:));
out = x / y;

BTW, since you want to normalize the image so that it falls in the range of [0,1] , I highly recommend you do not do this and use the im2double function instead. 顺便说一句,由于您想对图像进行归一化处理,使其落在[0,1]的范围内,因此我强烈建议您不要这样做,而应使用im2double函数。 It will perform this normalization for you as well as make other sanity checks: 它将为您执行此规范化并进行其他完整性检查:

x = imread('test.jpg');
out = im2double(x);

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

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