Create a video from a 4D array

조회 수: 15 (최근 30일)
Alber
Alber 2020년 4월 17일
댓글: Ameer Hamza 2020년 4월 18일
Hello, I have a problem in this code.
function [] = writeBufferToFinalVideo(buffer)
video = VideoWriter('example','MPEG-4');
video.FrameRate = 25;
open(video)
for i = 1:size(buffer,4)
img = buffer(i);
img(img>1) = 1;
img(img<0) = 0;
writeVideo(video,img);
end
close(video);
end
My buffer is a 4-dimensional array that has frames inside. It is composed of: (height, width, numChannels, framesPerSymbol). The latter is the number of frames that are in a symbol, that is, the size of my buffer.
In my opinion, I think the code is correct, but I always get an empty video without the frames in my buffer.
  댓글 수: 2
Ameer Hamza
Ameer Hamza 2020년 4월 17일
What is the class if buffer?
class(buffer)
Alber
Alber 2020년 4월 17일
편집: Alber 2020년 4월 17일
I made the buffer with an array buffer = zeros(height, width, numChannels, framesPerSymbol) and with other function I put the frames of the original video input the buffer.

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 17일
If buffer is a 4D-array, then you will need to index it like this
img = buffer(:,:,:,i);
  댓글 수: 9
Alber
Alber 2020년 4월 18일
Yes!!! This is the solution, Thank you!!
Ameer Hamza
Ameer Hamza 2020년 4월 18일
I am glad to be of help.
Reason: if the datatype of the matrix is double, MATLAB considers the RGB color values between 0 and 1. Whereas, for your matrix, the values were given in an 8-bit format from 0 to 255. MATLAB takes all value over one as white (that is why you saw white video frames) Dividing by 255, normalizes the value between 0 and 1. I guess, the following will also work for your original code
writeBufferToFinalVideo(uint8(encodedBuffer));

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

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 4월 18일
Make sure buffer is uint8.
buffer = zeros(height, width, numChannels, framesPerSymbol, 'uint8')

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by