【问题标题】:How can I overcome the warning: “Integer operands are required for colon operator when used as index”?如何克服警告:“用作索引时冒号运算符需要整数操作数”?
【发布时间】:2017-03-23 21:40:25
【问题描述】:

我需要创建一个具有三个级别的图像,如以下问题中所述: How do i create a rectangular mask at known angles?

代码:

%# Create a logical image of a circle with image size specified as follows:
imageSizeX = 401;
imageSizeY = 301;

[columngrids, rowgrids] = meshgrid(1:imageSizeX, 1:imageSizeY);

%# Next create a logical mask for the circle with specified radius and center
centerY = (imageSizeY/2) + 0.5;
centerX = (imageSizeX/2) + 0.5;
radius  = 50;

Img   = double( (rowgrids - centerY).^2 + (columngrids - centerX).^2 <= radius.^2 );


%# change image labels to numeric
for ii = 1:numel(Img)

    if Img(ii) == 0
        Img(ii) = 2;  %change label from 0 to 2
    end

end

%# plot image
RI = imref2d(size(Img),[0 size(Img, 2)],[0 size(Img, 1)]);
figure, imshow(Img, RI, [], 'InitialMagnification','fit'); 

%# create the desired angle
phi = 45;       
width = 350;        % Desired width in pixels
height = 300;       % Desired height of bar in pixels

y = centerY - round(radius*cos(phi*pi/180));    % Find the nearest column
y0 = max(1, y-height); % Find where to start the bar 

Img(y0:y, 1:width)=3;

figure, imshow(Img, RI, [], 'InitialMagnification','fit'); 

我已经意识到,将(radius*cos(phi*pi/180)) 部分舍入以找到y 在大多数情况下可能会在所需角度产生错误。因此,如果我删除‘round function’,我会在图像中形成所需角度的确切点处得到实际的y value。尽管如此,我还是收到了上述警告。但是,如果我进一步应用该行:Img(y0:y, 1:width)=3;,代码仍然有效,但我注意到 Matlab 在创建向量 y0:y 时近似 y 值(我觉得这是我有一个问题)

那么我的问题是:有没有一种方法可以解决这个问题,这样我可以准确地创建我想要的角度,并且最终仍然可以得到从 y 到 y0 的条形图? matlab 在创建向量y0:y 时没有近似 y 值?

如果我转换为笛卡尔 xy 坐标,我可能有机会?任何想法如何进行这种转换?非常感谢您的帮助!

【问题讨论】:

  • Img 是一个数组。您不能使用非整数索引数组。如果我有A = [1 2 3 4] 并向你询问A 中的2.4th 值,你能把它给我吗?没有。
  • 谢谢@excaza。我理解你的意思,但我只是想知道(并希望)是否有办法解决这个问题。

标签: matlab image-processing image-masking


【解决方案1】:

像素是数字图像的原子元素。数字图像是离散函数。相机有离散像素,你的屏幕有离散像素...

虽然始终可以通过插值读取子像素值,但您不能以与输出离散相同的方式写入像素。

请接受只能使用整数像素坐标进行绘图,但请记住,这不会阻止您在幕后进行更精确的计算。

所以回答你的问题: 您通过使用整数索引克服了警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多