How to speed up frame-by-frame image processing for writing a video?

29 views (last 30 days)
Hello,
I have a large video (avi) that I want to read, process each frame, crop, and save a processed video. Here is my example script, which takes extraordinally long time to run (~35 seconds/100 frames). Is there more efficient way to read, process, and write frames to a video?
% Read video
fname = 'vid.avi';
reader=VideoReader(fname);
n_frames = reader.NumFrames;
crop_position = [1000, 300, 149, 99];
% Write video
writer = VideoWriter('cropped_vid', 'MPEG-4');
writer.Quality = 95;
writer.FrameRate = reader.FrameRate;
% process frames
for i=1:n_frames
img = read(reader,i);
img = rgb2lab(img);
img = imcomplement(imreducehaze(imcomplement(img(:,:,1) ./ 100),'ContrastEnhancement','boost'));
img = medfilt2(imcrop(img,crop_mask),[5,5]);
img = imcomplement(imreducehaze(imcomplement(img)));
writeVideo(writer,img);
end
close(writer);
toc
Thanks!
  5 Comments
Walter Roberson
Walter Roberson on 9 Feb 2022
User already said, "the most of the time is in the code is spent in rgb2lab conversion"

Sign in to comment.

Answers (1)

yanqi liu
yanqi liu on 8 Feb 2022
yes,sir,may be split image process and video write,such as process image to files,and then read files and write to video,so it will avoid video write flush step

Community Treasure Hunt

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

Start Hunting!