【问题标题】:image stack display in matlab using a slider使用滑块在matlab中显示图像堆栈
【发布时间】:2015-01-31 20:19:48
【问题描述】:

我有一个 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)]);

【问题讨论】:

  • 所以@Gordon Freeman 我的回答对你有帮助吗?

标签: image matlab matrix slider stack


【解决方案1】:

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

图是这样的:

希望有帮助!

【讨论】:

  • 非常好的例子。 2 评论虽然。您可以在滑块定义中添加'SliderStep',[1/NumFrames 2/NumFrames](但这只是一件令人欣慰的事情)。另一个要注意的是,一般建议只为handles 保留guidata 结构。较重的数据集(如您所有的图像帧等...)最好使用setappdata/getappdata 放置在它们自己的变量/结构中。这样matlab就不需要每次调用guidata时都在子函数工作区中传递你繁重的数据集来获取单个uicontrol的句柄。
  • 感谢@Hoki 的提示,我会记住这一点并编辑答案。
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 2013-05-31
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
相关资源
最近更新 更多