2015-01-31 69 views
0

我有一個3維矩陣的數據(一維的圖像堆棧,例如時間 我想顯示一個圖像,並有一個下面的滑塊瀏覽圖像使用滑塊在matlab中的圖像堆棧顯示

我寫了一段可以工作的代碼,但它很笨重,有點醜陋我認爲...我想寫一個乾淨的函數,所以我想知道是否有人知道更清潔,更好的方法來做。它

這裏是我的代碼:

interv = [min max]; % interval for image visualization 

imagesc(Temps_visu,X*100,squeeze(X,Y,MyMatrix(:,:,1)),interv); 

title('My Title'); 
xlabel('X (cm)'); 
ylabel('Y (cm)'); 

pos = get(gca,'position'); 
% slider position 
Newpos = [pos(1) pos(2)-0.1 pos(3) 0.05]; 

pp = 1; 
% callback slider 
S = ['pp=floor(get(gcbo,''value''));imagesc(Temps_visu,X*100,squeeze(X,Y,MyMatrix(:,:,1)),interv));' ... 
    'set_axes_elasto;title(''My Title'');disp(pp);']; 

Mz = size(MyMatrix,3); 

% Creating Uicontrol 
h = uicontrol('style','slider',... 
    'units','normalized',... 
    'position',Newpos,... 
    'callback',S,... 
    'min',1,'max',Mz,... 
    'value',pp,... 
    'sliderstep',[1/(Mz-1) 10/(Mz-1)]); 
+0

所以@Gordon Freeman做了我的回答對你有幫助嗎? – 2015-02-02 13:45:09

回答

2

她e是一種使用偵聽器對象來實現它的方法,可以平滑地顯示堆棧。我使用同一圖像的灰度變化(即只有4幀)組成了一個虛擬棧,但原理對於您的應用程序來說是相同的。請注意,我使用imshow來顯示圖像,但使用imagesc時不會引起任何問題。

該代碼被評論,所以希望這是足夠清晰的。如果不是,請不要猶豫,尋求幫助!

代碼:

function SliderDemo 
clc 
clear all 

NumFrames = 4; %// Check below for dummy 4D matrix/image sequence 
hFig = figure('Position',[100 100 500 500],'Units','normalized'); 

handles.axes1 = axes('Units','normalized','Position',[.2 .2 .6 .6]); 

%// Create slider and listener object for smooth visualization 
handles.SliderFrame = uicontrol('Style','slider','Position',[60 20 400 50],'Min',1,'Max',NumFrames,'Value',1,'SliderStep',[1/NumFrames 2/NumFrames],'Callback',@XSliderCallback); 
handles.SliderxListener = addlistener(handles.SliderFrame,'Value','PostSet',@(s,e) XListenerCallBack); 

handles.Text1 = uicontrol('Style','Text','Position',[180 420 60 30],'String','Current frame'); 
handles.Edit1 = uicontrol('Style','Edit','Position',[250 420 100 30],'String','1'); 

%// Create dummy image sequence, here 4D sequence of grayscale images. 
MyImage = imread('peppers.png'); 

MyMatrix = cat(4,rgb2gray(MyImage),MyImage(:,:,1),MyImage(:,:,2),MyImage(:,:,3)); 

%// Use setappdata to store the image stack and in callbacks, use getappdata to retrieve it and use it. Check the docs for the calling syntax. 

setappdata(hFig,'MyMatrix',MyMatrix); %// You could use %//setappdata(0,'MyMatrix',MyMatrix) to store in the base workspace. 

%// Display 1st frame 
imshow(MyMatrix(:,:,:,1)) 

%// IMPORTANT. Update handles structure. 
guidata(hFig,handles); 

%// Listener callback, executed when you drag the slider. 

    function XListenerCallBack 

     %// Retrieve handles structure. Used to let MATLAB recognize the 
     %// edit box, slider and all UI components. 
     handles = guidata(gcf); 

%// Here retrieve MyMatrix using getappdata. 
MyMatrix = getappdata(hFig,'MyMatrix'); 

     %// Get current frame 
     CurrentFrame = round((get(handles.SliderFrame,'Value'))); 
     set(handles.Edit1,'String',num2str(CurrentFrame)); 

     %// Display appropriate frame. 
     imshow(MyMatrix(:,:,:,CurrentFrame),'Parent',handles.axes1); 

     guidata(hFig,handles); 
    end 


%// Slider callback; executed when the slider is release or you press 
%// the arrows. 
    function XSliderCallback(~,~) 

     handles = guidata(gcf); 

%// Here retrieve MyMatrix using getappdata. 
    MyMatrix = getappdata(hFig,'MyMatrix'); 

     CurrentFrame = round((get(handles.SliderFrame,'Value'))); 
     set(handles.Edit1,'String',num2str(CurrentFrame)); 

     imshow(MyMatrix(:,:,:,CurrentFrame),'Parent',handles.axes1); 

     guidata(hFig,handles); 
    end 

end 

這個數字看起來是這樣的:

enter image description here

希望幫助!

+1

非常好的例子。雖然有2條評論。您可以在滑塊定義中添加''SliderStep'',[1/NumFrames 2/NumFrames]'(但這只是一個舒適的事情)。另一種說法是,通常建議只保留'handles'的'guidata'結構。更重的數據集(像所有的圖像幀等)更好地放置在它們自己的變量/結構中,使用'setappdata' /'getappdata'。這樣,每次你調用'guidata'來得到單個uicontrol的句柄時,matlab就不需要在子函數工作空間中傳遞你沉重的數據集。 – Hoki 2015-01-31 22:26:28

+0

感謝提示@Hoki我會記住這一點,並編輯答案。 – 2015-02-01 14:43:13