How to receive rgb values for each frame of the video?
30 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2021-5-31
@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.
采纳的回答
harjeet singh
2016-1-16
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 个评论
Image Analyst
2017-3-11
编辑:Image Analyst
2017-3-11
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
2019-6-18
更多回答(1 个)
Image Analyst
2016-1-16
My attached demo does exactly that (plus a little more). See the script underneath this figure that it makes:
2 个评论
Image Analyst
2016-1-17
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!