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
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);
Best Answer