Conversion between R G B and H S V

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?

답변 (2개)

José-Luis
José-Luis 2014년 10월 8일

0 개 추천

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

John Marcin
John Marcin 2014년 10월 8일
we are mean to find another implementation apart from this code and then compare with this built in function
Image Analyst
Image Analyst 2014년 10월 8일
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?
José-Luis
José-Luis 2014년 10월 8일
편집: José-Luis 2014년 10월 8일
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.
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

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2014년 10월 8일

0 개 추천

It doesn't look like the right formulas. See the formulas here: http://www.easyrgb.com/index.php?X=MATH&H=20#text20

댓글 수: 9

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
Image Analyst
Image Analyst 2014년 10월 8일
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
John Marcin 2014년 10월 8일
I'm expect the input to be a real-valued array with each channel represented in the range 0.0 to 1.0, each dimension of the output should be a real-value in the range 0 to 1 and the third dimension of the output matrix should consist of the Hue, Saturation and Value in that order.
José-Luis
José-Luis 2014년 10월 8일
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
Image Analyst 2014년 10월 8일
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.
Stephen23
Stephen23 2014년 10월 9일
Converting an RGB image to HSV and displaying this using imshow does result in distinct red-shift... Some users expect imshow to magically work for whatever colorspace that they happen to be using:
Image Analyst
Image Analyst 2014년 10월 9일
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.
Stephen23
Stephen23 2014년 10월 10일
편집: Stephen23 2014년 10월 10일
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 2014년 10월 10일
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).

댓글을 달려면 로그인하십시오.

질문:

2014년 10월 8일

댓글:

2014년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by