MATLAB Answers

Using GUI to Edit Image

4 views (last 30 days)
K F
K F on 27 Aug 2020
Edited: KALYAN ACHARJYA on 27 Aug 2020
I want to make a GUI window that allows the user to change the threshold value of a binarized of an image and see the output. The GUI would show the binarized image, a text box, and a button. The inital image would use the default Matlab threshold value. If the user was unhappy with it, they could type a new thresholding value in the text box and push the button to update the image.
The code I have written is capable of doing, but only once. I want to user to be able to update the image with a new value as many times as they want. What sort of edits can I make to allow for this?
figure
iptsetpref('ImshowInitialMagnification',50)
img_data=imcomplement(imbinarize(mat2gray(raw_data)));
handles.img_1=imshow(img_data)
%make the text box
edit_box_h = uicontrol('style','edit',...
'units', 'normalized',...
'position', [0.45 0.1 0.05 0.05]);
%make the button
but_h = uicontrol('style', 'pushbutton',...
'string', 'Update Thresholding',...
'units', 'normalized',...
'position', [0.4 0 0.2 0.1],...
'callback', {@thrs_img, edit_box_h, img_data,lvl_data});
%Function code located in a separate file -------------------------------------------------------------
function thrs_img(~,~,edit_box_h, img_data, raw_data)
str_entered = get(edit_box_h, 'string'); %get the user text input
dbl_entered = str2double(str_entered);
if dbl_entered>0 && dbl_entered<1
img_data1=imcomplement(imbinarize(mat2gray(raw_data),dbl_entered)); %binarize using the new threshold value
handles.img_1=imshow(img_data1)
end

  1 Comment

Mario Malic
Mario Malic on 27 Aug 2020
I think you need to have a callback to update your variable edit_box_h when you change its value. I am not sure if functions share their variables, so you can use make a public property instead and pass it on.
I am not sure how one does it in GUI, since I did not use it. If there's a numeric box in GUI, use that one, also, there's a possibility for such box to limit its acceptable numeric range.

Sign in to comment.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 27 Aug 2020
Edited: KALYAN ACHARJYA on 27 Aug 2020
I am providing here the alternate option, now a days I rarely do GUI (Guide option). MATLAB have great appdesigner features, you can do it with 5 minutes. Please enter appdesigner in command window, see the layout of the new window or see any basic tutorial, you can do it easily.
Like in your case, please note this the partial code of callback function
function ConvertButtonPushed(app, event)
im=rgb2gray(imread('test_image.png'));
imshow(im,'parent',app.UIAxes);
th=app.ThresholdValueSlider.Value;
bwImage=im2bw(im,th);
imshow(bwImage,'parent',app.UIAxes2);
end
Cheers!

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!