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
2014년 10월 8일
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
2014년 10월 8일
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?
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
2014년 10월 8일
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
John Marcin
2014년 10월 8일
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
2014년 10월 8일
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
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.
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.
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
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).
카테고리
도움말 센터 및 File Exchange에서 Color에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!