How to save a value from a slider

조회 수: 8 (최근 30일)
Cait Newport
Cait Newport 2022년 1월 21일
답변: Voss 2022년 1월 21일
I am trying to extra a value from a slider in a GUI. I have followed many suggestions buy none of them seem to work on the code I already have. The intention is that a video frame will appear as a gray frame. The user will then move the slider which will produce a binary image and adjust the black and white threshold. When the threshold is correct, the user will press a button to close the GUI and save the threshold, which will then be used in another section of code.
Here is what I have so far:
function buttonPlot(Frame)
%display a video frame
Frame=rgb2gray(Frame);
fig=uifigure('Name', 'Set the black-white threshold');
fig.Position=[100 100 1200 800]; %this sets the size of the gui
cg=uiaxes(fig, 'Position', [50 150 1920*.5 1080*.5],'Visible', 'off');
imshow(Frame, 'parent', cg);
%Slider
sld=uislider(fig, 'Position', [50 100 1920*.5 3], 'Value', 50, 'Limits', [20, 200],'ValueChangingFCN',@(sld,event) changeThreshold(event,cg,Frame),'BusyAction','cancel');
thresholdValue=[];
%Button
btn=uibutton(fig,'push', 'Text', 'Save Value', 'Position', [75+1920*.5, 1080*.5, 100, 22],'ButtonPushedFcn', @(btn,event) plotButtonPushed(btn,event,fig,thresholdValue));
%% Slider callback
function slider=changeThreshold(event,cg,Frame)
% hobj is the image object handle
threshold=event.Value;
%display frame as a binary image. Changing the slider adjust the
%threshold
binaryImage = Frame < threshold;
binaryImage = imfill(binaryImage, 'holes');
fish_area=200;
bw=bwareaopen(binaryImage,fish_area);
L=bwlabel(bw);
s=regionprops(L,'area','centroid');
area_vector=[s.Area];
[tmp,idx]=max(area_vector);
imshow(bw,'parent', cg);
%% Button to close GUI and save the threshold value
function button=plotButtonPushed(btn,event,fig,thresholdValue)
close(fig)
end
end
end

답변 (1개)

Voss
Voss 2022년 1월 21일
One thing to note is that plotButtonPushed() is nested inside changeThreshold(), so the main function buttonPlot() will not see it and you'll get an error when pushing the button. Instead, both changeThreshold() and plotButtonPushed() should be nested inside buttonPlot(), like this:
function buttonPlot(Frame)
%display a video frame
Frame=rgb2gray(Frame);
fig=uifigure('Name', 'Set the black-white threshold');
fig.Position=[100 100 1200 800]; %this sets the size of the gui
cg=uiaxes(fig, 'Position', [50 150 1920*.5 1080*.5],'Visible', 'off');
imshow(Frame, 'parent', cg);
%Slider
sld=uislider(fig, 'Position', [50 100 1920*.5 3], 'Value', 50, 'Limits', [20, 200],'ValueChangingFCN',@(sld,event) changeThreshold(event,cg,Frame),'BusyAction','cancel');
thresholdValue=[];
%Button
btn=uibutton(fig,'push', 'Text', 'Save Value', 'Position', [75+1920*.5, 1080*.5, 100, 22],'ButtonPushedFcn', @(btn,event) plotButtonPushed(btn,event,fig,thresholdValue));
%% Slider callback
function slider=changeThreshold(event,cg,Frame)
% hobj is the image object handle
threshold=event.Value;
%display frame as a binary image. Changing the slider adjust the
%threshold
binaryImage = Frame < threshold;
binaryImage = imfill(binaryImage, 'holes');
fish_area=200;
bw=bwareaopen(binaryImage,fish_area);
L=bwlabel(bw);
s=regionprops(L,'area','centroid');
area_vector=[s.Area];
[tmp,idx]=max(area_vector);
imshow(bw,'parent', cg);
end
%% Button to close GUI and save the threshold value
function button=plotButtonPushed(btn,event,fig,thresholdValue)
close(fig)
end
end
Another thing to consider is that rgb2gray() returns a grayscale image with values between 0 and 1, but the threshold is defined to be between 20 and 200, so you'll need to scale one or the other in order to compare them on the same basis, e.g., Frame = 255*rgb2gray(Frame/255); I'm not sure of the exact transform because I don't know what your Frame data is.

카테고리

Help CenterFile Exchange에서 Display Point Clouds에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by