将黑白图像转换为矩阵

Convert BW Image to Matrix

使用 im2bw(x) 将图像 x 转换为黑白图像后,imfilter 仅使用 return 的黑白图像。我怎样才能将这个黑白图像转换成一个正常的矩阵,使得 imfilter 可以 return 一个实数矩阵?演示:

>> k = [0.3 0.3; 0   0.3];
>> x = [0.9 0.3; 0.4 0.2];
>> y = im2bw(x)
y =

  1 0
  0 0
>> imfilter(y, k)
ans = 

  1  0
  0  0
>> imfilter([1 0; 0 0], k)
ans =

  0.30000  0.00000
  0.00000  0.00000
>> 

如您所见,imfilter 在应用于二值图像时对结果进行四舍五入。我想阻止这种情况。如何在保持其值的同时将二值图像转换为常规矩阵?

正在查看 imfilter

的文档

The result B has the same size and class as A.

由于 im2bw 的输出是逻辑数组,因此 imfilter 的输出也是。为了从 imfilter 获得浮点输出,您需要将阈值图像转换为浮点类型:

y = double(y);   % or, y = single(y);

结果:

>> k = [0.3 0.3; 0   0.3];
>> x = [0.9 0.3; 0.4 0.2];
>> y = im2bw(x)
y =

  1  0
  0  0

>> y = double(y)
y =

   1   0
   0   0

>> imfilter(y, k)
ans =

   0.30000   0.00000
   0.00000   0.00000