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!