Question

In: Computer Science

MATLAB question: how do you write a video file using videowriter without displaying the current figure...

MATLAB question: how do you write a video file using videowriter without displaying the current figure when doing getframe? I want to go through the data packet Xpixels x Ypixels x Zframes for each frame and write it to a video without showing the display until it's finally done. also I noticed if I turn the figure off the code runs much much slower using fig = figure(visible off). is there anyway to avoid this sluggishness? and speed it up without showing the figure?

Solutions

Expert Solution

  % Create a VideoWriter object to write the video out to a new, different file.
  writerObj = VideoWriter('NewRhinos.avi');
  open(writerObj);
  
  % Read the frames back in from disk, and convert them to a movie.
  % Preallocate recalledMovie, which will be an array of structures.
  % First get a cell array with all the frames.
  allTheFrames = cell(numberOfFrames,1);
  allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
  % Next get a cell array with all the colormaps.
  allTheColorMaps = cell(numberOfFrames,1);
  allTheColorMaps(:) = {zeros(256, 3)};
  % Now combine these to make the array of structures.
  recalledMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps)
  for frame = 1 : numberOfFrames
    % Construct an output image file name.
    outputBaseFileName = sprintf('Frame %4.4d.png', frame);
    outputFullFileName = fullfile(outputFolder, outputBaseFileName);
    % Read the image in from disk.
    thisFrame = imread(outputFullFileName);
    % Convert the image into a "movie frame" structure.
    recalledMovie(frame) = im2frame(thisFrame);
    % Write this frame out to a new video file.
    writeVideo(writerObj, thisFrame);
  end
  close(writerObj);
  % Get rid of old image and plot.
  delete(hImage);
  delete(hPlot);
  % Create new axes for our movie.
  subplot(1, 3, 2);
  axis off;  % Turn off axes numbers.
  title('Movie recalled from disk', 'FontSize', fontSize);
  % Play the movie in the axes.
  movie(recalledMovie);
  % Note: if you want to display graphics or text in the overlay
  % as the movie plays back then you need to do it like I did at first
  % (at the top of this file where you extract and imshow a frame at a time.)
  msgbox('Done with this demo!');

This is the code which will write video file using videowriter without displaying the current figure .

This doesn't uses  fig = figure(visible off),rather you can follow what I have done in the code. This code doesn't have any speed issues and also it does the jobs.However,  fig = figure(visible off) should also work just fine as it is also a very efficient way. You can try running other operations of during exexution if it is still slower.

Happy to help!


Related Solutions

Write a MATLAB script file to integrate  using trapezoid method (do not use trapz command for this...
Write a MATLAB script file to integrate  using trapezoid method (do not use trapz command for this part, write your own script). Consider x=-0.5 to 3 with Δt=0.01, and compare your result with using “integral” and "trapz" commands (all in one file).
Using the below question and values how do you calculate the standard deviation without using a...
Using the below question and values how do you calculate the standard deviation without using a calculator (by hand) of (s1^2/n1+s2^2/n2) with the standard deviation answer being 1.4938 and 1.1361 /sqrt(1.4938^2/10 + 1.1361^2/10) Choose two competing store and compare the prices of 10 items at both stores. Preform a two-sample hypothesis test to try an prove if one store is more expensive than the other based on your sample. Store A item #1-$5.49, #2 $3.77, #3 $0.87, #4 $3.29, #5...
Can you show how to do this step by step using Matlab please. Write a function...
Can you show how to do this step by step using Matlab please. Write a function with header [S] = myAddString(S1, S2), where S is the concatenation of the stings S1 and S2. Test Cases: S = myAddString(myAddString('Programming', ' '), myAddString(' is ', 'fun! ')) S = Programming is fun!
Use MATLAB to figure out the following problem, if you do not know how to use...
Use MATLAB to figure out the following problem, if you do not know how to use MATLAB then please do not answer. Coding is required for the exercise. For f(x) = arctan(x), find its zeros by implimenting Newtons method and the Secant method in Matlab. (Hint: Use Newtons method to calculate x1 for Secant method) Comment all code please since I would like to learn how to do this correctly in MATLAB. Thank you.
Coding: Use MATLAB to figure out the following problem, if you do not know how to...
Coding: Use MATLAB to figure out the following problem, if you do not know how to use MATLAB then please do not answer. Coding is required for the exercise. For f(x) = arctan(x), find its zeros by implimenting Newtons method and the Secant method in Matlab. (Hint: Use Newtons method to calculate x1 for Secant method) Comment all code please since I would like to learn how to do this correctly in MATLAB. Thank you.
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only...
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only explain the steps of designing filter
In MatLab ( this question is after a lecture about GUI ) % How do you...
In MatLab ( this question is after a lecture about GUI ) % How do you read a property from a control (i.e. the % 'string' property from an edit box)?
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT