Image not updating after processing parameters are changed.

My code looks like this...
% --- Executes on selection change in popupmenu_contrast.
function popupmenu_contrast_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_contrast (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_contrast contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_contrast
global slider1value; global slider2value; global slider3value; global slider4value; global slider5value
%
str = get(hObject, 'String');
val = get(hObject, 'Value');
% set current contrast operation to the user-selected
switch str{val};
case 'imadjust';
slider1value = 0
slider2value = 1
slider3value = 0
slider4value = 1
slider5value = 1
handles.current_contrast = @imadjust;
set(handles.slider1, ...
'visible', 'on',...
'min', 0, 'max', 1, 'value', slider1value,...
'sliderstep', [0.05, 0.1]);
set(handles.slider2, ...
'visible', 'on',...
'min', 0, 'max', 1, 'value', slider2value,...
'sliderstep', [0.05, 0.1]);
set(handles.slider3, ...
'visible', 'on',...
'min', 0, 'max', 1, 'value', slider3value,...
'sliderstep', [0.05, 0.1]);
set(handles.slider4, ...
'visible', 'on',...
'min', 0, 'max', 1, 'value', slider4value,...
'sliderstep', [0.05, 0.1]);
set(handles.slider5, ...
'visible', 'on',...
'min', 0, 'max', 10, 'value', slider5value,...
'sliderstep', [0.05, 0.1]);
case 'histeq';
handles.current_contrast = @histeq;
set(handles.slider1, ...
'visible', 'on',...
'min', 1, 'max', 255, 'value', 64,...
'sliderstep', [0.000393701, 0.001574803]);
set(handles.text1, 'visible', 'on', 'string', '# Bins');
set(handles.slider2, ...
'visible', 'off',...
'min', 0, 'max', 1, 'value', 1,...
'sliderstep', [0.05, 0.1]);
set(handles.slider3, ...
'visible', 'off',...
'min', 0, 'max', 1, 'value', 0,...
'sliderstep', [0.05, 0.1]);
set(handles.slider4, ...
'visible', 'off',...
'min', 0, 'max', 1, 'value', 1,...
'sliderstep', [0.05, 0.1]);
set(handles.slider5, ...
'visible', 'off',...
'min', 0, 'max', 10, 'value', 1,...
end
if handles.current_contrast == @imadjust
handles.image2 = imadjust(handles.image,[slider1value; slider2value], [slider3value; slider4value], slider5value);
end
imshow(handles.image2, 'Parent', handles.axes2);
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global slider1value;
slider1value = get(handles.slider1, 'value')
drawnow
% update handles structure
guidata(hObject,handles)
I know there's a bunch going on there. All I want to know is if someone can figure out why handles.image2 doesn't update when the slider values are changed.

 Accepted Answer

Because the function slider1_Callback doesn't do anything with handles.image2. Only your popup callback changes handles.image2.

8 Comments

I thought the global variable would do something that it's not doing. So how do I write a callback for slider1 that incorporates handles.image2 without having to call the whole function that I used again?
Each slider is going to be used for modifying the parameters of all kinds of functions, so I want the slider_callback to only update the value of the slider and then pass that back to the function containing the parameter that it's assigned to update.
The slider callback does NOT update the value of the slider. Moving the slider first updates the slider value and then puts you into the slider callback function where you can immediately get its new value and use it.
Each callback function has access to handles, so you can get the value of any slider or any checkbox or any radio button in any callback. You can even get the value of any variable you attach to handles, like image2, as long as you updated handles by calling guidata(). So in the slider callback, just get its value (like you're already doing, but you don't need the drawnow) and then use that and handles.image2 in whatever way you want, for example
scaledImage = sliderValue * handles.image2; % or whatever you want.
or if slider represents a window width, you might do this to blur a grayscale image:
kernel = ones(sliderValue) / sliderValue^2;
blurryImage = conv2(handles.image2, kernel);
imshow(blurryImage, []);
or whatever you want to do when you move your slider.
That's what I was afraid of. Will I have to code a bunch of if statements for all of the cases that the slider could control when it moves? For example, slider1_callback will be used for nearly every processing step in my GUI, but it will be performing different tasks, and hence, the coding will be different depending on the function being modified. Is there no simple way to accomodate this?
That's nothing to be afraid of. That's just common sense. If you're having just one slider mean different things, then of course it will be used for different things. You know that already so I'm puzzled why you said "I was afraid of that". In the callback for your popup, you can call different functions: function1, function2, function3, etc. Inside each of those functions you can use sliderValue = get(handles.slider1, 'value'), and that sliderValue means different things in each functions, exactly as you want, need, and should expect. How could it be any other way? If you want sliderValue to be window width in function 1, then fine, it's window width. If you want it to be weight (or distance, or amplitude, or whatever) in function 2, then fine. So what's the problem?
I'm thinking that my GUI is going to get pretty bogged down because I have all kinds of code to do what seems to be simple tasks. I don't know if all of this is going to end up costing me speed. What I thought originally is that I would be able to pass the information from the slider back to the original program. The way it looks now, I can call the function within the slider_callback() and adjust values there, but that just makes my slider programming a lot longer/more tedious.
Where do you actually want to do the various functions? In the popup callback? Well if that resets the slider limits, and then does the function, then how are you going to give the user the ability to move/adjust the slider?
Well I thought I might be able to do that originally, where I can make adjustments within the popup callback, which would have made the slider programming quicker. Instead, I'm doing if statements within the slider callbacks to make the sliders recognize what their particular operation is depending on the function that was called from the popup menu.
The problem I see arising is that my program could be extremely slow. In essence, I'm making an image processor complete with morphological operaters, contrast adjusters, filters and whatever else you could do to an image. When a particular function is called, the sliders adjust to mimic what the user would input if they were writing these functions from the command line.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!