在MATLAB imagesc函数中显示网格线

问题描述 投票:3回答:4

我一直在尝试使用imagesc函数显示纯黑色网格线,这样每个像素周围都有一个黑色边界。我尝试了一些方法,但似乎无论如何,线条总是通过像素。作为一个例子,对于imagesc(randn(21,21)),我试图得到一个图,其中每个正方形(即像素)这里有一个黑色边框。

我在这里找到了一个解决方案:In matlab, how to draw a grid over an image,但我不确定如何使用imagesc,而不是a.jpg图像。

我也尝试使用hold on功能手动放置线条。但是每个解决方案,似乎网格线都穿过像素的中间。任何帮助,将不胜感激。谢谢。

image matlab matlab-figure
4个回答
5
投票

请尝试以下方法:

imagesc(randn(21,21))
hold on;
for i = 1:22
   plot([.5,21.5],[i-.5,i-.5],'k-');
   plot([i-.5,i-.5],[.5,21.5],'k-');
end

编辑:事物是像素的中心位于整数格点,所以要勾勒像素,你需要使用以.5结尾的坐标。


5
投票

pcolor正是这样做的:

pcolor(randn(15,21))
axis image %// equal scale on both axes
axis ij %// use if you want the origin at the top, like with imagesc


0
投票

如果您想突出显示对角线,请执行以下操作:

    mat=rand(10);
    figure, imagesc(mat)
    colormap gray

    hold on;
    n=size(mat,1);
    for i = 1:n
       plot([.5,n+.5],[i-.5,i-.5],'k-');
       plot([i-.5,i-.5],[.5,n+.5],'k-');
    end

    % Highlight diagonal values
    k=0.5;
    for i=1:numel(diag(mat))
    line([k, i+.5], [i+.5, i+.5], 'Color', 'r', 'LineWidth', 2);
    k=k+1;
    end

    k=0.5;
    for i=1:numel(diag(mat))
    line([k, i+.5], [k, k], 'Color', 'r', 'LineWidth', 2);
    k=k+1;
    end

    k=0.5;
    for i=1:numel(diag(mat))
    line([i+.5, i+.5], [k, i+.5], 'Color', 'r', 'LineWidth', 2);
    k=k+1;
    end

    k=0.5;
    for i=1:numel(diag(mat))
    line([k, k], [k, i+.5], 'Color', 'r', 'LineWidth', 2);
    k=k+1;
    end

0
投票

延伸到@ luis-mendo的答案

正如@Girardi所提到的,pcolor取代了矩阵内容。例如:

i = eye(5);
pcolor(i);
axis image %// equal scale on both axes
axis ij %// use if you want the origin at the top, like with imagesc

Note: Shows 4x4 instead of 5x5

请注意,它给出了4x4而不是5x5。解决方案:用零填充矩阵


i = eye(5);
pcolor([i, zeros(size(i,1), 1); zeros(1, size(i,2)+1)])
axis image %// equal scale on both axes
axis ij %// use if you want the origin at the top, like with imagesc
axis off

Correct

© www.soinside.com 2019 - 2024. All rights reserved.