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!