I am trying to extract RGB values from video and convert to HSV

4 views (last 30 days)
So I have a sample video of which I wish to extract the RGB values and then convert them to HSV and plot that. I have extracted the RGB values from the video using the following:
%Reading the video file
video=VideoReader('sample.mp4');
vidHeight=video.Height;
vidWidth=video.Width;
k=1;
while hasFrame(video)
img = readFrame(video);
% [H S V] = rgb2hsv(img);
r(:,:,k)=img(:,:,1);
g(:,:,k)=img(:,:,2);
b(:,:,k)=img(:,:,3);
k = k+1;
end
r_mean=mean(r(:))
g_mean=mean(g(:))
b_mean=mean(b(:))
for val = 1:k-1
mean_blue = mean(b(:,:,val));
mean_red = mean(r(:,:,val));
mean_green = mean(g(:,:,val));
mean_all = (mean_blue+mean_red+mean_green)/3;
end
Now I wish to convert the matrix mean_all (which contains the mean of each frame in RGB) to HSV values. I tried using the rgb2hsv function in the for-loop while extracting the RGB values but I don't think that is correct. I am new to MATLAB, so please excuse me if this question sounds too basic. I have looked online for a solution and it may have missed me. Any help would be appreciated! :)
  1 Comment
Abel Babu
Abel Babu on 21 Feb 2017
'rgb2hsv' in the for loop should do the job. Hence, can you tell me why do you think that it is not producing the correct results?.
Also attaching the documentation for 'rgb2hsv' : http://in.mathworks.com/help/matlab/ref/rgb2hsv.html

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 21 Feb 2017
Your input image is almost certainly integer data type. You do not initialize r, g, b before the loop, so they are going to get the integer data type from img. However, mean() is going to give you a floating point data type. If you do not take that into account then when you go to view the image, the image would probably end up looking almost all white.
Next, you have
for val = 1:k-1
mean_blue = mean(b(:,:,val));
mean_red = mean(r(:,:,val));
mean_green = mean(g(:,:,val));
mean_all = (mean_blue+mean_red+mean_green)/3;
end
that code is overwriting all of those variables every time through the loop. You should consider using
mean_all(val,:) = (mean_blue+mean_red+mean_green)/3;
It is not immediately clear, though, why you want to create a per-column mean (leaving you with a row vector for each frame.)

Categories

Find more on Images 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!