GUI - Error when trying to program the second slide bar for the upper limit for variable hue in image processing (lower limit succeeded)

3 views (last 30 days)
I am trying to implement two sliders - one for the lower limit and one for the upper limit - for an image processing project I am doing myself. I am trying to seperate fruits from the background by using a range of the hue value. I succeeded to implement one slider, the one for the lower limit. However, when I add the code of the second slider for the upper limit unfortunately I get the following error:
Error using uicontrol
Incorrect number of input arguments
Can someone help me with it?
RGB_Image_Re = imread('42.jpg');
%Shifting color model
HSV_B = rgb2hsv(RGB_Image_Re);
%Color seperation
hImage = HSV_B(:, :, 1);
f = figure(1);
ax = axes('Parent',f,'position',[0.13 0.39 0.77 0.54]);
%Initial values
Hue_min = 0;
Hue_max = 1;
%Slider for the lower limit of hue
Hmin_Slider = uicontrol('Parent',f,'Style','slider','Position',[81,54,419,23],...
'value', Hue_min, 'min',0, 'max',0.5, 'Callback', @(es, ed)Slider(RGB_Image_Re, hImage, es.Value));
% % Add a text uicontrol to label the slider.
% txt_Hmin = uicontrol('Style','text',...
% 'unit','normalized',...
% 'position',[81,55,419,24],...
% 'String','Fitting Parameter "H_min"');
%Slider for the upper limit of hue
Hmax_Slider = uicontrol('Parent',f,'Style','slide','Position',[81,54.1,419,23],...
'value', Hue_max, 'min',0,51, 'max',1, 'Callback', @(es, ed)Slider(RGB_Image_Re, hImage, es.Value));
%
% % Add a text uicontrol to label the slider.
% txt_Hmax = uicontrol('Style','text',...
% 'unit','normalized',...
% 'position',[81,60,419,24],...
% 'String','Fitting Parameter "H_max"');
function Slider(RGB_Image_Re, hImage, Hue_min, Hue_max)
mask = (hImage >= Hue_min) & (hImage <= Hue_max);
% Create variable masked image (output) based on RGB image (input).
maskedRGBImage = RGB_Image_Re;
% Set background pixels (i.e. leaves, sky, logs) where the mask is false to
% black color [0,0,0]
maskedRGBImage(repmat(~mask,[1 1 3])) = 0;
imshow(maskedRGBImage);
title('Initial color seperation: fruit');
end
  1 Comment
Rik
Rik on 29 Nov 2021
I have deleted the duplicate post as a comment on another thread. Please do not post your question in multiple places.

Sign in to comment.

Accepted Answer

Rik
Rik on 29 Nov 2021
Your syntax doesn't match up. In your callback definition you have only 3 inputs, but your function assumes 4. I would suggest storing all handles and data in guidata and retrieving it in the callback.
You should also always use explicit handles to graphics objects. You also might consider using imshow only during initialisation, and then only setting the CData property of the image object it returns. That will be much faster.
h=struct; %put all handles and data in this struct
h.f=f;
%Slider for the lower limit of hue
h.min_Slider = uicontrol('Parent',f,'Style','slider', ...
'Units','Pixels','Position',[81,54,419,23],...
'value', Hue_min, 'min',0, 'max',0.5,...
'Callback', @Slider);
h.max_Slider = uicontrol('Parent',f,'Style','slider',...
... %this was missing an r
'Units','Pixels','Position',[81,54.1,419,23],...
'value', Hue_max, 'min',0.51, 'max',1,...
... %this was 0,51
'Callback', @Slider);
guidata(h,h.f)
function Slider(hObj,eventdata)
h=guidata(hObj);
RGB_Image_Re=h.RGB_Image_Re;
hImage=h.hImage;
Hue_minh.min_Slider.Value;
Hue_max=h.max_Slider.Value;
mask = (hImage >= Hue_min) & (hImage <= Hue_max);
% Create variable masked image (output) based on RGB image (input).
maskedRGBImage = RGB_Image_Re;
% Set background pixels (i.e. leaves, sky, logs) where the mask is false to
% black color [0,0,0]
maskedRGBImage(repmat(~mask,[1 1 3])) = 0;
imshow(maskedRGBImage,'Parent',h.ax);
title(h.ax,'Initial color seperation: fruit');
end
  14 Comments
S.
S. on 2 Dec 2021
Edited: S. on 2 Dec 2021
Yes, I understood the Position, otherwise I wouldn't succeed (at first I didn't understand, but when I read the documentation I succeeded). I think you misunderstood me. I removed that comment.
My main focus is not the GUI. That's why I got confused by the button. I only want the parameters as output.

Sign in to comment.

More Answers (1)

yanqi liu
yanqi liu on 30 Nov 2021
Edited: yanqi liu on 1 Dec 2021
yes,sir,use Rik method,modify as
clc; clear all; close all;
RGB_Image_Re = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/816944/42.jpg');
%Shifting color model
HSV_B = rgb2hsv(RGB_Image_Re);
%Color seperation
hImage = HSV_B(:, :, 1);
f = figure(1);
ax = axes('Parent',f,'position',[0.13 0.39 0.77 0.54]);
% here use Rik method
h=struct; %put all handles and data in this struct
%Initial values
Hue_min = 0;
Hue_max = 1;
%Slider for the lower limit of hue
h.min_Slider = uicontrol('Parent',f,'Style','slider', ...
'Units','Pixels','Position',[81,54,419,23],...
'value', Hue_min, 'min',0, 'max',0.5);
h.max_Slider = uicontrol('Parent',f,'Style','slider',...
... %this was missing an r
'Units','Pixels','Position',[81,77,419,23],...
'value', Hue_max, 'min',0.51, 'max',1);
% here modify some code, not use guidata, just use h
set(h.min_Slider, 'Callback', @(es, ed)Slider(RGB_Image_Re, hImage, h));
set(h.max_Slider, 'Callback', @(es, ed)Slider(RGB_Image_Re, hImage, h));
function Slider(RGB_Image_Re, hImage, h)
Hue_min=h.min_Slider.Value;
Hue_max=h.max_Slider.Value;
mask = (hImage >= Hue_min) & (hImage <= Hue_max);
% Create variable masked image (output) based on RGB image (input).
maskedRGBImage = RGB_Image_Re;
% Set background pixels (i.e. leaves, sky, logs) where the mask is false to
% black color [0,0,0]
maskedRGBImage(repmat(mask,[1 1 3])) = 0;
imshow(maskedRGBImage);
title('Initial color seperation: fruit');
end
  5 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by