MATLAB: 我正在尝试创建一个GUI,在其中可以滚动查看一堆3D图像。

huangapple go评论59阅读模式
英文:

MATLAB: I am trying to create a GUI where I can scroll through a stack of 3-d images

问题

我正在进行一个项目,需要仔细查看堆叠的个别图像(CT扫描)。我不喜欢拼贴功能,因为堆叠包含了159张图像。我正在尝试创建一个GUI,允许我以自己的速度浏览堆叠中的个别图像。过去我已经做过这个,所以我知道这是可能的,但那是4年前。

目前,我已经创建了一个显示图像动画幻灯片的GUI,但我不能以自己的速度浏览它们。

以下是我目前的程序。

v = niftiread('插入图像名称');
[h, w, d] = size(v);
close all
figure
%montage(v)
for slice = 1:d
    imshow(v(:,:,slice),[])
end

提前谢谢。

英文:

I am working on a project in which I need to scrutinize individual images in a stack (CT scans). I do not like the montage function as the stack is 159 images thick. I am trying to create a gui that allows me to scroll through the individual images in the stack at my own pace. I have done it in the past, so I know it is possible, but that was 4 years ago.

Currently, I have created a gui that shows an animated slideshow of the images, but I cannot scroll through them at my own pace.

Here is my program so far.

v=niftiread('insert image name');
[h,w,d]=size(v);
close all
figure
%montage(v)
for slice = 1:d
    imshow(v(:,:,slice),[])
end

Thank you in advance.

答案1

得分: 3

这是我一直用来实现类似功能并对图形属性有一定控制的函数:

function genCustomSlider(plotterfcn, Data, varargin)

    fig = figure;
    set(fig, 'Toolbar', 'figure', 'NumberTitle', 'off')
    % 创建用于绘图的坐标轴
    ax = axes('Position', [0.1300 0.1500 0.7750 0.8150]);
    % 用于epsilon和lambda的滑块
    len = size(Data, ndims(Data));
    slider1_handle = uicontrol(fig, 'Style', 'slider', 'Max', len, 'Min', 1, ...
        'Value', 1, 'SliderStep', [1/(len-1) 10/(len-1)], ...
        'Units', 'normalized', 'Position', [.02 .02 .9 .05]);
    label = uicontrol(fig, 'Style', 'text', 'Units', 'normalized', 'Position', [.02 .07 .14 .04], ...
        'String', 'Choose frame');
    
    % 设置回调函数
    vars = struct('slider1_handle', slider1_handle, 'Data', Data, 'Axes', ax, 'Label', label, 'Figure', fig);
    if nargin > 2
        for i = 1:length(varargin)
            newField = "field" + num2str(i);
            vars.(newField) = varargin{i};
        end
    end

    set(slider1_handle, 'Callback', {@slider1_callback, vars, plotterfcn});
    plotterfcn(vars);
    % 主文件结束
end

% 用于支持用户界面操作的回调子函数
function slider1_callback(~, ~, vars, plotterfcn)
    % 运行slider1来控制epsilon的值
    plotterfcn(vars);
end

% plotterfcn在源文件中的示例
% function plotterfcn(vars)
%     % 绘制图像
%     val = round(get(vars.slider1_handle, 'Value'));
%     imagesc(vars.Axes, vars.Data(:,:,val));
%     colormap(vars.Axes, 'gray');
%     set(vars.Label, 'String', num2str(val));
% end

% 如何传递函数的示例:
% genCustomSlider(@plotterfcn2, frame)

基本上,你可以将这个文件保存在Matlab路径的某个位置。然后,在你的脚本中,创建一个被注释掉的函数的版本。然后,在你的脚本中调用genCustomSlider,并传递函数句柄。

如果你想添加其他作为genCustomSlider()输入并在子函数中可用的变量(例如坐标轴值),只需将它们作为参数添加到上面的函数中,它们将在vars结构体定义后的if语句中被合并。一个不包含这些内容的简化版本的代码如下:

function genSliderV2(Data)

fig = figure;
set(fig, 'Toolbar', 'figure', 'NumberTitle', 'off')
% 创建用于绘图的坐标轴
ax = axes('Position', [0.1300 0.1500 0.7750 0.8150]);
% 用于epsilon和lambda的滑块
len = size(Data, 3);
slider1_handle = uicontrol(fig, 'Style', 'slider', 'Max', len, 'Min', 1, ...
    'Value', 1, 'SliderStep', [1/(len-1) 10/(len-1)], ...
    'Units', 'normalized', 'Position', [.02 .02 .9 .05]);
label = uicontrol(fig, 'Style', 'text', 'Units', 'normalized', 'Position', [.02 .07 .14 .04], ...
    'String', 'Choose frame');
