MATLAB: Reshape Video Matrix Into Long RGB Matrix and Back

digital image processingimage processingMATLABvectorizationvideovideo processing

Hello,
Let's say I have a Video Matrix `mVideoMatrix`.
This is a 4D matrix (Numnber of Rows x Number of Columns x Number of Color Channels x Number of Frames).
For instance, 360 x 480 x 3 x 10, namely 360 Rows, 480 Columns, 3 RGB Channels and 10 Frames.
Now I want to make it a long RGB image where each frame is beneath the previous one. Is the a vectorized way to do so?
How would you formulate the way back, namely from the image into the Video?
I'm attaching sample codes which use "For Loop".
At first, reshaping RGB Video into long RGB Image:
function [ mOutputImage ] = ReshapeVideoIntoImage( tInputVideo )
numRows = size(tInputVideo, 1);
numCols = size(tInputVideo, 2);
numChannels = size(tInputVideo, 3);
numFrames = size(tInputVideo, 4);
mOutputImage = zeros([(numFrames * numRows), numCols, numChannels]);
firstRowIdx = 1;
for iFrameIdx = 1:numFrames
vRows = (firstRowIdx - 1) + [1:numRows];
mOutputImage(vRows, :, :) = tInputVideo(:, :, :, iFrameIdx);
firstRowIdx = firstRowIdx + numRows;
end
end
Going back, from long RGB Image into RGB Video:
function [ tOutputVideo ] = ReshapeImageIntoVideo( mInputtImage, videoNumRows )
numRows = size(mInputtImage, 1);
numCols = size(mInputtImage, 2);
numChannels = size(mInputtImage, 3);
videoNumFrames = numRows / videoNumRows;
tOutputVideo = zeros([videoNumRows, numCols, numChannels, videoNumFrames]);
firstRowIdx = 1;
for iFrameIdx = 1:videoNumFrames
vRows = (firstRowIdx - 1) + [1:videoNumRows];
tOutputVideo(:, :, :, iFrameIdx) = mInputtImage(vRows, :, :);
firstRowIdx = firstRowIdx + videoNumRows;
end
end
Thank You.

Best Answer

I think I have a solution (At least one).
going from video into one lone image:
function [ mOutputImage ] = ReshapeVideoIntoImage( tInputVideo )
numRows = size(tInputVideo, 1);
numCols = size(tInputVideo, 2);
numChannels = size(tInputVideo, 3);
numFrames = size(tInputVideo, 4);
mOutputImage = permute(tInputVideo, [1, 4, 2, 3]);
mOutputImage = reshape(mOutputImage, [(numRows * numFrames), numCols, numChannels]);
end
Going from Long Image into Video:
function [ tOutputVideo ] = ReshapeImageIntoVideo( mInputtImage, videoNumRows )
numRows = size(mInputtImage, 1);
numCols = size(mInputtImage, 2);
numChannels = size(mInputtImage, 3);
videoNumFrames = numRows / videoNumRows;
tOutputVideo = reshape(mInputtImage, [videoNumRows, videoNumFrames, numCols, numChannels]);
tOutputVideo = permute(tOutputVideo, [1, 3, 4, 2]);
end
Thank You.
P.S.
If there is an even faster way, I'd be happy to see.