【问题标题】:How to use a Matlab GUI slider to display image timeframes如何使用 Matlab GUI 滑块显示图像时间范围
【发布时间】:2016-07-15 22:55:37
【问题描述】:

首先,我需要能够使用“uigetfile”函数选择和加载多个图像文件。然后,我需要使用滑块将加载图像的连续时间帧显示到轴上。所以问题是,如何将多个图像文件加载到 GUI 中,以及如何使用滑块将多个图像一张一张地显示出来。我感觉需要使用函数'dir'来选择'uigetfile'中的多个图像,但我不确定如何实现它。我正在使用指南。

这是图片加载按钮代码。

function loadImagePushButton_Callback(hObject,~,handles)

[fileName,pathName] = uigetfile('*.*');
normalImage = imread(fullfile(pathName,fileName));
handles.normalimage = normalImage;
axes(handles.imageDisplay)
imshow(normalImage)
guidata(hObject,handles)

end

这是一张一张显示图片的滑块。

function imageFrameSlider_Callback(hObject,~,handles)

normalImage = handles.normalimage;
sliderValue = get(hObject,'Value');
nextImage = %?%?%?% Not sure what to code here
axes(handles.imageDisplay)
imshow(nextImage)

end

【问题讨论】:

  • 您可以通过添加选项'MultiSelect','on' 来选择带有uigetfile 的多个文件。 -> [fileName,pathName] = uigetfile('*.*','MultiSelect','on'); 其余的我不知道。
  • @ Brocodile - 它似乎仍然不起作用,即使使用该选项我也只能选择单个文件。
  • @Senyokbalgul 您如何尝试选择多个文件?你在按住 shift 键吗?
  • @ Brocodile & Suever - 没关系,我只是打错了,它有效。但是打开多个文件后出现错误。该行之后的代码似乎不知道如何处理多个文件而不是单个文件。
  • 因为fileName 现在是cell array 大小的[1x<NumberOfFiles>]。您需要相应地处理它。

标签: matlab user-interface matlab-figure matlab-guide


【解决方案1】:

最简单的做法是将图像存储在handles 变量中,然后您可以从滑块回调中访问它们。

function loadImagePushButton_Callback(hObject,~,handles)
    % Load your images
    [fileName, pathName] = uigetfile('*.*', 'MultiSelect', 'on');

    % Cancel if user hit cancel
    if isequal(fileName, 0); return; end

    % Make sure that fileName is a cell
    if ischar(fileName); fileName = {fileName}; end

    % Load all images into a cell array and store it in the handles structure
    filenames = fullfile(pathName, fileName);
    handles.imagedata = cellfun(@imread, filenames, 'uniformoutput', 0);

    % Display the first one and store the graphics handle to the imshow object
    handles.image = imshow(handles.imagedata{1}, 'Parent', handles.imageDisplay);

    % Update the slider to accomodate all of the images
    set(handles.hslider, 'Min', 1, 'Max', numel(filenames), ...
                         'SliderStep', [1 1]/(numel(filenames) - 1), 'Value', 1)

    % Now save guidata
    guidata(hObject, handles);
end

function imageFrameSlider_Callback(hObject,~,handles)
    % Figure out which image to show
    index = get(hObject, 'Value');

    % Update existing image object in the GUI using this image data
    set(handles.image, 'CData', handles.imagedata{index});  
end

【讨论】:

  • @Suever - 似乎images{1}handles.imageDisplayhandles.image = imshow(images{1},'Parent',handles.imageDisplay); 中未定义的变量。我该如何纠正这个问题?
  • @Senyokbalgul 我用images{1} 解决了这个问题,但根据您的代码,handles.imageDisplay 应该是您在 GUI 中定义的轴对象。
  • @Suever - 似乎当我加载图像时,滑块会自动设置在滑块的末尾。所以,我只能后退(向左拉),导致Subscript indices must either be real positive integers or logicals的错误。
  • @Senyokbalgul 您需要设置滑块的范围。我会更新。已更新。
  • @ Suever - 似乎StepSize 不是现有参数,而是SliderStepset(handles.hslider, 'Min', 1, 'Max', numel(fileName), 'SliderStep', [1 1], 'Value', 1)。它现在似乎可以正常工作,但由于某种原因我只能滑过两张图片。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
相关资源
最近更新 更多