フィルターのクリア

Variable number of output arguments in a function

3 ビュー (過去 30 日間)
giacomo
giacomo 2017 年 9 月 28 日
コメント済み: giacomo 2017 年 10 月 4 日
Hi guys. I'm sorry if this is a basic question but I'm stuck. I have a function that I need to keep very general, since it's attached to a switch menu where I test three different structural elements from three differents POV (frontal, crest, back) using another switch menu. The problem is that I need to detect colors, but colors change in type AND also in quantity. Being the max number 3, I have a problem in the crest where I just have one color. The best way would be to 'mute' the variables of the other 2 colors but how do I do it? If I put [] the program crashes. Basically sometimes I don't need to use all the variables and I need them not to interfer with the rest of the code.

回答 (1 件)

KSSV
KSSV 2017 年 9 月 28 日
編集済み: KSSV 2017 年 9 月 28 日
  10 件のコメント
giacomo
giacomo 2017 年 9 月 28 日
Let's try again. I'm adapting the SimpleColorDetectionByHue() code by Image Analyst to my needs. Link to that here: https://es.mathworks.com/matlabcentral/fileexchange/28512-simplecolordetectionbyhue-- In his code the switch menu is used to create a mask (using HSV) to detect the number of pixel of a given color. Mine is a bit different since I have to create up to 3 masks (for up to 3 colors). The problem is that if I create 3 masks it works fine, but if I create 1 or 2 it goes bad. Next I copy and paste the code so you can better understand, but since my explanations are poor you can look the Image Analyst code up to get an idea. Function definition:
% code
%%%%%%%%%%%%%
function [hueThresholdLowR1, hueThresholdLowR2, hueThresholdLowG, hueThresholdLowB, hueThresholdHighR1, hueThresholdHighR2, hueThresholdHighG, hueThresholdHighB, saturationThresholdLowR, saturationThresholdLowG, saturationThresholdLowB, saturationThresholdHighR, saturationThresholdHighG, saturationThresholdHighB, valueThresholdLowR, valueThresholdLowG, valueThresholdLowB, valueThresholdHighR, valueThresholdHighG, valueThresholdHighB] = SetThresholds()
try
button = menu('Which structure do you want to analise?', 'Cubipods', 'Cubes', 'Rocks');
switch button
case 1
% Cubipods
% Cubipods colors threshold
%Blue
hueThresholdLowB = 0.42;
hueThresholdHighB = 0.73;
saturationThresholdLowB = 0.68;
saturationThresholdHighB = 0.91;
valueThresholdLowB = 0.53;
valueThresholdHighB = 0.83;
%Green
hueThresholdLowG = 0.16;
hueThresholdHighG = 0.6;
saturationThresholdLowG = 0.36;
saturationThresholdHighG = 1;
valueThresholdLowG = 0;
valueThresholdHighG = 0.8;
%Red
hueThresholdLowR1 = 0.8;
hueThresholdHighR1 = 1;
hueThresholdLowR2 = 0;
hueThresholdHighR2 = 0.1;
saturationThresholdLowR = 0.6;
saturationThresholdHighR = 1;
valueThresholdLowR = 0.55;
valueThresholdHighR = 1;
case 2
% % Cubipods crest
% % Cubipods colors threshold
% %Yellow
hueThresholdLowB = 0.10;
hueThresholdHighB = 0.14;
saturationThresholdLowB = 0.4;
saturationThresholdHighB = 1;
valueThresholdLowB = 0.8;
valueThresholdHighB = 1;
% %Green
% hueThresholdLowG = 0.16;
% hueThresholdHighG = 0.6;
% saturationThresholdLowG = 0.36;
% saturationThresholdHighG = 1;
% valueThresholdLowG = 0;
% valueThresholdHighG = 0.8;
%
% %Red
% hueThresholdLowR1 = 0.8;
% hueThresholdHighR1 = 1;
% hueThresholdLowR2 = 0;
% hueThresholdHighR2 = 0.1;
% saturationThresholdLowR = 0.6;
% saturationThresholdHighR = 1;
% valueThresholdLowR = 0.55;
% valueThresholdHighR = 1;
case 3
%Cubipods rear side
% Cubipods colors threshold
% Purple
hueThresholdLowB = 0.76;
hueThresholdHighB = 0.94;
saturationThresholdLowB = 0.33;
saturationThresholdHighB = 0.67;
valueThresholdLowB = 0.1;
valueThresholdHighB = 0.7;
% White
hueThresholdLowG = 0.0;
hueThresholdHighG = 1;
saturationThresholdLowG = 0;
saturationThresholdHighG = 0.36;
valueThresholdLowG = 0.7;
valueThresholdHighG = 1.0;
% %Red
% hueThresholdLowR1 = 0.8;
% hueThresholdHighR1 = 1;
% hueThresholdLowR2 = 0;
% hueThresholdHighR2 = 0.1;
% saturationThresholdLowR = 0.6;
% saturationThresholdHighR = 1;
% valueThresholdLowR = 0.55;
% valueThresholdHighR = 1;
otherwise
warning('The user didnt choose to test any kind of structure');
end
The function is then recalled in the main section:
% code
%%%%%%%%%%%
[hueThresholdLowR1, hueThresholdLowG, hueThresholdLowB, hueThresholdHighR1, hueThresholdHighG, hueThresholdHighB, saturationThresholdLowR, saturationThresholdLowG, saturationThresholdLowB, saturationThresholdHighR, saturationThresholdHighG, saturationThresholdHighB, valueThresholdLowR, valueThresholdLowG, valueThresholdLowB, valueThresholdHighR, valueThresholdHighG, valueThresholdHighB] = SetThresholds()
%MASK
%
% hueMask = (hImage >= hueThresholdLow) & (hImage <= hueThresholdHigh);
% saturationMask = (sImage >= saturationThresholdLow) & (sImage <= saturationThresholdHigh);
% valueMask = (vImage >= valueThresholdLow) & (vImage <= valueThresholdHigh);
% %red
hueMaskR1 = (hImage >= hueThresholdLowR1) & (hImage <= hueThresholdHighR1);
hueMaskR2 = (hImage >= hueThresholdLowR2) & (hImage <= hueThresholdHighR2);
hueMaskR = hueMaskR1 & hueMaskR2;
saturationMaskR = (sImage >= saturationThresholdLowR) & (sImage <= saturationThresholdHighR);
valueMaskR = (vImage >= valueThresholdLowR) & (vImage <= valueThresholdHighR);
% %green
hueMaskG = (hImage >= hueThresholdLowG) & (hImage <= hueThresholdHighG);
saturationMaskG = (sImage >= saturationThresholdLowG) & (sImage <= saturationThresholdHighG);
valueMaskG = (vImage >= valueThresholdLowG) & (vImage <= valueThresholdHighG);
% %blue
hueMaskB = (hImage >= hueThresholdLowB) & (hImage <= hueThresholdHighB);
saturationMaskB = (sImage >= saturationThresholdLowB) & (sImage <= saturationThresholdHighB);
valueMaskB = (vImage >= valueThresholdLowB) & (vImage <= valueThresholdHighB);
% %combine the three masks for the three colors in a unique mask
hueMask = hueMaskR | hueMaskG | hueMaskB;
saturationMask = saturationMaskR | saturationMaskG | saturationMaskB;
valueMask = valueMaskR | valueMaskG | valueMaskB;
The error I get is:
%
Undefined function or variable "hueThresholdLowR1".
Error in menuswitch (line 292)
hueMaskR1 = (hImage >= hueThresholdLowR1) & (hImage <= hueThresholdHighR1);
My point being, I do not need a hueThresholdLowR1 variable for case2 since I'm only interested in detecting one color. Ex. case2 I only need variables ending in thresholdB since I just want yellow. Sometimes I need 3 masks, sometimes 2 sometimes 1. How can I make the code 'flexible' while keeping it as 'general' as possible? Thanks for being patient ^^
giacomo
giacomo 2017 年 10 月 4 日
For the record I solved my problem by myself avoiding a macrofunction instead creating separated functions in a switch case menu in the main.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by