Reshape Video Matrix Into Long RGB Matrix and Back

조회 수: 1 (최근 30일)
Royi Avital
Royi Avital 2015년 6월 29일
답변: Royi Avital 2015년 6월 30일
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.

채택된 답변

Royi Avital
Royi Avital 2015년 6월 30일
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.

추가 답변 (2개)

Sid
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?
Also, for more info, the article here about multidimensional arrays might prove useful. Good luck!
HTH.
  댓글 수: 1
Royi Avital
Royi Avital 2015년 6월 29일
Hi Sid,
I already have the video in 4-D Array.
As I specified above, the 4-D array is as following: (Num Rows x Num Columns x 3 (RGB) x Num Frames).
I want to create a 3-D matrix which its dimensions are ((NumRowx * Num Num Frame) x Num Columns x Num Channel (3, RGB)).
Namely to put frame below frame and then going back.
I added a sample code.
Thank You.

댓글을 달려면 로그인하십시오.


Image Analyst
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
  댓글 수: 1
Royi Avital
Royi Avital 2015년 6월 30일
Hi, You can see my code above does the same.
I thought there would be a faster way, a vectorized way (I think it can be done using permute).

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by