Help with set function, 'CloseRequestFunc' and Calling variables out of the function.
10 views (last 30 days)
Show older comments
I'm trying to use matlab to directly alter the contrast of an stock image with just the imcontrast figure. and link my contrast adjustments to the imadjust function. This way, I can automatically adjust contrast in an image without having to write any numbers down or save an image. I use the Closerequestfunc to trigger a function that records the final contrast values on the slider when I close out of the contrast figure. My trouble is that I cannot figure out a way to use the the final variables (the windowclose_min ; windowclose_max) in my function outside of the function unless I make them global variables. Pls Help!
Note!!! Do not hit "Adjust Data" in the imcontrast figure, just adjust the bars and close.
I = imread('pout.tif')
imshow(I)
imcontrast;
F = imcontrast(gcf);
window_min = str2double(get(findobj(F, 'tag', 'window min edit'), 'String'));
window_max = str2double(get(findobj(F, 'tag', 'window max edit'), 'String'));
disp(window_min) %toggle on and off at will
disp(window_max) %toggle on and off at will
set(F, 'CloseRequestFcn', @(s,e)getValues(s)); %This function still confuses me. I do not know what the s or e represent.
uiwait %activates once you close out of the first image (I)
G = imadjust(I,[closewindow_min/window_max closewindow_max/window_max],[0 1]);
imshowpair(I,G,'Montage')
function getValues(F)
global closewindow_min
global closewindow_max
closewindow_min = str2double(get(findobj(F, 'tag', 'window min edit'), 'String'));
closewindow_max = str2double(get(findobj(F, 'tag', 'window max edit'), 'String'));
disp(closewindow_min);
disp(closewindow_max);
delete(F) %This is the only way I could figure out how to close the imcontrast figure
end
4 Comments
Stephen23
on 17 Aug 2018
Edited: Stephen23
on 17 Aug 2018
"This function still confuses me. I do not know what the s or e represent"
As described in the MATLAB documentation:
"Graphics callback functions must accept at least two input arguments:"
- "The handle of the object whose callback is executing. Use this handle within your callback function to refer to the callback object."
- "The event data structure, which can be empty for some callbacks or contain specific information that is described in the property description for that object."
"Note!!! Do not hit "Adjust Data" in the imcontrast figure, just adjust the bars and close"
Clicking the "Adjust Data" button is exactly how imcontrast is designed to work. Using the tool as it was designed would mean that you don't have this problem, so you could spend time on solving more useful problems, or enjoying a nice drink in the garden.
This is a serious point: how important is this to you? How much time are you willing to spend creating some (possibly fragile) code just so that you don't need to click the big "Adjust Data" button? Consider your time investment cost and the (small) potential benefit that you expect to get...
Accepted Answer
Stephen23
on 17 Aug 2018
Edited: Stephen23
on 17 Aug 2018
One easy solution is to use a nested function (will not work for GUIDE: use guidata instead). Basically you would need to do something like this (pseudocode):
function out = mainfun(imh)
out = []; % important to allocate here!
ich = imcontrast(imh);
set(ich, 'CloseRequestFcn',@myClose)
%
function myClose(h,e)
out = get(findobj(h,'tag','window min edit'), 'String');
delete(h)
end
end
%
waitfor(ich)
end
and call it like this:
imh = imshow(I);
new = mainfun(imh)
More Answers (0)
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!