我正在尝试从视频中读取前20帧,将这些图像与背景图像一起保存在文件夹中,这是我通过拍摄这20幅图像的中值得到的。然后我试图从视频中的每个帧中减去此背景图像,并将输出写入新视频。但最后,我只是在新视频中获得黑色输出并收到以下警告: 警告:没有视频帧写入此文件。该文件可能无效。
在VideoWriter.VideoWriter> VideoWriter.close在307 在VideoWriter.VideoWriter> VideoWriter.delete中,256 在43的Framextraction中
我该怎么办?
clear all;
clc;
thresh=100;
workingDir = 'D:/DDP/proj3';
mkdir(workingDir)
mkdir(workingDir,'images')
vid= VideoReader('D:/DDP/sample1.3gp');
for k=1:20
img = readFrame(vid);
filename = [sprintf('%01d',k-1) '.jpg'];
imwrite(img,fullfile(workingDir,'images',filename))
end
folder = 'D:\DDP\proj3\images\';
filelist = dir( fullfile(folder,'*.jpg') );
filelist = strcat(folder, filesep, {filelist.name});
images = zeros(720,1280,3, numel(filelist), 'double');
for i=1:numel(filelist)
images(:,:,:,i) = double( imread(filelist{i}) );
end
background = uint8(median(images, 4));
imwrite(background,'D:\DDP\proj3\images\background.jpg','jpg')
fr_size = size(BW1);
width = fr_size(2);
height = fr_size(1);
fg = zeros(height, width);
vid.CurrentTime = 0; % Rewind video reader
VW = VideoWriter('D:/DDP/proj3/new');
open(VW);
while hasFrame(vid)
fr = readFrame(vid);
fr_diff = abs(double(fr) - double(background));
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
writeVideo(VW,double(fg)/255);
end
close(VW)
cc= VideoReader('D:/DDP/proj3/new.avi');
cd= VideoWriter('D:/DDP/proj3/new1');
open(cd);
while hasFrame(cc);
s=readFrame(cc);
J = imnoise(s,'salt & pepper',0.02);
img_filtered = J;
for c = 1 : 3
img_filtered(:, :, c) = medfilt2(J(:, :, c), [3, 3]);
end
writeVideo(cd, double(img_filtered)/255);
end
close(cd);
答案 0 :(得分:0)
在您的代码中,错误来自于您没有将视频阅读器倒回到第一帧的事实。您发布的代码似乎与您对背景图像的数据类型为双重类型的注释不对应。如果你使用imread读取背景图片,它应该是uint8类型。这是我的建议:
workingDir = 'D:/DDP';
mkdir(workingDir)
mkdir(workingDir,'images')
vid= VideoReader('D:/DDP/samplee.3gp');
for k=1:20
img = readFrame(vid);
filename = [sprintf('%01d',kk-1) '.jpg'];
imwrite(img,fullfile(workingDir,'images',filename))
end
folder = 'D:\DDP\images\';
filelist = dir( fullfile(folder,'*.jpg') );
filelist = strcat(folder, filesep, {filelist.name});
images = zeros(720,1280,3, numel(filelist), 'double');
for i=1:numel(filelist)
images(:,:,:,i) = double( imread(filelist{i}) );
end
background = uint8(median(images, 4)); % Convert background image to uint8 type
imwrite(background,'D:\DDP\images\background.jpg','jpg')
vid.CurrentTime = 0; % Rewind video reader
VW = VideoWriter('D:/DDP/new');
open(VW);
while hasFrame(vid)
CDatas(:,:,:) = readFrame(vid) - background;
writeVideo(VW,CDatas);
end
close(VW)