How to connect GUI sliders to image analysis

I want to instantiate this GUI
The project is the analysis of bee pollen.I first load the image by the left pushbutton.There is a warnig msg imcrop which croppes the image to the desired ROI.ANd then there is a Statistic Analysis.Because of the trash there is a second take by which you can select the Threshold (slider on the top) and also select which sizes aroud the mean Area to select(slider 2&3 below).Then by pushbutton(new Analysis )there is a retake with new Statistic analysis..My problem is i dont know how to connect the sliders and the new Analysis with the retake procedure.This is my GUI code.
EDIT: I've attached your code to your post rather than display it all here

 Accepted Answer

This is how I would modify your gui to get the image erodeII and slider values into pushbutton3_Callback
...
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
...
handles.erodedII = imerode(erodedI,se);
axes(handles.axes2)%%%%%%%%%
imshow(handles.erodedII);
...
L=bwlabel(handles.erodedII);
% Update handles structure
guidata(hObject, handles);
...
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
...
handles.slide_thresh=get(hObject,'value');
% Update handles structure
guidata(hObject, handles);
...
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
...
handles.slide_min=get(hObject,'value');
% Update handles structure
guidata(hObject, handles);
...
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
...
handles.slide_max=get(hObject,'value');
% Update handles structure
guidata(hObject, handles);
...
...
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img = handles.erodedII;
thresh = handles.slide_thresh;
slide_min = handles.slide_min;
slide_max = handles.slide_max;
In pushbutton3, you do not have to assign fields on the handles structure to a variable in order to use them. I just did that to demonstrate how to access them.

More Answers (3)

Sorry but it's not clear to me what you mean by "retake".
From what I understand, I would recommend moving your statistics code to a new function (you can create your own functions, in addition to callbacks). At the end of your callback function that loads the image (pushbutton1?) you can call the statistics function.
The benefit is that any other callback can also call the statistics function. So in your New Analysis callback (pushbutton2?), you would also just call the statistics function.
Any variables you need could be added to the handles structure to make them available.

3 Comments

In the first part i crop the image and from now on this is the only one image to work.I dont want another ROI.So with this ROI i change parameters and have a new statistic analysis...
But this is the last part of the story..I dont know how to connect the sliders!!
Connect to what? What should they be doing that they are not?
I see that changing the sliders 1-3 updates the string in edit10, edit11 and edit12 (though I can only guess which slider/edit box each one is). You know how to set the value of a slider from other callbacks.
set(handles.slider1,'value',string2num(edit));
Use a 'get' instead to access the slider value when needed in your new analysis
slide_thresh=get(handles.slider1,'value');
The other option is to add your slider values to the handles structure. Just be sure to update the handles structure so the changes are saved.
handles.slide_thresh=get(hObject,'value');
set(handles.edit10,'string',num2str(slide_thresh));
% Update handles structure
guidata(hObject.handles);
Like Cris say, you can create a custom function, like "PerformStatistics" or something:
function results = PerformStatistics(handles)
% Now do stats and make results
You only need handles if you want to use it in the function, for example to read the value of a slider, to get what files the user clicked on in a listbox, to check if the user checked a checkbox, etc. You could also pass in the values if you want, for example:
function results = PerformStatistics(sliderValue, computeResiduals, showPlot)
Then, in the callback function of the slider, or a button, or a checkbox, or wherever you want, you can just call the function, for example:
sliderValue = handles.slide_thresh.Value;
computeResiduals = handles.chkResiduals.Value;
results = PerformStatistics(sliderValue, slide_thresh, handles.chkShowPlot.Value);
So in your slider callback, you might simply set up the text label for it (assuming you have one)
handles.txtSliderLabel.String = sprintf('Threshold = %.2f', handles.slide_thresh.Value);
and then call PerformStatistics. If you have a table (uigrid) control on your GUI then you can load your results into that if you want. Or you could call xlswrite() or whatever you want to do with the results. You don't even have to pass any results back to the calling routine if you take care of everything inside PerformStatistics(). I hope that explains things a little more for you.

Sign in to comment.

Ι am still very confused...I start with some default values and crop the image.I get an analyzed image.Now i use the slider to get new values and then i pushbutton "new Analysis".
I dont want a new function with another new ROI i want to use the same ROI as in the beginning .But pushbutton3 doent recognize the cropped image(erodedII)..
how can i pass erodedII to pushbutton 3,, get new values from sliders 1-3 and reanalyse..
Forgive me, i am new in matlab and i apologise for this,,,,You guys give me the best advice but i still dont get it...

1 Comment

Do you realize there is no code in pushbutton3's callback?
Let's forget the gui for a second.
Can you provide a script that starts with the cropped image and runs the new analysis and statistics on it? If you can provide working code, it will be much easier for us to show you how to adapt that to a gui.
Also, as I said before, we have no idea what components go to what callbacks. Some we can guess, but you also need to provide that info.

Sign in to comment.

DIMITRIOS THEODOROPOULOS
DIMITRIOS THEODOROPOULOS on 21 Dec 2018
Edited: Cris LaPierre on 21 Dec 2018
Cris i copied your code and get the following error
i modified the code bit in order to see the values from sliders in edit texts..But it does work either..
here is the complete code
EDIT: I've attached your updated code to your post rather than display it all here

7 Comments

Please attach the .m file and .fig file with the paper clip icon.
The error is because you put a period instead of a comma on line 360. It should be
guidata(hObject, handles);
Be careful - I didn't give you working code. I only shared what lines need to be changed. Be careful to copy the various bits of code I posted to its corresponding callback function. Basically, add 'handles." to the variables that store the image erodedII and the slider values (thresh, slider_min and slider_max).
Once you have done that, you can access those variables in the pushbutton3 callback as I showed.
Here are some links that might be helpful to you:
  1. Debugging your code
  2. Store or retrieve UI data
  3. Sharing data between callbacks in Guide
  4. Writing callbacks in Guide
Your slider values should appear in the edit boxes as soon as you replace the period with a comma in the guidata function call.
Also note that your edit10 callback at least is using string2num. That is not a MATLAB function. Do you mean str2num? Code analyzer will recommend you use str2double. Make that correction and changing the value in the edit box will update the slider as well.
Here is how I would code slider1 and edit10 to communicate with each other
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
handles.thresh=get(hObject,'value');
set(handles.edit10,'string',num2str(handles.thresh));
guidata(hObject, handles);
function edit10_Callback(hObject, eventdata, handles)
edit=get(hObject,'string');
set(handles.slider1,'value',str2double(edit));
i think you guys have helped me a lot!
this seems to work..The only problem is when i dont use the sliders it doesnt recognize the fields...
That's probably because the variables thresh, slider_min and slider_max haven't been assigned a value yet. One option is to run your slider functions in the OpeningFcn. Just make sure the slider startup values (assigned in the component properties in the fig file) are what you want to use as your default values.
slider1_Callback(handles.slider1, eventdata, handles)
slider2_Callback(handles.slider2, eventdata, handles)
slider3_Callback(handles.slider3, eventdata, handles)
The other option is to just assign values to your varials in the OpeningFcn. If you want to do it that way, just add the following code. Same tip about setting the default values applies.
handles.slide_thresh=get(hObject,'value');
handles.slide_min=get(hObject,'value');
handles.slide_max=get(hObject,'value');
following the second advise i just copy/paste the 3 lines...but i get this :ice_screenshot_20181221-172353.png
i solved it by using in pushbutton3 area:
if isfield (slide_thresh) &(slide_min) &(slide_max)
% pushbutton3 code...
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!