Hello everyone, i'm new to matlab and i'm trying to implement slider for un sharp mask filter and i don't know how to do that. I implemented slide for brightess and my code in slider1_Callback function looks like this:

2 views (last 30 days)
global im
if(isempty(im))
h = msgbox('Please browse image file.');
else
val = 1 * get(hObject,'Value') - 1;
imbright=im+val;
axes(handles.axes1);
imshow(imbright);
end

Answers (1)

Geoff Hayes
Geoff Hayes on 20 Oct 2017
Dan - please clarify what (with the code you have provided) is the behaviour that you are observing. Is there any change to the image in the axes? Is there no change but a new figure opens?
Also, I recommend that you avoid using global variables like im. Just save the image (that the user has chosen) to the handles structure like
% in the function that picks the image
handles.im = im;
guidata(hObject, handles);
Then your above code would become
if ~isfield(handles, 'im')
msgbox('Please browse image file.');
else
val = 1 * get(hObject,'Value') - 1;
imbright=handles.im+val;
axes(handles.axes1);
imshow(imbright);
end
You may then want to save the updated image back to the handles structure so that the next time the slider moves, you use the already updated image to brighten or darken.
if ~isfield(handles, 'im')
msgbox('Please browse image file.');
else
val = 1 * get(hObject,'Value') - 1;
imbright=handles.im+val;
axes(handles.axes1);
imshow(imbright);
handles.im = imbright;
guidata(hObject, handles);
end
Note that I'm assuming that you are using GUIDE since you have referenced the handles structure.
See also continuous slider callback for a means to update the image when the slider moves and not just when it stops (like it would do for your callback).
  1 Comment
Dan the Man
Dan the Man on 20 Oct 2017
Edited: Dan the Man on 20 Oct 2017
Geoff thank you for fast response, i'll change that. I implemented slider for brightess and everything is working fine (i'm following GUI tutorial), but when i tried to implement same thing for un sharpening on my own, that works fine but my image changes brightness too (i do not want brightness to change). Sorry if this is a stupid question, but i started y-day :) My code is below:
filter = fspecial('unsharp',0.5);
val = 0.5 * get(hObject,'Value') - 0.5;
result = imfilter(im, filter) + val;
axes(handles.axes1);
imshow(result);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!