Borrar filtros
Borrar filtros

how to change a variable value in a while loop?

2 visualizaciones (últimos 30 días)
jim
jim el 3 de Abr. de 2011
Hi, I have a while loop in my matlab code and one of the variables in the loop 'Threshold_Value' is intially set to 0.5, but i would like to use a slider button in my matlab graphical user interface to adjust this value from 0 to 3.5, This value is used to change the threshold on a binary image(making it darker or lighter).
I have created the slider and set the min and max values correctly. But when i try and change the values with the slider it apears to change the value of 'Threshold_Value' as i can see it printing out the new values of the slider, But it apears to have no affect on the binary image?
The problem seems to be that although the value for 'Threshold_Value' is being changed when i move the slider it does not update the value in the loop ?
main code i used shown below
% --- Executes on button press in start_pushbutton.
function start_pushbutton_Callback(hObject, eventdata, handles)
%initialize the flag variable
set(handles.start_pushbutton,'UserData',1);
imaqhwinfo ('winvideo', 1);
video = videoinput('winvideo',1,'RGB24_640x480');
origimage = 0; %initilises origimage
preview(video);
Threshold_Value = 0.5
%while the flag variable is one, the loop continues
while (get(handles.start_pushbutton,'UserData') ==1)
origimage=getsnapshot(video);
pause(0.005)
imgGray = rgb2gray(origimage); %Converts Image to GrayScale
Threshold_Value = noiseThreshold ; %Changes senstivity
imgB = im2bw(imgGray,noiseThreshold); %converts to binary
imshow(imgB); %displays binery image
end
% --- Executes on slider movement.
function Slider_Callback(hObject, eventdata, handles)
% hObject handle to Slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Threshold_Value = get(hObject,'Value')

Respuestas (1)

Matt Fig
Matt Fig el 3 de Abr. de 2011
I don't see where in the loop you are retrieving the value of the slider. Each time through the loop you will have to retrieve the value of the slider and set Threshold_Value equal to it.
Even if you do this, it appears that you are setting Threshold_Value equal to noiseThreshold, which I am guessing is a function.
  3 comentarios
Matt Fig
Matt Fig el 3 de Abr. de 2011
You are retrieving the value of the slider in its own callback, but that value for Threshold_Value has nothing to do with the value for Threshold_Value in another function. Each function has its own workspace, so the variables created in one function do not exist in another function.
You need to get the value of the slider each time through the loop. What is noiseThreshold? Is it a function? This code must surely error if it is not.
jim
jim el 3 de Abr. de 2011
noiseThreshold is just a variable i set to 0.5, I could have just replaced it with 0.5.
e.g imgB = im2bw(imgGray, 0.5); %converts to binary
But because i wanted to make the number changeable i called it noiseThreshold.
noiseThreshold = 0.5
e.g imgB = im2bw(imgGray, noiseThreshold); %converts to binary
So do you know of a way i can get the new value to change/update the noiseThreshold eat time the program loops?
Thanks a lot for the advice and help!!

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by