GUI - Error when trying to program the second slide bar for the upper limit for variable hue in image processing (lower limit succeeded)
Show older comments
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
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.
Accepted Answer
More Answers (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 Comments
Rik
on 30 Nov 2021
What is the point of this modification? What makes it different from my answer? I don't see why this couldn't be a comment on my answer. Can you explain that?
S.
on 30 Nov 2021
S.
on 1 Dec 2021
yanqi liu
on 1 Dec 2021
yes,sir,because I refer to some codes
anyway, If you can solve the problem, I wish you every success
Categories
Find more on White in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!