How to convert RGB to hsv?

5 views (last 30 days)
Fredrick  Graham
Fredrick Graham on 16 Jun 2019
Edited: Fredrick Graham on 17 Jun 2019
I have a RGB data with each size like R is 1 x 100 double. how can i convert these RGB to hsv. I find the formula for it from Wikipedia and it available at [here]:
I also found some materials that could answer my questions such as 1 but it is in another language than Matlab.

Answers (3)

KALYAN ACHARJYA
KALYAN ACHARJYA on 16 Jun 2019
Edited: KALYAN ACHARJYA on 16 Jun 2019
For one frame, you can check here, as you mentioned the reference (wiki), I have implmented the same from here
I assumed, you can find R,G,B
Defining hue in terms of RGB
hue_rgb=atan2(sqrt(3)*(G-B),R-G-B)

Walter Roberson
Walter Roberson on 16 Jun 2019
for H = 1:ncols
if T3(H) == 0
hprime(H) = 0;
elseif T1(H) == R(H)
hprime(H) = mod((G(H)-B(H))/C(H), 6);
elseif T1(H) == G(H)
hprime(H) = ((B(H)-R(H))/C(H))+2;
elseif T1(H) == B(H)
hprime(H) = ((R(H)-G(H))/C(H))+4;
else
error('undefined hue at index %d', H);
end
end
  6 Comments
Walter Roberson
Walter Roberson on 16 Jun 2019
What difficulty did you run into with the code that I had already posted to take care of the issue?
T1 = max([R; G; B]);

Sign in to comment.


Image Analyst
Image Analyst on 16 Jun 2019
Why not simply use the built-in rgb2hsv() function:
% load R and G and B data
clear
close all
clc
load('RGB.mat')
subplot(4,1,1)
plot(t,R,'r')
ylim([110, 160]);
xlabel('Time (sec)')
ylabel('Red')
grid on;
subplot(4,1,2)
plot(t,G,'g')
ylim([110, 160]);
xlabel('Time (sec)')
ylabel('Green')
grid on;
subplot(4,1,3)
plot(t,B,'b')
ylim([110, 160]);
xlabel('Time (sec)')
ylabel('Blue')
grid on;
% Convert RGB to HSV:
RGBMatrix = [R; G; B];
% Make MxNx3 array rather than a Mx3 array (where rgb2hsv will think it's a colormap rather than a list of RGB values).
RGBMatrix = repmat(RGBMatrix, [1, 1, 2]);
RGBMatrix = permute(RGBMatrix, [3, 2, 1]);
hsvVec = rgb2hsv(RGBMatrix);
hueVector = hsvVec(1, :, 1);
subplot(4,1,4)
plot(t,hueVector,'b')
xlabel('Time (sec)')
ylabel('Hue')
grid on;

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!