2

Sorry for my bad English :( hope you guys can understand what my problems are.

Fist this is my code of a double pendulum simulation.

close all;clear all; clc;

theta1          = -pi;
dtheta1         = -5;
theta2          = pi/2;
dtheta2         = 10;

m1              = 1;
m2              = 1.7;
r1              = 1.3;
r2              = 1;
g               = 9.8;

duration        = 5;

y0 = [theta1;dtheta1;theta2;dtheta2;m1;m2;r1;r2;g];

[t,y] = ode45(@eqns,[0 duration],y0);

theta1          = wrapToPi(y(:,1));
dtheta1         = y(:,2);
theta2          = wrapToPi(y(:,3));
dtheta2         = y(:,4);

fps             = round(length(theta1)/duration);

X = zeros(length(theta1),3);
Y = zeros(length(theta1),3);

for i=1:length(theta1)
    X(i,:)=[0,r1*sin(theta1(i)),r1*sin(theta1(i))+r2*sin(theta2(i))];
    Y(i,:)=[0,-r1*cos(theta1(i)),-r1*cos(theta1(i))-r2*cos(theta2(i))];
end

mov = VideoWriter('Double Pendulum','MPEG-4');
set(mov,'FrameRate',fps,'Quality',100);
open(mov)

h = plot(0,0,'MarkerSize',30,'Marker','.','LineWidth',1.4);
range = 1.25*(r1+r2);axis([-range range -range range]); axis square;
xlabel('x');ylabel('y');title('Double Pendulum');

set(gca,'nextplot','replacechildren');
textlocation = range * 0.7;
hold on

for i=2:length(theta1)
    set(h,'XData',X(i,:),'YData',Y(i,:));
    plot([X(i-1,3) X(i,3)],[Y(i-1,3) Y(i,3)],'r')
    plot(X(i,2),Y(i,2),'g')
    frame = getframe;
    writeVideo(mov,frame);
end

close(mov);

And here is the result, https://www.dropbox.com/s/7pyagcnhca6khb3/Double%20pendulum%20result.PNG

For some reason the labels and title aren't get recorded, and I also realize that the colour are really different from what I see in Matlab. I tried using 'Uncompressed AVI' instead of 'MPEG-4' is give me the best result in terms of colour and quality, but the file size is 50MB, with 'MPEG-4' the file size is only 560KB.

I want to know, is there any setting that will give me a a better coulor (closer to what I see in Matlab) with lower file size (I am quite happy with this mp4 quality) and the labels will get recorded.

Thanks a lot.

Shai
  • 111,146
  • 38
  • 238
  • 371
SamC
  • 19
  • 1
  • 3
  • The default quality for MPEG-4 videos (`VideoWriter.getProfiles()`) is only set at 75/100. Have you tried setting it to 100? It will increase file size a bit, but color accuracy may improve. Unfortunately MPEG-4/h.264 is not the best codec for most videos Matlab of figures (there are better h.264 versions/profiles, but Matlab seems to use port settings). You might try Motion JPEG 2000. Or, if you don't mind QuickTime, try my [`QTWriter`](http://stackoverflow.com/a/16827218/2278029), which has codecs that are perfect for Matlab-type graphics. From there you could convert to other formats. – horchler Dec 19 '13 at 15:27
  • I use "set(mov,'FrameRate',fps,'Quality',100)" to set my quality to 100 already, but the colour still really bad. Thanks for you advice, I will try your QTWriter :) – SamC Dec 20 '13 at 04:49

1 Answers1

0

You ask two questions:
1. How to compress video without too much degradation in color quality
2. How to capture the title and labels for each frame

I will answer only your second question:

If you read the manual for getframe you'll see that by default it captures the current axes. In order to capture labels and title you need to capture the current figure. So, what you need is to replace frame = getframe by

frame = getframe( gcf );
Shai
  • 111,146
  • 38
  • 238
  • 371
  • This actually only gets the xlabel ylabel and the colorbar if its there, it does not get the title or for instance colorbar scaling. – Leo Feb 16 '14 at 16:17