if文の書き方

22 visualizzazioni (ultimi 30 giorni)
hiro
hiro il 20 Nov 2020
Commentato: hiro il 28 Nov 2020
vid0bj = VideoReader('image1-1.avi');
vid0bj.NumFrames
workingDir = tempname;
mkdir(workingDir)
mkdir(workingDir, 'images')
ii = 1;
while hasFrame(vid0bj)
img = readFrame(vid0bj);
filename = [sprintf('%03d', ii) '.jpg'];
fullname = fullfile(workingDir, 'images', filename);
imwrite(img, fullname)
ii = ii+1;
end
imageNames = dir(fullfile(workingDir, 'images', '*.jpg'));
imageNames = {imageNames.name}';
outputVideo = VideoWriter(fullfile(workingDir, 'image1-1kai.avi'));
outputVideo.FrameRate = vid0bj.FrameRate;
open(outputVideo)
for ii = 1:length(imageNames)
img = imread(fullfile(workingDir, 'images', imageNames{ii}));
writeVideo(outputVideo,img)
end
close(outputVideo)
vid0bjavi = VideoReader(fullfile(workingDir, 'image1-1kai.avi'));
ii = 1;
while hasFrame(vid0bjavi)
mov(ii) = im2frame(readFrame(vid0bjavi));
ii = ii+1;
end
figure
imshow(mov(1).cdata, 'Border', 'tight')
movie(mov,1,vid0bjavi.FrameRate)
上記のようなコードを組むと動画ファイルから画像ファイルを作り、画像ファイルから動画ファイルを再び作ることができました。
このimage1-1.aviという動画ファイルのフレーム数は122で、これをだいたい30くらいのフレーム数の動画として新しく作りたいと考えています。画像ファイルを読み込むときに等間隔に4フレームに1回読み込んで30フレームの動画を作りたいのですがどのようにコードを書き換えればよろしいでしょうか?
for ii = 1:length(imageNames)
img = imread(fullfile(workingDir, 'images', imageNames{ii}));
writeVideo(outputVideo,img)
end
自分の考えでは上記の部分をif文を使って条件分岐して作ればいいと考えているのですがその書き方がわかりません。そこも教えていただけると助かります。

Risposta accettata

Hiroshi Miyagawa
Hiroshi Miyagawa il 25 Nov 2020
力技ですが、以下のような方法があるかと。
NumFrame = length(imageNames);
SampleRate = round(NumFrame/30);
jj = 0;
for ii = 1:NumFrame
if mod(jj,SampleRate) == 0
img = imread(fullfile(workingDir, 'images', imageNames{ii}));
writeVideo(outputVideo,img);
jj = 0;
end
jj = jj +1;
end
  1 Commento
hiro
hiro il 28 Nov 2020
ありがとうございました。 うまく作ることができました。

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!