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

1 回表示 (過去 30 日間)
Miquel Montes Perez
Miquel Montes Perez 2022 年 5 月 18 日
編集済み: Aishwarya 2023 年 10 月 13 日
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