How can I crop several regions from a video and plot all the signals of rgb values in the same graph?

2 vues (au cours des 30 derniers jours)
I have selected 3 regions from the first frame .Now I want to collect the rgb values for each region for the whole video and plot 3 different signals in a graph. However I am unable to do that.Here is the code that Ihave tried.
vid= VideoReader('respiration.mp4');
Fs= 6;
i=1;
j=0;
nof= vid.NumberOfFrames;
for k= 1:nof
img=read(vid,k);
if k==1
for c=1:3
[I,rect] = imcrop(img);
end
else
I = imcrop(img,rect) ;
end
r= I(:,:,1);
meanR(i)= mean(mean(r));
time(i)=j;
i=i+1;
j=j+(1/Fs);
end
figure(2)
plot(time,meanR)
grid on;
xlabel('time (seconds)');
ylabel('value of Red channel');

Réponse acceptée

Ameer Hamza
Ameer Hamza le 25 Sep 2020
Check this code
vid = VideoReader('respiration.mp4');
I = cell(1, 3);
rects = zeros(3, 4);
numFrames = vid.NumFrames; % good initial guess for pre-allocation
meanR = zeros(numFrames, 3); % The columns contain value for corresponding rectangles
time = zeros(numFrames, 1);
count = 1;
while hasFrame(vid)
img = readFrame(vid);
if count == 1
for c = 1:3
[I{c}, rects(c, :)] = imcrop(img);
end
else
for c = 1:3
I{c} = imcrop(img, rects(c, :));
end
end
for c = 1:3
meanR(count, c)= mean(I{c}(:,:,1), 'all');
end
time(count) = vid.CurrentTime;
count = count + 1;
end
  4 commentaires

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by