使用滑块在matlab中显示图像堆栈

时间:2015-01-31 20:19:49

标签: image matlab matrix slider stack

我有一个3维数据矩阵(例如,维度上的图像堆栈,时间。 我想要显示一个图像,并在下面有一个滑块来浏览图像。

我写了一段有用的代码,但是我认为它很笨重,有点难看...我想写一个干净的功能,所以我想知道是否有人知道更清洁,更好的方式这样做。

这是我的代码:

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)]);

1 个答案:

答案 0 :(得分:3)

这是一种使用侦听器对象来平滑可视化堆栈的方法。我使用相同图像的灰度变化(即仅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

希望有所帮助!