【问题标题】:Wrong dimensions when normalizing an image标准化图像时尺寸错误
【发布时间】:2018-10-16 22:27:04
【问题描述】:

我在这段代码中遇到了一些错误

 X=imread ('Lighthouse.jpg'); %reads picture as int8 matrix  
figure, imagesc(X), colormap gray, title('original picture'), % display picture  
filter=[-1 0 1; -2 0 2; -1 0 1]; % builds Sobel filter matrix  
filter=single(filter); %convert double to single 
x=single(X); % convert int8 to single  
x=x/max(max(x)); %normalisation to [0,1] 

我得到的错误:

Error using  / 
Inputs must be 2-D, or at least one input must be scalar.
To compute elementwise RDIVIDE, use RDIVIDE (./) instead.
Error in sobel (line 10)
x=x/max(max(x)); %normalisation to [0,1]

另外,当我按照建议使用./ 时,我收到新错误:

Array dimensions must match for binary array op.
Error in sobel (line 10)
x=x./max(max(x)); %normalisation to [0,1]

我在规范化步骤中做错了。

我该如何解决这个问题?

【问题讨论】:

  • 看你过去的问题:请不要在标题中包含标签,这就是标签系统的用途。请参阅this answer on meta.SE

标签: matlab image-processing matrix sobel


【解决方案1】:

你为什么要调用 max 两次。如果我用

运行代码
x=x/max(x(:))

我没有收到错误消息。这会在一维中运行矩阵。

【讨论】:

  • 调用 max 两次允许分别对每一层进行归一化。你的生产线做了别的事情
【解决方案2】:

当我运行您的代码时,错误消息显示“使用 RDIVIDE (./)”。 像这样实现它:

x=x./max(max(x)); 

这会将每个 RGB 层除以其最大值。您可能需要复制最大值(我猜这取决于 matlab 版本),请改用这一行

x=x./repmat(max(max(x)),size(X,1),size(X,2),1); 

【讨论】:

  • 引用 OP:另外,当我按照建议使用 ./ 时,我收到新错误:*Array dimensions must match for binary array op. sobel 中的错误(第 10 行)x=x./max(max(x)); % 归一化为 [0,1] 所以不,那是行不通的。请参阅我对为什么会这样的问题的评论。
【解决方案3】:

虽然Caduceus' answer 是正确的;它一次性对所有三种颜色进行标准化。对于您的情况,可能更好的是rgb2gray,以获得单个颜色通道,然后对其进行规范化(使用x/max(x(:)))。

X=imread ('lighthouse.png'); %reads picture as int8 matrix  
filter=[-1 0 1; -2 0 2; -1 0 1]; % builds Sobel filter matrix  
filter=single(filter); %convert double to single 
x = single(rgb2gray(X)); % rgb2gray gives a uint8, you want single
% x=x/max(x(:)); %normalisation to [0,1] , not needed here as x can directly be used
% for Sobel purposes as it's a grey scale image.

figure;
subplot(1,2,1)
imagesc(X)
colormap(gray)
title('original picture'), % display picture 
subplot(1,2,2)
imagesc(x)
colormap(gray)
title 'Grey scale'


第一个错误的原因是max 给出了一个列最大值,这是一个 3D 矩阵。 max(max()) 因此给出一维的,而不是所需的标量。

然后发生第二个错误,因为max(max()) 给出了一个数组,该数组的条目数量与完整矩阵的数量不同(显然)。

基本上如果size(x) = [row, column channels], size(max(x)) = [row channels]size(max(max(x)) = [row]。使用冒号运算符实际上使整个 3D 矩阵成为一个单列向量,max(x(:)) 因而给出了一个值,这是所有行、列和通道中的最大值。

【讨论】:

    猜你喜欢
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多