Create a video from frames taken 5 by 5
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
Hello, I have this function to create a video:
function [video] = writeBufferToFinalVideo(buffer,fps)
video = VideoWriter('prueba','MPEG-4');
video.FrameRate = fps;
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
This function have it has a dimension buffer as input
frameBuffer = zeros (height, width, numChannels, framesPerSymbol)
where framePerSymbol will be the number of frames that my symbol lasts. In my case this number equals 5, so I will have 5 frames.
The idea is that from a video I take 5 by 5 frames as a FIFO queue and once I fill the frame buffer, I decide whether I can encode it or not, and once decided I create a video with those frames (if I know has been able to encode or not). This is the code I run it on:
frameBuffer = zeros(height,width,numChannels,framesPerSymbol);
framesInBuffer = 0;
while hasFrame(videoObject)
    frame = double(readFrame(videoObject));
    frameBuffer = shiftBuffer(frameBuffer,frame);
    % We update the framesInBuffer counter
    framesInBuffer = framesInBuffer + 1;
    if framesInBuffer > framesPerSymbol
        framesInBuffer = framesPerSymbol;
        bypassEncoding = false;
    else
        bypassEncoding = true;
    end
    if ~bypassEncoding
        if canWeEncode(frameBuffer,alpha,threshold)     % Frames with code
            encodedBuffer = steganographicEncoding(frameBuffer,width,height,codeRows,codeCols,alpha,sigma);
            writeBufferToFinalVideo(encodedBuffer,100);
        else                                            % Only Frames without code
            writeFrameToFinalVideo(squeeze(frameBuffer(:,:,:,1)));
        end
    end
end
Finally you should get a video with the same length as the original. ShiftBuffer will be in charge of taking the frames from 5 to 5 as a FIFO queue.
댓글 수: 2
  Geoff Hayes
      
      
 2020년 4월 20일
				Alberto - what is your question? Or have you posted code that you think others would be interested in?
채택된 답변
  Geoff Hayes
      
      
 2020년 4월 20일
        
      편집: Geoff Hayes
      
      
 2020년 4월 20일
  
      Alberto - whenever you call writeBufferToFinalVideo the code creates a new VideoWriter, writes five frames to file, and then closes the file. I think that what is happening is that every time when the code creates the VideoWriter object for the prueba file, the file is being re-created and so all information previously  written to it is lost. I was able to reproduce this by writing 20 frames to a file, then re-opening it to write five more frames...once close, the video file was smaller than before. What you may want to do is to create the video writer in the main file and then pass it as a parameter to the writeBufferToFinalVideo method.
frameBuffer = zeros(height,width,numChannels,framesPerSymbol);
framesInBuffer = 0;
% create the video writer object
video = VideoWriter('prueba','MPEG-4');
video.FrameRate = 100;
open(video);
while hasFrame(videoObject)
    frame = double(readFrame(videoObject));
    frameBuffer = shiftBuffer(frameBuffer,frame);
    % We update the framesInBuffer counter
    framesInBuffer = framesInBuffer + 1;
    if framesInBuffer > framesPerSymbol
        framesInBuffer = framesPerSymbol;
        bypassEncoding = false;
    else
        bypassEncoding = true;
    end
    if ~bypassEncoding
        if canWeEncode(frameBuffer,alpha,threshold)     % Frames with code
            encodedBuffer = steganographicEncoding(frameBuffer,width,height,codeRows,codeCols,alpha,sigma);
            writeBufferToFinalVideo(encodedBuffer,video);
        else                                            % Only Frames without code
            writeFrameToFinalVideo(squeeze(frameBuffer(:,:,:,1)));
        end
    end
end
% close the video writer object
close(video);
with
function [video] = writeBufferToFinalVideo(buffer,videoWriterObj)
for i = 1:size(buffer,4)
    img = buffer(:,:,:,i);
    img(img>1) = 1;
    img(img<0) = 0;
    writeVideo(videoWriterObj,img);
end
end
You will probably need to do the same for writeFrameToFinalVideo.
댓글 수: 3
  Geoff Hayes
      
      
 2020년 4월 21일
				What variable does the warning correspond to? (Typically warnings can be ignored.)
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Tracking and Motion Estimation에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

