Slider using GUI in matlab (Image processing)

6 views (last 30 days)
Hi, I´ve got this program in Matlab. But I need to put it on a GUI to add other filters and stuff like that. I can´t make it work. Please help.
function GaussianSlider()
clear
clc
close all
handles.Image = imread('peppers.png');
handles.fig = figure;
handles.axes1 = axes('Units','pixels','Position',[50 100 400 400]);
handles.slider = uicontrol('Style','slider','Position',[50 50 400 20],'Min',3,'Max',15,'Value',3);
handles.Listener = addlistener(handles.slider,'Value','PostSet',@(s,e) gaussian_blur(handles));
imshow(handles.Image,'Parent',handles.axes1);
guidata(handles.fig);
function gaussian_blur(handles)
slider_value = round(get(handles.slider,'Value'));
h = fspecial('gaussian',slider_value,slider_value);
handles.Image=imfilter(handles.Image,h,'conv');
axes(handles.axes1);
imshow(handles.Image)
end
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 12 Oct 2015
José - I think that the problem is that your slider callback is receiving an outdated handles structure each time that it is called. Note how you assign the callback
handles.Listener = addlistener(handles.slider,'Value','PostSet',@(s,e) gaussian_blur(handles));
The input to gaussian_blur will always be the copy of handles at the time that this callback is created (assigned to handles.Listener). This can easily be verified by looking/displaying the contents of the handles structure as soon as the callback is fired. You should notice that there is no Listener field in the structure. It will only have those fields that have been created prior to being passed to gaussian_blur.
I realize that you are trying to do something like how you would if using GUIDE, but that may not be necessary here. Since your gaussian_blur function is nested within the main GaussianSlider function, then it will have access to any local variables that have been declared within GuassianSlider. i.e. it will have access to handles and so you do not have to pass it as an input parameter into your function. Try the following instead
handles.Listener = addlistener(handles.slider,'Value','PostSet',@(s,e) gaussian_blur);
and
function gaussian_blur
slider_value = round(get(handles.slider,'Value'));
h = fspecial('gaussian',slider_value,slider_value);
handles.Image=imfilter(handles.Image,h,'conv');
axes(handles.axes1);
imshow(handles.Image)
end
Now, every time that gaussian_slider is called, it will access and update the local variable handles so that on subsequent calls to this callback, it will have access to the last updated handles.Image.
Try the above and see what happens!
Note that your line of code
guidata(handles.fig);
is not necessary and is incorrect. The above would just return a copy to the handles structure for the figure. If you wish to save the updated handles to the figure, you would need to do
guidata(handles.fig, handles);
  10 Comments
Image Analyst
Image Analyst on 13 Oct 2015
Like I said, I don't think you need a listener. Why do you think you do? Why isn't a regular callback okay?
José González
José González on 13 Oct 2015
Because I need realtime updating of values for the slider.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 12 Oct 2015
Get rid of the "clear" line of code.

Image Analyst
Image Analyst on 12 Oct 2015
Jose, you don't need to add a listener. Simply put a call to the blurring function in your callback for your slider. Here is a good example framework to get you started with GUIDE: http://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!