- https://www.mathworks.com/help/matlab/ref/videoreader.html
- https://www.mathworks.com/help/matlab/ref/imwrite.html
- https://www.mathworks.com/help/images/ref/imadjust.html
- https://www.mathworks.com/help/matlab/ref/videowriter.html
video processing
1 view (last 30 days)
Show older comments
I want to split a video into frames ie., jpg/bmp images with a decent frame rate - 100fps, perform contrast enhancement for each frame and rejoin it. How do i go through this procedure? I know im2frame is available, but i don't know how to use it in real time.
0 Comments
Answers (1)
Satwik
on 20 Aug 2024
Hi,
To break down a video into individual frames, enhance the contrast of each frame, and then reassemble the frames into a video at a specified frame rate, you can utilize MATLAB's built-in functions. Here's an example of how you can do it:
1. Read the Video and Extract Frames:
% Create a VideoReader object for the sample video file
videoFile = 'video_file.mp4';
videoReader = VideoReader(videoFile);
% Create a directory to save frames if it doesn't exist
outputDir = 'frames';
if ~exist(outputDir, 'dir')
mkdir(outputDir);
end
frameIdx = 1;
while hasFrame(videoReader)
% Read each frame from the video
frame = readFrame(videoReader);
% Save each frame as an image file
imwrite(frame, fullfile(outputDir, sprintf('frame_%04d.jpg', frameIdx)));
frameIdx = frameIdx + 1;
end
2. Perform Contrast Enhancement
% Get list of all frame files in the directory
frameFiles = dir(fullfile(outputDir, '*.jpg'));
for k = 1:length(frameFiles)
% Read the image from file
framePath = fullfile(outputDir, frameFiles(k).name);
frame = imread(framePath);
% Enhance the contrast of the image using imadjust
enhancedFrame = imadjust(frame, stretchlim(frame), []);
% Save the enhanced image, overwriting the original or in a new directory
imwrite(enhancedFrame, framePath);
end
3. Reassemble the Frames into a Video
% Create a VideoWriter object to write the video
outputVideoFile = 'enhanced_video.mp4';
videoWriter = VideoWriter(outputVideoFile, 'MPEG-4');
videoWriter.FrameRate = 100; % Set the frame rate to 100 fps
open(videoWriter);
% Write each enhanced frame back into the video
for k = 1:length(frameFiles)
% Read the enhanced image from file
framePath = fullfile(outputDir, frameFiles(k).name);
enhancedFrame = imread(framePath);
% Write the frame to the video
writeVideo(videoWriter, enhancedFrame);
end
% Close the video writer object
close(videoWriter);
Here is some documentation for the functions used in the example above, which you may refer to:
I hope this gives you a direction on how you can efficiently process video frames and reassemble them back into a video. Adjust the paths and filenames as necessary for your specific use case.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!