Quality/compression problem using VideoWriter

39 vues (au cours des 30 derniers jours)
Roderick
Roderick le 19 Avr 2023
Commenté : DGM le 27 Avr 2023
Dear all
I have a problem with the sharpness with which my videos are generated by VideoWriter compared to when I individually plot a frame. An example of what a frame, https://www.dropbox.com/s/38b98pg4nxe32tf/Monolayer_CrSBr_mx_t1_MC_Biaxial_No_Field_Dipolar_2x_DMI.pdf?dl=0, and its corresponding guide code looks like:
u1=figure('visible','off');
imagesc(space_x,space_y,mx_extended(:,:,i));
axis xy;
colormap(othercolor('PuOr8'));
box on;
clr1=colorbar('YTickLabel',{'-1','-0.5','0','0.5','1'},'YTick',[-1:0.5:1]);
xlabel('$x$-{\it th} spatial direction, $x \, \, \left( \mathrm{nm} \right)$','FontSize',14,'interpreter','latex');
ylabel('$y$-{\it th} spatial direction, $y \, \, \left( \mathrm{nm} \right)$','FontSize',14,'interpreter','latex');
ylabel(clr1,'$x$-{\it th} magnetization component, $m_x$','Interpreter','Latex','FontSize',14);
caxis([-1 1]);
xlim([0 100]);
ylim([0 100]);
t1=title(['$t= \, \, $',num2str(round(time(i),1)),' ns, $T= \, \,$',num2str(temperature(i)),' K, $\mathbf{H}=0$'],'FontSize',14,'interpreter','latex');
set(t1,'interpreter','latex','FontSize',14);
set(gca,'TickLabelInterpreter','latex','FontSize',14);
set(u1,'Units','Inches');
posu1=get(u1,'Position');
set(u1,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[posu1(3),posu1(4)]);
saveas(gcf,fullfile(outputdir,strcat(['Monolayer_CrSBr_mx_t',num2str(i),'_MC_Biaxial_No_Field_Dipolar_2x_DMI.pdf'])));
On the other hand, an example of what one of my generated videos looks like, https://www.dropbox.com/s/epz49pz507vnjb5/Monolayer_CrSBr_mx_MC_Biaxial_No_Field_Dipolar_2x_DMI.avi?dl=0, and the code with which I generate it:
avi_mx=VideoWriter('C:...\Monolayer_CrSBr_mx_MC_Biaxial_No_Field_Dipolar_2x_DMI.avi');
avi_mx.FrameRate=1;
open(avi_mx);
u4=figure('visible','off');
for i=1:length(time)
imagesc(space_x,space_y,mx_extended(:,:,i));
hold off
axis xy;
colormap(othercolor('PuOr8'));
box on;
clr4=colorbar('YTickLabel',{'-1','-0.5','0','0.5','1'},'YTick',[-1:0.5:1]);
xlabel('$x$-{\it th} spatial direction, $x \, \, \left( \mathrm{nm} \right)$','FontSize',14,'interpreter','latex');
ylabel('$y$-{\it th} spatial direction, $y \, \, \left( \mathrm{nm} \right)$','FontSize',14,'interpreter','latex');
ylabel(clr4,'$x$-{\it th} magnetization component, $m_x$','Interpreter','Latex','FontSize',14);
caxis([-1 1]);
xlim([0 100]);
ylim([0 100]);
t4=title(['$t= \, \, $',num2str(round(time(i),1)),' ns, $T= \, \,$',num2str(temperature(i)),' K, $\mathbf{H}=0$'],'FontSize',14,'interpreter','latex');
set(t4,'interpreter','latex','FontSize',14);
set(gca,'TickLabelInterpreter','latex','FontSize',14);
set(u4,'Units','Inches');
posu4=get(u4,'Position');
set(u4,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[posu4(3),posu4(4)]);
frame_mx=getframe(gcf);
writeVideo(avi_mx,frame_mx)
end
close(avi_mx)
Any idea how to solve this problem? Also, would it be possible for the videos not to have a greyish background color? In the single frame the background is white.

Réponses (1)

Himanshu
Himanshu le 24 Avr 2023
Hello Richard,
As per my understanding, you are facing a problem with the sharpness while generating a video using "VideoWriter" in MATLAB. The issue you are facing is likely due to the resolution of the frames in the video.
To improve the sharpness and quality of your video, you can increase the resolution of the frames by using the below code:
set(u4, 'Position', [0 0 1920/300 1080/300]); % Set the figure size to match the desired video resolution (e.g., 1920x1080)
set(u4, 'PaperPositionMode', 'Auto', 'PaperUnits', 'Inches', 'PaperSize', [1920/300 1080/300]); % Set the paper size to match the figure size
set(u4, 'InvertHardcopy', 'off'); % Preserve the background color when saving the figure
You can set the background color of the figure to white using the below code:
u4 = figure('visible', 'off');
set(u4, 'color', 'w'); % Set the background color to white
You can refer to the below documentation to learn more about "Position", "PaperPositionMode", "InvertHardcopy", and "color" properties of "Figure" in MATLAB.
  2 commentaires
Roderick
Roderick le 27 Avr 2023
Dear Himanshu
Thank you for your detailed response. Concerning the problem with the color of the background, your proposal worked perfectly. However, concerning
set(u4, 'Position', [0 0 1920/300 1080/300]); % Set the figure size to match the desired video resolution (e.g., 1920x1080)
set(u4, 'PaperPositionMode', 'Auto', 'PaperUnits', 'Inches', 'PaperSize', [1920/300 1080/300]); % Set the paper size to match the figure size
this just changes the size of the image, but you can still see that the image is blurry. I would be interested in keeping the size of the original image, which is the same as the pdf images I generate (see the first example of code in my post), and with the same sharpness as those images. I am attaching a video that I generated with your instructions (it is another system), https://www.dropbox.com/s/hx2j3kd0am27pkh/mx_CrSBr_Bilayer_First_Layer_Dipolar_DMI_AFM_x_Field_5_mT.avi?dl=0. To generate it I also included
avi_mx=VideoWriter('C:....avi','Uncompressed AVI');
DGM
DGM le 27 Avr 2023
There are other lossless profiles supported by VideoWriter(), and there are corresponding options for compression settings.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by