Chase plot changes figure size internally

2 次查看(过去 30 天)
I am trying to run a driving Scenario in a chase Plot and save the animation in a figure as:
scenario = drivingScenario;
ls = lanespec(2,'Width',5);
roadCenters = [-20 0; 5 0];
road(scenario,roadCenters,'Name','Road 1','Lanes',ls);
roadCenters = [10 5; 10 30];
road(scenario,roadCenters,'Name','Road 2','Lanes',ls);
roadCenters = [10 -5; 10 -30];
road(scenario,roadCenters,'Name','Road 3','Lanes',ls);
roadCenters = [15 0; 40 0];
road(scenario,roadCenters,'Name','Road 2','Lanes',ls);
car1 = vehicle(scenario, 'ClassID', 1, 'Position', [-20, -2.5, 0], 'Length', ...
3, 'Width', 2, 'Height', 1.5);
waypoints = [-20, -2.5; 40, -2.5];
speed = [12 12];
yaw = [0 0];
smoothTrajectory(car1, waypoints, speed, 'Yaw', yaw, 'Jerk', 2);
vidObj2 = VideoWriter('SCP_Chase_Plot','MPEG-4');
open(vidObj2);
fig2 = figure('Name', 'Chase Plot', 'Units', 'pixels', 'Position', [100, 100, 640, 480]);
ax2 = axes('Parent', fig2);
chasePlot(car1,'Centerline','on', 'Parent', ax2);
set(fig2, 'Units', 'pixels', 'Position', [100, 100, 640, 480]);
while advance(scenario)
set(fig2, 'Units', 'pixels', 'Position', [100, 100, 640, 480]);
frame2 = getframe(ax2);
writeVideo(vidObj2, frame2);
pause(0.01)
end
However, when I run the code, it gives me the error:
Error using VideoWriter/writeVideo (line 368)
Frame must be 640 by 480

采纳的回答

Walter Roberson
Walter Roberson 2025-5-13
frame2 = getframe(ax2);
writeVideo(vidObj2, frame2);
You are fetching graphics data from an axes. It turns out, however, that the size of an axes can vary slightly, mostly due to slight differences in the exact width of tick labels as the tick labels change. But when you go to writeVideo, each frame after the first one must be exactly the same size as the first frame.
A workaround is to use
frame2 = getframe(ax2);
frame2 = imresize(frame2.cdata, [640 480]);
writeVideo(vidObj2, frame2);
If you use this technique, you might notice the exact position of the drawn right boundary moving around a little according to how many pixels beyond the boundary any given final pixel label extends.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Weather and Atmospheric Science 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by