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

I need to write a MATLAB code for this question, but I couldn't figure how to...
I need to write a MATLAB code for this question, but I couldn't figure how to do it. 7. Engineers often have to convert from one unit of measurement to another; this can be tricky sometimes. You need to think through the process carefully. For example, convert 5 acres to hectares, given that an acre is 4840 square yards, a yard is 36 inches, an inch is 2.54 cm, and a hectare is 10000 m2.
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...
Displaying Content of an XML File Using PHP Script or Code In this week, you are...
Displaying Content of an XML File Using PHP Script or Code In this week, you are going to write the PHP script or code to read an XML file and display its content on the screen. The file you will modify is the Products page, which you created in Week 1 Project. Given the following data in XML format, modify the Products page to read and display the information in the Products page: <Product> <Item> <Name>T-Shirt</Name> <Price>12.5</Price> </Item> <Item> <Name>Pants</Name>...
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)?
how to create a function to compute PACF of a time series in MATLAB without using...
how to create a function to compute PACF of a time series in MATLAB without using built-in function ''parcorr''?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT