2014-11-02 162 views
0

我想加載一個圖像,然後創建一個Matrix與圖像的大小。 矩陣應該是透明的,只有一些備用值(點)。 然後我想在圖中顯示圖像並將矩陣放在頂部。Matlab透明覆蓋矩陣

至今代碼:

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 

我的基質含有一些要點:

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 

它:

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

如果我現在通過這樣的矩陣中的每個值迭代會很慢。

所以我想要做的是創建一個透明矩陣,然後設置一些點,以便我可以在原始圖像上打印矩陣。

這可能嗎?我怎麼做?有沒有更好的方法來做到這一點?

+0

密切相關的問題:http://stackoverflow.com/questions/3842195/how-to-show-points-on-image-in-matlab – paisanco 2014-11-02 13:59:02

+0

看看''gscatter()'' – Nras 2014-11-02 14:45:17

+0

我覺得這個傢伙演示如何做到這一點:[圖片覆蓋使用透明度](http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/) – bdecaf 2014-11-02 16:29:40

回答

0

作爲一個開始,您可以通過實際循環遍歷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()