Reshape Video Matrix Into Long RGB Matrix and Back
显示 更早的评论
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.
采纳的回答
更多回答(2 个)
Sid
2015-6-29
Can you use something like this?
% Concatenate along 'fourth' dimension.
alpha = cat(4,newArray(:,:,:));
% Extract every third color plane, starting with 1.
red = alpha(:,:,1:3:size(alpha,3));
red = vertcat(red(:,:));
% Extract every third color plane, starting with 2.
green = alpha(:,:,2:3:size(alpha,3));
green = vertcat(green(:,:));
% Extract every third color plane, starting with 3.
blue = alpha(:,:,3:3:size(alpha,3));
blue = vertcat(blue(:,:));
RGB = cat(3,red,green,blue);
figure, imshow(RGB)
where newArray refers to the original 4-D array.
Perhaps I'm missing something, but I'm not sure I completely follow the logic of concatenating images?
HTH.
Image Analyst
2015-6-29
To stitch the frames together vertically, do this:
numFrames = size(myVideo, 4);
stitchedImage = [];
for k = 1 : numFrames
stitchedImage = [stitchedImage; myVideo(:,:,:,k)];
end
类别
在 帮助中心 和 File Exchange 中查找有关 Video Formats and Interfaces 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!