2016-08-12 82 views
-2

我使用的是matlab函數。此功能正在使用相同圖像的碎片。但最後我不把這些作品結合起來。我只能顯示不同的數字。我如何重新加入此圖片? (我已經看到Mathematica使用'ImageAssemble'命令,也許matlab有這樣的功能。)如果沒有一個函數,我認爲這個peaces可以在subplot命令下顯示,但問題是我必須在函數中打開subplot,當我每次調用函數,不同的子圖打開。我只想打開一個副劇場。例如,我有像下面使用matlab中的子圖命令繪製,但功能不同

function[] =seperate(I,n,m) 
I1=I(1:m/2,1:n/2); 
I2=I(1:m/2,n/2+1:n); 
I3=I(m/2+1:m,1:n/2); 
I4=I(m/2+1:m,n/2+1:n); 
subplot(2,4,1) %for eight image 
imshow(I1); 
subplot(2,4,2) 
imshow(I2); 
subplot(2,4,3) 
imshow(I3); 
subplot(2,4,4) 
imshow(I4); 
end 

有一個實際的程序

img=imread('any_image.jpg'); 
gray=rgb2gray(img); 
[n,m] = size(gray); 
seperate(gray,n,m); 
img_2=imread('any_image_2'); 
gray_2=rgb2gray(img_2); 
[n1,m1]=size(gray_2) 
seperate(gray_2,n1,m1); 

正如你可以看到這個「獨立」功能4成分隔條件等於一塊塊的圖像的功能。當你在兩個不同的圖像中使用這個函數時,你有兩個不同的子圖。我想要一個副劇場。例如,應將第一個圖像拼圖放置在'子圖(2,4,1),子圖(2,4,2),子圖(2,4,3),子圖(2,4,4)'和第二圖像拼圖放置'子圖(2,4,5),子圖(2,4,6),子圖(2,4,7),子圖(2,4,8)'。我怎樣才能做到這一點? 我也可以重新加入這些和平,我可以創造一個新的形象作爲一個和平,包括前兩個和平的8個和平?謝謝你的幫助。

+0

只需將兩張圖像傳遞給''獨立'並添加更多'副'調用...... – excaza

回答

1

您的問題非常陳述,您的初始代碼中存在錯誤。然而,由於碼提供我跑了它,並設法理解這個問題:

要調用一個函數(其中有一些圖形輸出)的兩倍,但你想要的圖形在同一figure輸出(你用subplot這個詞的方式太多了,有時候不適合)。

,以確保一個函數總是在同一圖中繪製的方法是:

  • 附加一個標識到目標人物(我用的身影tag 屬性,但你可以使用其他屬性(Name) 只要您可以指定一個唯一標識符)。
  • 當函數必須執行圖形輸出時,它首先搜索目標圖形是否存在 。您可以使用功能findobj。如果yes它直接輸出到它,如果no它 創建一個新的。

下面是你的函數seperate.m的改寫:

function seperate(I,subset) 

    % default subset = 1 
    if nargin < 2 ; subset=1 ; end 

    % first find if the target figure already exist 
    hfig = findobj(0,'Type','figure','Tag','SeparatingFigure') ; 
    if isempty(hfig) 
     figure('Tag','SeparatingFigure') ; % create a new one 
    else 
     figure(hfig) ; % just make the existing figure active 
    end 

    % now split the image in 4 pieces 
    [m,n] = size(I); 
    Isplit{1} = I(1:m/2,1:n/2) ; 
    Isplit{2} = I(1:m/2,n/2+1:n); 
    Isplit{3} = I(m/2+1:m,1:n/2); 
    Isplit{4} = I(m/2+1:m,n/2+1:n); 

    % now display to the proper set of subplots 
    % "subpos" store the index of the 4 subplot to use for 1 subset 
    subpos = [1 2 5 6 ; ... %subset "1" will plot in subplot [1 2 5 6] 
       3 4 7 8] ; %subset "2" will plot in subplot [3 4 7 8] 
    for k=1:4 
     subplot(2,4,subpos(subset,k)) 
     imshow(Isplit{k}); 
    end 
end 

我冒昧地部分重構代碼,以便能夠使用循環(而不是幾乎重複的語句一長串) 。

當您使用功能,subset參數取值12,以指明次要情節的4個部分將被繪製。你可以用這種方式:

img1=imread('coins.png'); 
seperate(img1,1); 

img2=imread('peppers.png'); 
seperate(img2,2); 

separated1

而且任何新的圖像,你扔它會簡單地取代舊的(仍然在同一個圖):

% replace the first subset with another image 
img3=imread('football.jpg'); 
seperate(img3,1); 

enter image description here

+0

非常感謝。這正是我需要的。我們也可以在這個過程中結束這些獨立的和平嗎? – ahmd14

+0

好吧,你已經手動分割圖像,你可以重新連接在一起。像'Ifull = [Isplit {1},Isplit {2}; Isplit {3},Isplit {4}]' – Hoki