Slider using GUI in matlab (Image processing)

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

 采纳的回答

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 个评论

Thanks for your repply, the code that I put works. You can run it. What I need is to use GUIDE and I don´t know where to put the parts of the code. I am just gonna use one axis where is the image and a slider to regulate the filter.
See http://www.mathworks.com/matlabcentral/answers/153865-make-gui-slider-behave-like-ios-frame-scroller which tells you where to put the addListener code (in the OpeningFcn of your GUI).
Thanks for your help. I used this and works perfect.
function filterblurring_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% create the listener for the slider
handles.sliderListener = addlistener(handles.slider1,'ContinuousValueChange',@(hFigure,eventdata)slider1ContValCallback(hObject,eventdata));
%load image
handles.a=imread('peppers.png');
imshow(handles.a);
% Update handles structure
guidata(hObject, handles);
function slider1ContValCallback(hFigure,eventdata)
handles = guidata(hFigure);
fprintf('slider value: %f\n',get(handles.slider1,'Value'));
slider_value = round(get(handles.slider1,'Value'));
h = fspecial('gaussian',slider_value,slider_value);
handles.Image=imfilter(handles.a,h,'conv');
imshow(handles.Image,'Parent',handles.axes1);
But I don´t know why appears some errors in matlab despite it works perfect.
Error using feval Undefined function 'slider1_CreateFcn' for input arguments of type 'double'.
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in filterblurring (line 17) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)filterblurring('slider1_CreateFcn',hObject,eventdata,guidata(hObject))
Error using struct2handle Error while evaluating uicontrol CreateFcn
Error using feval Undefined function 'axes1_CreateFcn' for input arguments of type 'double'.
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in filterblurring (line 17) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)filterblurring('axes1_CreateFcn',hObject,eventdata,guidata(hObject))
Error using struct2handle Error while evaluating axes CreateFcn
Error using feval Undefined function 'filterblurring_OutputFcn' for input arguments of type 'struct'.
Error in gui_mainfcn (line 265) feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in filterblurring (line 17) gui_mainfcn(gui_State, varargin{:});
What are you doing when these errors are being generated? How are you running your GUI?
These errors are generated when I start the GUI, but then when move the slider it does the work and doesn´t show any other error.
Are you starting the GUI from the command line, the editor, or the GUIDE GUI builder? It could be that you have deleted some functions and will have to restore them.
Oh I understand now, what happened is that I create callbacks that later erased because didn´t use. I solved it making a new gui from zero. I really appreciate your help Geoff.
Like I said, I don't think you need a listener. Why do you think you do? Why isn't a regular callback okay?
Because I need realtime updating of values for the slider.

请先登录,再进行评论。

更多回答(2 个)

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

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by