Conversion between R G B and H S V
9 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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
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.
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
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
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).
See Also
Categories
Find more on Modify Image Colors in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!