How can I save the frame on one loop to subtract it to another frame in the next loop?

1 次查看(过去 30 天)
Okay so i want to subtract one frame from a video from the one who came before, so if the value is high enough i can tell that there's movement, the problem is that i have to use this base model, the reason why the image is binarized is so its easier to detect movement:
function hello(nameIn,nameOut)
vin = VideoReader(nameIn);
vout = VideoWriter(nameOut,'MPEG-4');
open(vout);
while hasFrame(vin)
frameIn = readFrame(vin);
imagesc(imdilate(frameIn,ones(5))-imerode(frameIn,ones(5)));axis equal
frameOut = (imdilate(frameIn,ones(5))-imerode(frameIn,ones(5)));
%ideally everything would be written here
writeVideo(vout,frameOut);
end
close(vout);
my idea was something similar to this:
function hello(nameIn,nameOut)
vin = VideoReader(nameIn);
vout = VideoWriter(nameOut,'MPEG-4');
open(vout);
while hasFrame(vin)
frameIn = readFrame(vin);
diference = frameIn - save;
if (diference < 100)
%printf the second this happens
end
frameOut = (imdilate(frameIn,ones(5))-imerode(frameIn,ones(5)));
save = frameOut;
writeVideo(vout,frameOut);
end
close(vout);
and as i expected, it goes horribly wrong, i dont really know how to solve this problem, if someone can help i would much appreciate it, thanks and sorry if this is a mess.

回答(1 个)

Aishwarya
Aishwarya 2023-10-13
编辑:Aishwarya 2023-10-13
Hi Miquel,
After reviewing the code provided, I think the problem is in how the difference is calculated. In the code, this statement could be the reason for ambiguity:
diference = frameIn save
diference” variable is same dimension as “frameIn” image matrix and is not a numerical value. To address this, I suggest incorporating the following line instead:
diference = mean(frameIn save)
By doing so, you will obtain the mean value of the pixel difference as required.
Regards,
Aishwarya

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by