% 设置回调函数
vars = struct('slider1_handle', slider1_handle, 'Data', Data, 'Axes', ax, 'Label', label);
set(slider1_handle, 'Callback', {@slider1_callback, vars});
plotterfcn(vars)
% 主文件结束
end

% 用于支持用户界面操作的回调子函数
function slider1_callback(~, ~, vars)
    % 运行slider1来控制epsilon的值
    plotterfcn(vars)
end

function plotterfcn(vars)
    % 绘制图像
    val = round(get(vars.slider1_handle, 'Value'));
    imagesc(vars.Axes, vars.Data(:,:,val));
    colormap(vars.Axes, 'gray');
    set(vars.Label, 'String', num2str(val));
end
英文:

This is the function I've used to do something like this with some control over the figure properties for some time now:

function genCustomSlider(plotterfcn,Data,varargin)

    fig=figure;
    set(fig,'Toolbar','figure','NumberTitle','off')
    % Create an axes to plot in
    ax = axes('Position',[0.1300 0.1500 0.7750 0.8150]);
    % sliders for epsilon and lambda
    len = size(Data,ndims(Data));
    slider1_handle=uicontrol(fig,'Style','slider','Max',len,'Min',1,...
        'Value',1,'SliderStep',[1/(len-1) 10/(len-1)],...
        'Units','normalized','Position',[.02 .02 .9 .05]);
    label = uicontrol(fig,'Style','text','Units','normalized','Position',[.02 .07 .14 .04],...
        'String','Choose frame');
    
    
    % Set up callbacks
    % climVals = [mean(min(Data,[],[1,2])),mean(max(Data,[],[1,2]))];
    vars=struct('slider1_handle',slider1_handle,'Data',Data,'Axes',ax,'Label',label,'Figure',fig);%,'CLIMS',climVals);
    if nargin > 2
        for i = 1:length(varargin)
            newField = "field"+num2str(i);
            vars.(newField) = varargin{i};
        end
    end

    set(slider1_handle,'Callback',{@slider1_callback,vars,plotterfcn});
    plotterfcn(vars);
    % End of main file
end

% Callback subfunctions to support UI actions
function slider1_callback(~,~,vars,plotterfcn)
    % Run slider1 which controls value of epsilon
    plotterfcn(vars);
end


% Example of how plotterfcn may be written in source file:

% function plotterfcn(vars)
%     % Plots the image
%     val = round(get(vars.slider1_handle,'Value'));
%     imagesc(vars.Axes,vars.Data(:,:,val));
%     colormap(vars.Axes,'gray');
%     set(vars.Label,'String',num2str(val));
% end

% Example of how to pass the function:
% genCustomSlider(@plotterfcn2,frame)

Basically, you save this file somewhere on your matlab path. Then inside your script, you create a version of the commented out function. Then you call genCustomSlider in your script and pass it the function handle.

If you want to add other variables that are inputs to genCustomSlider() and are usable inside the subfunction (i.e. axes values) then just add them as parameters to the function above and they will be incorporated in the if statement after the vars structure is defined. A simpler version of the code without that is the following:

function genSliderV2(Data)

fig=figure;
set(fig,'Toolbar','figure','NumberTitle','off')
% Create an axes to plot in
ax = axes('Position',[0.1300 0.1500 0.7750 0.8150]);
% sliders for epsilon and lambda
len = size(Data,3);
slider1_handle=uicontrol(fig,'Style','slider','Max',len,'Min',1,...
    'Value',1,'SliderStep',[1/(len-1) 10/(len-1)],...
    'Units','normalized','Position',[.02 .02 .9 .05]);
label = uicontrol(fig,'Style','text','Units','normalized','Position',[.02 .07 .14 .04],...
    'String','Choose frame');
% Set up callbacks
% climVals = [mean(min(Data,[],[1,2])),mean(max(Data,[],[1,2]))];
vars=struct('slider1_handle',slider1_handle,'Data',Data,'Axes',ax,'Label',label);%,'CLIMS',climVals);
set(slider1_handle,'Callback',{@slider1_callback,vars});
plotterfcn(vars)
% End of main file
end

% Callback subfunctions to support UI actions
function slider1_callback(~,~,vars)
    % Run slider1 which controls value of epsilon
    plotterfcn(vars)
end

function plotterfcn(vars)
    % Plots the image
    val = round(get(vars.slider1_handle,'Value'));
    imagesc(vars.Axes,vars.Data(:,:,val));
    colormap(vars.Axes,'gray');
    set(vars.Label,'String',num2str(val));
end

huangapple
  • 本文由 发表于 2023年6月14日 23:42:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定