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

1 次查看(过去 30 天)
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

采纳的回答

Rik
Rik 2021-11-29
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 个评论
Rik
Rik 2021-12-2
You need to make that confirmation button yourself.
If you would read the documentation you would see that the Position argument can be specified in several units, which is determined by the Unit property. In this case the default is Normalized, meaning that the x is in terms of the width of the parent object and the y is in terms of the height of the parent object.
Most of the follow-up issues so far seem to stem from the point that you don't seem to read the documentation for the functions you're using. If you start reading it yourself you don't have to wait for me to respond.
S.
S. 2021-12-2
编辑:S. 2021-12-2
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.

请先登录,再进行评论。

更多回答(1 个)

yanqi liu
yanqi liu 2021-11-30
编辑:yanqi liu 2021-12-1
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by