Help with set function, 'CloseRequestFunc' and Calling variables out of the function.

10 views (last 30 days)
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
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...
Alan Lee
Alan Lee on 21 Aug 2018
Thanks for the reply! The reason I'm trying to avoid the adjust data button is that I would like to take my contrast adjustments and automatically have them link into later parts of the code that control for thresholding of the image. I have quite a few images so I would rather like to no have to individually save and import everytime I run the contrast tool. Now if you know a smarter way of going about this business, please do share! You seem rather experienced and I'm trying to play with your psuedocode now.

Sign in to comment.

Accepted Answer

Stephen23
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)
  1 Comment
Alan Lee
Alan Lee on 22 Aug 2018
Thanks to the Big Mony MAAAN Stephen Cobeldick, we have an answer ladies and gentlemen. It took a bit for my pea brain to work through his response, but I assure you the results work. So, here is the modified code with the ability to call the values (and display them) when you adjust the contrast in imcontrast so that you can use them later in the code without having to save and import the adjusted data again and again. Again, Big Shout Out to the Big MONY MAAAN!
J = imread('pout.tif');
imshow(J) %Shows(I) as an image
imcontrast(imshow(J));
win_min = str2double(get(findobj(imcontrast(imshow(J)), 'tag', 'window min edit'), 'String'));
win_max = str2double(get(findobj(imcontrast(imshow(J)), 'tag', 'window max edit'), 'String'));
imh = imshow(J);
[closewin_min, closewin_max]= mainfun1(imh);
disp(win_min)
disp(win_max)
disp(closewin_min)
disp(closewin_max)
uiwait
G = imadjust(J,[closewin_min/win_max closewin_max/win_max],[0 1]);
imshowpair(J,G,'Montage')
function [out1, out2] = mainfun1(imh)
out1 = []; % important to allocate here!
out2 = [];
ich = imcontrast(imh);
set(ich, 'CloseRequestFcn',@myClose)
%
function myClose(h,~)
out1 = str2double(get(findobj(h,'tag','window min edit'), 'String'));
out2 = str2double(get(findobj(h,'tag','window max edit'), 'String'));
delete(h)
end
%
waitfor(ich)
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!