How to receive rgb values for each frame of the video?

30 views (last 30 days)
Hello, everyone. I am having trouble with getting RGB values for all frames of my video. What I need to do is to find what average value of red (of all pixels) each of my frames has. I've tried to use impixel, but it makes me specify the width&length, which have to be the same, but in my case, my video is 1040X1400 size.. I appreciate any advice! Thank you My code is:
%Reading the video file
video=VideoReader('video.mov');
vidHeight=video.Height;
vidWidth=video.Width;
k=1;
while hasFrame(video)
allframes(k).cdata = readFrame(video);
k = k+1;
end
i=1;
while hasFrame(video)
frames(i,:)=impixel(allframes(i).cdata,1:vidHeight-1,1:vidHeight-1);
i=i+1;
end
  2 Comments
Image Analyst
Image Analyst on 31 May 2021
@Jasleen kaur, this code is the code with problems and is why a question is being asked about it. You should run the code in the Answer, not the question.

Sign in to comment.

Accepted Answer

harjeet singh
harjeet singh on 16 Jan 2016
try this code
video=VideoReader('video.mov');
vidHeight=video.Height;
vidWidth=video.Width;
k=1;
while hasFrame(video)
img = readFrame(video);
r(:,:,k)=img(:,:,1);
g(:,:,k)=img(:,:,2);
b(:,:,k)=img(:,:,3);
k = k+1;
end
r_mean=mean(r(:))
c_mean=mean(c(:))
b_mean=mean(b(:))
  6 Comments
Image Analyst
Image Analyst on 11 Mar 2017
Edited: Image Analyst on 11 Mar 2017
Something like:
% Determine how many frames there are.
numberOfFrames = videoObject.NumberOfFrames;
r = zeros(vidHeight, vidWidth, numberOfFrames, 'uint8');
g = zeros(vidHeight, vidWidth, numberOfFrames, 'uint8');
b = zeros(vidHeight, vidWidth, numberOfFrames, 'uint8');
There is no reason to store all the frames like that to simply get the mean. You could simply use mean2() to get the mean of each frame (like I do in my answer), then take the mean of all those means. It would use millions of times less memory. To fix this code:
while hasFrame(video)
img = readFrame(video);
r(k)= mean2(img(:,:,1));
g(k)= mean2(img(:,:,2));
b(k)= mean2(img(:,:,3));
k = k+1;
end
r_mean=mean(r)
c_mean=mean(g)
b_mean=mean(b)
Gordafrin
Gordafrin on 18 Jun 2019
wld u plz let me know which library you used for the above code @harjeet singh ?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Jan 2016
My attached demo does exactly that (plus a little more). See the script underneath this figure that it makes:
  2 Comments
Image Analyst
Image Analyst on 17 Jan 2016
You're welcome. This code is much more memory efficient that the code you accepted. There is no need to store all the red, green, and blue images in the loop and then take the average after the loop. In fact, for a large video you will run out of memory. Note how my
% Calculate the mean R, G, and B levels.
meanRedLevels(frame) = mean(mean(thisFrame(:, :, 1)));
meanGreenLevels(frame) = mean(mean(thisFrame(:, :, 2)));
meanBlueLevels(frame) = mean(mean(thisFrame(:, :, 3)));
Takes the mean inside the loop and does not store the frame. It uses literally millions of bytes less memory. I also preallocate the super-tiny arrays that I do use but my code uses such a microscopic amount of memory that it's hardly necessary, though it's good practice. One improvement though is that you could use mean2() instead of mean(mean()) but it requires that you have the Image Processing Toolbox, but most people have that. It might speed it up a few microseconds.
I'd recommend you use my code if you want to be more memory efficient and not run out of memory.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!