how to make transformation in mp4 vedio

9 views (last 30 days)
Muhammad
Muhammad on 17 Feb 2023
Edited: Hitesh on 3 Apr 2025 at 4:21
i have two vedio of mp4.i want to apply some tranformation on it and run the tranform vedio.i have wriiten the code but contently facing the error.i even tried "NumberOfFrames" but it is unrecongnized in my matlab
code is here
% read videos
vid1 = VideoReader('video1.mp4');
vid2 = VideoReader('vedio2.mp4');
% read first frame from each video
frame1 = readFrame(vid1);
frame2 = readFrame(vid2);
% create a figure
fig = figure;
while hasFrame(vid1) && hasFrame(vid2)
% read frames from each video
frame1 = readFrame(vid1);
frame2 = readFrame(vid2);
% perform geometric transformations
frame1_scaled = imresize(frame1, 0.5);
frame1_sheared = imresize(frame1, [size(frame1,1) size(frame1,2)*0.8]);
frame1_rotated = imrotate(frame1, 30, 'crop');
frame2_scaled = imresize(frame2, 0.5);
frame2_sheared = imresize(frame2, [size(frame2,1) size(frame2,2)*0.8]);
frame2_rotated = imrotate(frame2, -30, 'crop');
% concatenate frames
combined_frame = cat(2, cat(1, frame1, frame1_scaled, frame1_sheared, frame1_rotated), ...
cat(1, frame2, frame2_scaled, frame2_sheared, frame2_rotated));
% display frames
imshow(combined_frame, 'Parent', fig);
drawnow;
end
error that i facing is
Error using cat
Dimensions
of
arrays
being
concatenated
are
not
consistent.
Error in untitled (line 27)
combined_frame = cat(2, cat(1, frame1, frame1_scaled, frame1_sheared, frame1_rotated), ...

Answers (1)

Hitesh
Hitesh on 2 Apr 2025 at 12:23
Edited: Hitesh on 3 Apr 2025 at 4:21
Hi Muhammad,
The error indicates that frames concatenated are not having consistent dimensions which leads to error. Kindly ensure that all frames have the same dimensions before concatenation. Follow the below steps to ensure the consistent dimension across all frames
  • Padding: Each frame is padded to ensure consistent dimensions across all frames being concatenated.
  • Dynamic Sizing: The maximum height and width are calculated dynamically to accommodate the largest transformation result.
The "fig" variable is also used incorrectly as an axes handle. In MATLAB, "imshow" requires a valid axes handle, not a figure handle. You need to create an axes object within the figure and then use that axes handle with "imshow".
Kindly refer to the revised code and mentioned comments for better understanding:
% Read videos
vid1 = VideoReader('video1.mp4');
vid2 = VideoReader('video2.mp4');
% Create a figure
fig = figure;
ax = axes('Parent', fig);
while hasFrame(vid1) && hasFrame(vid2)
% Check if the figure is still open
if ~isvalid(fig)
break; % Exit the loop if the figure is closed
end
% Read frames from each video
frame1 = readFrame(vid1);
frame2 = readFrame(vid2);
% Perform geometric transformations
frame1_scaled = imresize(frame1, 0.5);
frame1_sheared = imresize(frame1, [size(frame1,1) size(frame1,2)*0.8]);
frame1_rotated = imrotate(frame1, 30, 'crop');
frame2_scaled = imresize(frame2, 0.5);
frame2_sheared = imresize(frame2, [size(frame2,1) size(frame2,2)*0.8]);
frame2_rotated = imrotate(frame2, -30, 'crop');
% Find the maximum height and width for consistent dimensions
max_height = max([size(frame1, 1), size(frame1_scaled, 1), size(frame1_sheared, 1), size(frame1_rotated, 1), ...
size(frame2, 1), size(frame2_scaled, 1), size(frame2_sheared, 1), size(frame2_rotated, 1)]);
max_width = max([size(frame1, 2), size(frame1_scaled, 2), size(frame1_sheared, 2), size(frame1_rotated, 2), ...
size(frame2, 2), size(frame2_scaled, 2), size(frame2_sheared, 2), size(frame2_rotated, 2)]);
% Pad frames to make them the same size
frame1 = padarray(frame1, [max_height - size(frame1, 1), max_width - size(frame1, 2)], 'post');
frame1_scaled = padarray(frame1_scaled, [max_height - size(frame1_scaled, 1), max_width - size(frame1_scaled, 2)], 'post');
frame1_sheared = padarray(frame1_sheared, [max_height - size(frame1_sheared, 1), max_width - size(frame1_sheared, 2)], 'post');
frame1_rotated = padarray(frame1_rotated, [max_height - size(frame1_rotated, 1), max_width - size(frame1_rotated, 2)], 'post');
frame2 = padarray(frame2, [max_height - size(frame2, 1), max_width - size(frame2, 2)], 'post');
frame2_scaled = padarray(frame2_scaled, [max_height - size(frame2_scaled, 1), max_width - size(frame2_scaled, 2)], 'post');
frame2_sheared = padarray(frame2_sheared, [max_height - size(frame2_sheared, 1), max_width - size(frame2_sheared, 2)], 'post');
frame2_rotated = padarray(frame2_rotated, [max_height - size(frame2_rotated, 1), max_width - size(frame2_rotated, 2)], 'post');
% Concatenate frames
combined_frame = cat(2, cat(1, frame1, frame1_scaled, frame1_sheared, frame1_rotated), ...
cat(1, frame2, frame2_scaled, frame2_sheared, frame2_rotated));
% Display frames
imshow(combined_frame, 'Parent', ax);
drawnow;
end

Categories

Find more on Axes Appearance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!