update image in GUI without GUIDE

I am new to GUI without GUIDE, and I would appreciate any help in completing this function. I need it to get the value from the slider and show me the updated image.
function making_SliderForIm(ImCh1,BWCh1)
global I
global Imask
Imask=BWCh1;
I=ImCh1;
imshow(I,[]);
%create a slider
sld=uicontrol('Style', 'slider',...
'Min',1,'Max',50,'Value',41,...
'Position', [400 20 120 20],...
'Callback', @SliderVal);
function SliderVal(source,event)
val=ceil(source.Value);
% Display segmented image
BWsIbf = activecontour(I,Imask, 1+val, 'edge');
[B,L] = bwboundaries(BWsIbf,'noholes');
imshow(I,[]);%label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2);
end
end
end

Answers (1)

Simply get the value:
currentSliderValue = sld.Value;
whenever you need to retrieve the current value of the slider.
It should be as simple as that.

11 Comments

Thank you for this, this addressing the first part of the problem, the next part is to get the updated image :)
OK, good luck with that. Let us know if you need help with that. If you do, post your image(s) and updated code.
This is working now, until the end. I get an error when I press the push bottom to get the final mask.
Attched is the sample image in m.file. I appreciate your help.
Best regards Peyman
function making_SliderForIm(ImCh1,BWCh1)
DemoFig = figure();
TextBox = uicontrol('Style', 'text', 'Units', 'normalized', ...
'Position', [.4 .2 .2 .05], 'String', 'Slider Value', ...
'FontSize', 12);
% For setting up a slider, 'Value' must be between 'Min' and 'Max'.
% 'Min' and 'Max' can be any numeric value (defaults 0 and 1).
% 'SliderStep' is a 2-value vector [LittleStep BigStep], both between 0 and
% 1, where LittleStep is the fraction the slider bar will move if you click
% the arrow at either end, and BigStep the fraction the slider bar will
% move when you click between bar and arrow, as well as the fraction of the
% slider the bar will take up.
% a slider
Slider = uicontrol('Style', 'slider', 'Units', 'normalized', ...
'Position', [.1 .1 .8 .05], 'Min', 0, 'Max', 800, 'Value', 50,...
'SliderStep', [0.01 0.1], 'Callback', @SliderUpdate);
% a button for proceding
Button = uicontrol('Style', 'pushbutton', 'Units', 'normalized', ...
'Position', [.85 .03 .1 .06], 'String', 'Proceed', 'Callback', @buttonPush);
% will add axes for a figure on top of the slider
Axes = axes('parent', DemoFig, 'Position', [.1 .3 .8 .6]);
% the image that you have in your m code will seat here
global I
global Imask
I=ImCh1;
%Imask=BWCh1;
Imask = zeros(size(I));
Imask(25:end-25,25:end-25) = 1;
% figure, imshow(mask);
% title('Initial Contour Location');
Image = BWCh1;
imshow(Image, 'parent', Axes);
hold on ; imshow(Imask);
axis image;
SliderListener = addlistener(Slider, 'ContinuousValueChange', @ListenerFcn);
% Callback functions
function SliderUpdate(varargin)
% Function is excecuted every time slider is RELEASED, but not while the
% slider is being moved
set(TextBox, 'String', num2str(get(Slider, 'Value')));
% Set color back to default after every time slider callback is
% executed
set(TextBox, 'BackgroundColor', get(Slider, 'BackgroundColor'));
end
function ListenerFcn(varargin)
% Function is excecuted while slider is being moved.
set(TextBox, 'String', num2str(get(Slider, 'Value')));
% To show what action is the slider callback and what is the
% listener, we'll change the color of the text box while the
% listener is active
SliderVal(Slider);
set(TextBox, 'BackgroundColor', [0.5 0.5 0.9]);
end
function SliderVal(source)
%delete(findobj('parent', Axes, 'type', 'line', 'marker', 'o'));
val=ceil(source.Value);
% Display segmented image
BWsIbf = activecontour(I,Imask, 1+val, 'edge');
[B,L] = bwboundaries(BWsIbf,'noholes');
% add here
if ~isempty(B);
imshow(I,[]);%label2rgb(L, @jet, [.5 .5 .5]))
hold on
set(Axes, 'NextPlot', 'add');
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2);
end
set(Axes, 'NextPlot', 'replace');
end
end
function buttonPush(Button)
currentSliderValue = sld.Value;
BWsIbf= activecontour(I,Imask, 1+currentSliderValue, 'edge');
uiresume(BWsIbf);
close(DemoFig);
end
uiwait;
end
I don't know why you don't just use GUIDE. Why make it hard on yourself by trying to handle all of the tedious details yourself? If you want to do a bunch of extra work because you like pain, then you'll have to figure out how to pass the variables between your various functions. For that, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
I thought this will give me more control over the script and easier to learn.
Good to know, however, this is almost done and if I can get help to complete this then I will start to learn the GUI with GUIDE when I have more time. Thank you for your time.
Attach all needed m-files with the paper clip icon.
It is working nicely but slow :)
function BW2Ch1=making_SliderForIm(ImCh1,BWCh1);
% This slider UI use your image and an estimated mask as input to generate a more accurate mask. It uses the "activecontour" function in matlab as
% the main core of the code
%last edited on 04-April-2017
DemoFig = figure();
TextBox = uicontrol('Style', 'text', 'Units', 'normalized', ...
'Position', [.4 .2 .2 .05], 'String', 'Slider Value', ...
'FontSize', 12);
% a slider
Slider = uicontrol('Style', 'slider', 'Units', 'normalized', ...
'Position', [.1 .1 .8 .05], 'Min', 0, 'Max', 800, 'Value', 300,...
'SliderStep', [0.01 0.1], 'Callback', @SliderUpdate);
% a button for proceding
Button = uicontrol('Style', 'pushbutton', 'Units', 'normalized', ...
'Position', [.85 .03 .1 .06], 'String', 'Proceed', 'Callback', @buttonPush);
% will add axes for a figure on top of the slider
Axes = axes('parent', DemoFig, 'Position', [.1 .3 .8 .6]);
% the image that you have in your m code will seat here
global I
global Imask
I=ImCh1;
Imask=BWCh1;
% Imask = zeros(size(I));
% Imask(25:end-25,25:end-25) = 1;
% figure, imshow(mask);
% title('Initial Contour Location');
Image = BWCh1;
imshow(Image, 'parent', Axes);
hold on ; imshow(Imask);
axis image;
SliderListener = addlistener(Slider, 'ContinuousValueChange', @ListenerFcn);
% Callback functions
function SliderUpdate(varargin)
% Function is excecuted every time slider is RELEASED, but not while the
% slider is being moved
set(TextBox, 'String', num2str(get(Slider, 'Value')));
% Set color back to default after every time slider callback is
% executed
set(TextBox, 'BackgroundColor', get(Slider, 'BackgroundColor'));
end
function ListenerFcn(varargin)
% Function is excecuted while slider is being moved.
set(TextBox, 'String', num2str(get(Slider, 'Value')));
% To show what action is the slider callback and what is the
% listener, we'll change the color of the text box while the
% listener is active
SliderVal(Slider);
set(TextBox, 'BackgroundColor', [0.5 0.5 0.9]);
end
function SliderVal(source)
%delete(findobj('parent', Axes, 'type', 'line', 'marker', 'o'));
val=ceil(source.Value);
% Display segmented image
BW2Ch1 = activecontour(I,Imask, 1+val, 'edge');
[B,L] = bwboundaries(BW2Ch1,'noholes');
% add here
if ~isempty(B);
imshow(I,[]);%label2rgb(L, @jet, [.5 .5 .5]))
hold on
set(Axes, 'NextPlot', 'add');
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2);
end
set(Axes, 'NextPlot', 'replace');
end
end
currentSliderValue = ceil(get(Slider, 'Value'));
function buttonPush(varargin)
BW2Ch1inal= activecontour(I,Imask, 1+currentSliderValue, 'edge');
close(DemoFig);
end
uiwait;
end

Sign in to comment.

Asked:

on 31 Mar 2017

Commented:

on 3 Apr 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!