Effacer les filtres
Effacer les filtres

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

38 vues (au cours des 30 derniers jours)
Anar Alshanbayeva
Anar Alshanbayeva le 16 Jan 2016
Commenté : Image Analyst le 31 Mai 2021
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 commentaires
Jasleen kaur
Jasleen kaur le 31 Mai 2021
This code is not giving any result.
Image Analyst
Image Analyst le 31 Mai 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.

Connectez-vous pour commenter.

Réponse acceptée

harjeet singh
harjeet singh le 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 commentaires
Image Analyst
Image Analyst le 11 Mar 2017
Modifié(e) : Image Analyst le 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 le 18 Juin 2019
wld u plz let me know which library you used for the above code @harjeet singh ?

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 16 Jan 2016
My attached demo does exactly that (plus a little more). See the script underneath this figure that it makes:
  2 commentaires
Anar Alshanbayeva
Anar Alshanbayeva le 17 Jan 2016
Thank you, this is very helpful.
Image Analyst
Image Analyst le 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by