Conversion between R G B and H S V

9 views (last 30 days)
John Marcin
John Marcin on 8 Oct 2014
Commented: Image Analyst on 10 Oct 2014
I have been tryin to implement the rgb2hsv function as a Matlab function. This is part of a bigger assignment which is due for next year.
I have managed to come up with this code:
function [output]=rgb2hsv(A);
[m,n,t]= size(A); output = zeros(m,n,t); for i=1:m for j=1:n R = A(i,j,1) / 255; G = A(i,j,2) / 255; B = A(i,j,3) / 255; massimo = max([R,G,B]); minimo = min([R,G,B]); V = massimo; d = massimo - minimo;
if (massimo == 0)
S = 0;
else
S = d / massimo;
end
if (massimo == minimo)
H = 0;
else
switch(massimo)
case R
if (G < B)
tmp = 6;
else
tmp = 0;
end
H = (G - B) / d + tmp;
case G
H = (B - R) / d + 2;
case B
H = (R - G) / d + 4;
end
H = H / 6;
end
output(i,j,1) = H;
output(i,j,2) = S;
output(i,j,3) = V;
end
end
end
What isn't working here pls?

Answers (2)

José-Luis
José-Luis on 8 Oct 2014
I don't get it. rgb2hsv() is a built-in Matlab function.
If you want to see how it is implemented, type
edit rgb2hsv
in the command line.
  4 Comments
José-Luis
José-Luis on 8 Oct 2014
Edited: José-Luis on 8 Oct 2014
Well then, it seems like your professor meant: come up with an algorithm yourself, based on the formulas. For that, the link that Image Analyst gave you works.
It's not really that complicated and there are not going to be a million ways to do this, unless you're big into obfuscation.
John Marcin
John Marcin on 8 Oct 2014
changed the formula to work correctly: But the image conversion result is really pink. When dividing H/ 360 it gets more blue.. What possibly could i be doing wrong here?
M = max([R,G,B]);
m = min([R,G,B]);
V = M;
C = M-m;
if (V == 0)
S = 0;
else
S = C / V;
end
if (C==0)
H=0;
elseif (M==R)
H = 60 * mod(((G-B)/C),6);
elseif (M==G)
H = 60 * (((B-R)/C) + 2);
elseif (M==B)
H = 60 * (((R-G)/C) +4);
end

Sign in to comment.


Image Analyst
Image Analyst on 8 Oct 2014
It doesn't look like the right formulas. See the formulas here: http://www.easyrgb.com/index.php?X=MATH&H=20#text20
  9 Comments
Stephen23
Stephen23 on 10 Oct 2014
Edited: Stephen23 on 10 Oct 2014
Of course it is nonsense to use an RGB image displaying function to display an HSV encoded image. I just wanted to point out that it does happen. And then they try to interpret the results...
Given that the OP has not responded to the requests for information about the process they are using, this is one possibility for the OP's "[the] result is really pink" comment.
Image Analyst
Image Analyst on 10 Oct 2014
I absolutely agree, and know you didn't think that. I just wanted to further explain and drive the point home, because some people do, perhaps due to programs like Adobe Photoshop. With Photoshop, if you convert your image from RGB to LAB it doesn't look any different. If you go to the individual channels, L, A, or B you will see them, but when you click on LAB (all 3 channels), it looks identical to the RGB image, which can be deceptive if you don't know what it's doing behind the scenes (which is to convert the LAB back to an RGB image).

Sign in to comment.

Categories

Find more on Modify Image Colors 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!