Conversion between R G B and H S V
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?
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
John Marcin
on 8 Oct 2014
Image Analyst
on 8 Oct 2014
Yeah, I agree -- do you not have the Image Processing Toolbox, John? Or have some ridiculous professor requirement to not use any built-in functions?
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
on 8 Oct 2014
Image Analyst
on 8 Oct 2014
0 votes
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
John Marcin
on 8 Oct 2014
Image Analyst
on 8 Oct 2014
What input RGB values are you converting? What HSV values do you expect to get out and what are you actually getting out?
John Marcin
on 8 Oct 2014
José-Luis
on 8 Oct 2014
You are only answering part of the question. What are you feeding your function and what are your results? How do they differ from what you expect?
Image Analyst
on 8 Oct 2014
Yes. Give an actual value for RGB like (128,128,128) which is gray, and show that you're getting (0.1, 0.5, 50) which is pink instead of (0,0,50) which would be mid-gray. I need to know how/why you're believing that your conversion has a reddish tint that should not be there.
Image Analyst
on 9 Oct 2014
Well like that link discusses, it's basically nonsense to display an HSV image as RGB, with the hue image going into the red channel, the saturation image going into the green channel, and the value image going into the blue channel. I wouldn't say that it always causes a distinct red shift - it could cause any kind of weird alteration of colors (green cast, blue tint, psychadelic colors, etc.) because it's just not appropriate at all to do that. Same thing for other colorspaces - YCbCr, LAB, YUV, etc. It just doesn't make sense. It would be kind of like taking a Russian sentence in Cyrillic characters and translating it into ASCII Western/Latin letters and displaying it and wondering why they don't look the same.
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).
Categories
Find more on Color 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!