Undefined function or variable

3 views (last 30 days)
Josephine
Josephine on 7 Dec 2013
Answered: Wayne King on 7 Dec 2013
hi i am trying to make my own rgb-to-hsv function. this is my code.
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
hue_one = mod((((G-B).*60)./(max(I,[],3)-min(I,[],3))),360);
hue_two = (((B - R).*60)./(max(I,[],3)-min(I,[],3))) + 120;
hue_three = (((R - G).*60)./(max(I,[],3)-min(I,[],3))) + 240;
if (R == G) & (R == B)
H = 0;
elseif (R >= G) & (R >= B)
H = hue_one;
elseif (G >= R) & (G >= B)
H = hue_two;
elseif (B >= R) & (B >= G)
H = hue_three;
end
V = max(I,[],3);
if V == 0
S = 0;
else
S = (max(I,[],3)-min(I,[],3))./max(I,[],3);
end
HSV(:,:,1) = H;
HSV(:,:,2) = S;
HSV(:,:,3) = V;
imshow(HSV)
values of S and V are loaded in the workspace without errors while the H returns an error.
Undefined function or variable 'H'.
Error in convertRGB (line 29)
HSV(:,:,1) = H;
i tried running my code line by line. after the if/elseif statements, there was no generated value of H. the variables hue_one, hue_two and hue_three also returned a matrix and can be found on the workspace pane. WHAT COULD HAVE BEEN THE PROBLEM OF THIS? THANK YOU.

Answers (2)

Kan-Hua
Kan-Hua on 7 Dec 2013
I would suggest you add a "else" statement in your first "if" block. It may be that your RGB components of the image you loaded does not match any of the if-condiations in your block, and thus your H is not assigned to any value.
For example:
if (R == G) & (R == B)
H = 0;
elseif (R >= G) & (R >= B)
H = hue_one;
elseif (G >= R) & (G >= B)
H = hue_two;
elseif (B >= R) & (B >= G)
H = hue_three;
else
H=0; %or whatever
end
Hope this helps. :)

Wayne King
Wayne King on 7 Dec 2013
What do you expect these comparisons to yield:
if (R == G) & (R == B)
H = 0;
elseif (R >= G) & (R >= B)
H = hue_one;
elseif (G >= R) & (G >= B)
H = hue_two;
elseif (B >= R) & (B >= G)
H = hue_three;
end
If these are false for any element of these matrices (very, very likely), then H is not assigned anything.

Community Treasure Hunt

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

Start Hunting!