i want to do background subtraction and then save edited frames in a video but I cant go through the last part!
1 次查看(过去 30 天)
显示 更早的评论
clear all;
close all;
%warning('OFF','all')
clc;
thresh = 0.05;%originalmente era 40 pero a mi me funciona con este valor 0.005 aproximadamente
c=0;
filename = 'YourAVI4.avi';
hvfr = vision.VideoFileReader(filename, 'ImageColorSpace', 'RGB');
v = VideoWriter('matin');
image = step(hvfr);
% ----------------------- frame size variables -----------------------
bg = image; % read in 1st frame as background frame
open(v)
bg_bw = rgb2gray(bg); % convert background to greyscale
% ----------------------- set frame size variables -----------------------
fr_size = size(bg);
width = fr_size(2);
height = fr_size(1);
fg = zeros(height, width);
while ~isDone(hvfr)
fr = step(hvfr);
c = c+1;
end
% --------------------- process frames -----------------------------------
while ~isDone(hvfr)
fr = step(hvfr);
fr_bw = rgb2gray(fr); % convert frame to grayscale
fr_diff = abs(double(fr_bw) - double(bg_bw));
for j=1:width
for k=1:height
if ((fr_diff(k,j) > thresh))
fg(k,j) = 255;
else
fg(k,j) = 0;
end
end
end
bg_bw = fr_bw; %to consider next two frames as candidates
writeVideo(v,fg)
%figure(1),subplot(3,1,1),imshow(fr)
%subplot(3,1,2),imshow(fr_bw)
%subplot(3,1,3),imshow(uint8(fg))
end
close(v)
2 个评论
Walter Roberson
2020-3-15
writeVideo(v,fg)
fg will be double precision. What range of values can it be? What range of values does writeVideo permit for double precision data?
回答(1 个)
Walter Roberson
2020-3-15
v = VideoWriter('matin');
No file extension and no profile. What will happen in that case?
If you do not specify a valid file extension, VideoWriter appends the extension .avi, .mj2, or .mp4, depending on the value of the profile argument. If you do not specify a value for profile, then VideoWriter creates a Motion JPEG compressed AVI file with the extension .avi.
Okay so you are getting an .avi file. What are the limits for avi?
When creating AVI or MPEG-4 files:
- img is an array of single, double, or uint8 values representing one or more grayscale or RGB color images, which writeVideo writes as one or more RGB video frames.
- Data of type single or double must be in the range [0,1], except when writing indexed AVI files.
Your values are double() so they must be in the range 0 to 1. But are they?
if ((fr_diff(k,j) > thresh))
fg(k,j) = 255;
else
fg(k,j) = 0;
end
No, they are 0 and 255.
Perhaps you should initialize fg to be uint8.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!