1

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.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120

1 Answers1

3

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
drakon101
  • 524
  • 1
  • 8