【问题标题】:Matlab transparent overlay matrixMatlab透明叠加矩阵
【发布时间】:2014-11-02 13:48:41
【问题描述】:

我想加载一张图片,然后用图片的大小创建一个Matrix。 矩阵应该是透明的,只有一些备用值(点)。 然后我想在图中显示图像并将矩阵放在顶部。

到目前为止的代码:

world = imread('map1.jpg');           % import image of location
[x_world,y_world] = size(world);      % get the size of the image
A = zeros(x_world,y_world);           % create matrix with dimension of image

imshow(world);                        % display image
axis image;                           % label the axis

我的矩阵包含一些点:

A(200,300) = 1;
A(500,500) = 5;
A(580,200) = 3;

如果我现在像这样遍历矩阵中的每个值:

for i = 1:x_world
    for j = 1:y_world
        if(A(i,j) == 1)
            plot(i,j,'r.','MarkerSize',20);   % plot a single point
        elseif(A(i,j) == 2)
            plot(i,j,'y.','MarkerSize',20);   % plot a single point
        elseif(A(i,j) == 3)
            plot(i,j,'m.','MarkerSize',20);   % plot a single point
        elseif(A(i,j) == 4)
            plot(i,j,'g.','MarkerSize',20);   % plot a single point
        elseif(A(i,j) == 5)
            plot(i,j,'b.','MarkerSize',20);   % plot a single point
        elseif(A(i,j) == 6)
            plot(i,j,'w.','MarkerSize',20);   % plot a single point
        end
    end
end

真的很慢。

所以我想做的是创建一个透明的矩阵,然后设置一些点,这样我就可以在原始图像上打印矩阵。

这可能吗?我怎么做?还有其他更好的方法吗?

【问题讨论】:

标签: image matlab matrix transparent


【解决方案1】:

首先,您可以通过实际循环遍历 6 个组来避免遍历所有这些行和列。代码攻击应该给出相同的结果:

markers = {'r.', 'y.', 'm.', 'g.', 'b.', 'w.'}; % // define some markers
figure
hold on
for group = 1:6
    [i, j] = ind2sub(size(A), find(A==group)); % // find indices of current group
    plot(i, j, markers{group}, 'markersize', 20) % // plot them with their marker
end

如果速度是个问题,您也许可以看看gscatter() 和/或sparse()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多