2012-08-26 48 views
1

我想結合matlab中的圖像數量。組合圖像的數量最多爲四個。我希望它們根據圖層的概念(如在Photoshop中)進行組合,而不是連接在一起。這意味着結果圖像的大小將與每個組合圖像的大小相同。有沒有一個matlab函數能夠正確執行這個任務?結合使用matlab的圖像數量

+0

圖層就是這樣,圖層。如果所有圖像的大小相同,則在組合圖像中看到的只是最上層(即最後一張圖像),以防沒有透明度。現在,我假設你想要通過乘,加,減或其他類似操作來合併它們?如果是,具體哪個操作? –

+0

其實我正在考慮這個想法來幫助我分割醫療組織圖像。 這個想法與flash(或photoshop)中圖層的概念類似。例如,您首先在單獨的圖層上添加背景圖像(例如海圖像),在此圖層上添加包含對象(例如船隻)的另一圖層,並在這兩層之上添加另一個包含星號的圖層作爲示例。最終結果(具有swf文件時)是包含所有對象的單個圖像,因爲它們位於不同位置(不應用透明度),所以這些對象都是可見的。 –

回答

0

試試這個:

img1 = imread... 
img2 = imread... 
img3 = imread... 
img4 = imread... 

combined_img(:,1) = img1; 
combined_img(:,2) = img2; 
combined_img(:,3) = img3; 
combined_img(:,4) = img4; 

現在你有,你可以通過combined_img的第三索引來訪問一個4分層圖像。 下面的命令將顯示第1圖像:

imshow(combined_img(:,1)); 
+0

非常感謝您的幫助。我會嘗試代碼。希望能得到我想要的結果。 –

+0

我假設圖像是灰度圖像。所以我的意思是他們只有兩個維度。也許你應該提供一些關於你想使用的圖像的信息。 –

+0

是的圖像是灰色的,我試過了代碼,但沒有奏效。 –

0

這確實像你描述:

function imgLayers 


    % initialize figure 
    figure(1), clf, hold on 

    % just some random image (included with Matlab) to server 
    % as the background 
    img{1,1} = imresize(imread('westconcordaerial.png'), 4);  
    img{1,2} = [0 0]; 

    % rotate the image and discolor it to create another image 
    img{2,1} = uint8(imresize(imrotate(img{1}, +12), 0.3)/2.5); 
    img{2,2} = [150 20]; 

    % and another image 
    img{3,1} = uint8(imresize(imrotate(img{1}, -15), 0.5)*2.5); 
    img{3,2} = [450 80]; 

    % show the stacked image 
    imshow(stack_image(img)); 

    %% create new image, based on several layers 

    function newImg = stack_image(imgs) 

     % every image contained at a cell index (ii) is placed 
     % on top of all the previous ones (0:ii-1) 

     rows = cellfun(@(x)size(x,1),imgs(:,1)); 
     cols = cellfun(@(x)size(x,2),imgs(:,1)); 

     % initialize new image   
     newImg = zeros(max(rows(:)), max(cols(:)), 3, 'uint8'); 

     % traverse the stack 
     for ii = 1:size(imgs,1) 

      layer = imgs{ii,1}; 
      offset = imgs{ii,2}; 

      newImg(offset(1)+(1:rows(ii)), offset(2)+(1:cols(ii)), :) = layer; 

     end 
    end 

end 

小心,雖然 - 沒有足夠的約束檢查等,所以你必須做有點發展自己。但它應該足以讓你開始:)

+0

感謝您理解我描述的想法。我將嘗試理解上面的代碼並使其適用於我的情況。 –