Why figure doesn't render properly? (Opengl painters)
Show older comments
I was trying to save a uint8 3D RGB matrix into png format with print(), but some pixels showed undesired shades. First try, I was saving a simple magenta rectangle (matrix 300x600x3) and the shades were on the borders: I solved setting the renderer to 'painters' (figures attached)

But when I tried the same on a much larger matrix (945x2556x4355) with more complex plot, even '-painters' does not work
Maybe here is not very clear, but in the original output I just want white and magenta pixels, while you can see here there are a lot of pink and grey shades that (I checked it) are not present in the original matrix and they don't show up when I display the image with imshow(). It's a matter of how Matlab represent the output.
I've read that Matlab can't mantain 'painters' for complex plot and adjust automatically the renderer to Opengl, so I tried to force it with several solutions:
imshow(layer1) % my matrix
s=1
fig=get(groot,'CurrentFigure');
fig.PaperUnits='points';
fig.PaperPosition=[0 0 288 144]; % I need a 600x300 pixel output
fig.Renderer='painters';
fig.RendererMode='manual';
filenamearray=["layer",s];
loopfilename=strjoin(filenamearray,'_');
axis off;
print('-painters','-dpng',loopfilename);
%%% or
print('-painters',loopfilename,"-dpng");
%%% or
print('-vector',loopfilename,"-dpng");
%%% or
print('-zbuffer',loopfilename,"-dpng");
%%% or
saveas(fig,"layer1",'png');
but they work only for the simple rectangle.
Any hint? Thanks in advance
8 Comments
Jan
on 13 Oct 2022
Did you update the drivers of your graphics card? Which Matlab version are you using? The last version, which supported "ZBuffer" was R2014a.
Simone Cotta Ramusino
on 13 Oct 2022
Simone Cotta Ramusino
on 13 Oct 2022
Robert U
on 14 Oct 2022
Check, whether you deactivated the default-graphics improvements such as smoothing: https://de.mathworks.com/help/matlab/ref/rendererinfo.html?searchHighlight=painters&s_tid=srchtitle_painters_4
Additionally, I am not sure, whether or not the resolution and dpi values are forced correctly. Have a look here: https://de.mathworks.com/matlabcentral/answers/415952-plot-resolution-and-size?s_tid=srchtitle
Kind regards,
Robert
Simone Cotta Ramusino
on 14 Oct 2022
Simone Cotta Ramusino
on 14 Oct 2022
Robert U
on 14 Oct 2022
Can you provide some data you are working with. Then, it would be possible to reconstruct your problem maybe, and try to fix it.
Kind regards,
Robert
Simone Cotta Ramusino
on 14 Oct 2022
Answers (2)
Robert U
on 14 Oct 2022
Hi Simone Cotta Ramusion,
as suspected I could reproduce the smoothing at the edges. Setting smoothing off, eliminates the shaded starting and ending pixels in each line. Even I use the default renderer.
brick=zeros(300,600,3,'uint8');
brick(:,:,1)=165;
brick(:,:,2)=35;
brick(:,:,3)=99;
imshow(brick)
loops=1;
for index=1:loops
filenamearray=["face",index];
loopfilename=strjoin(filenamearray,'_');
imshow(brick);
fig=get(groot,'CurrentFigure');
fig.PaperUnits='points';
fig.PaperPosition=[0 0 288 144]; %
fig.GraphicsSmoothing = 'off';
axis off;
print(loopfilename,"-dpng");
end
16 Comments
Simone Cotta Ramusino
on 14 Oct 2022
Simone Cotta Ramusino
on 14 Oct 2022
Simone Cotta Ramusino
on 15 Oct 2022
Hi Simone Cotta Ramusino,
I tried to reproduce the problem with your test image. Unfortunately, I do not see any minimal pink shade. Maybe, you can attach a file with the region of interest.
brick=zeros(1300,1600,3,'uint8');
colonnebianche=randi(1550,[500,1]);
righebianche=randi(1000,[400,1]);
brick(:,:,1)=165;
brick(righebianche,colonnebianche,1)=255;
brick(:,:,2)=35;
brick(righebianche,colonnebianche,2)=255;
brick(:,:,3)=99;
brick(righebianche,colonnebianche,3)=255;
loops=1;
for index=1:loops
filenamearray=["face",index];
loopfilename=strjoin(filenamearray,'_');
imshow(brick);
fig=get(groot,'CurrentFigure');
fig.PaperUnits='points';
fig.PaperPosition=[0 0 288 144]; %
fig.GraphicsSmoothing = 'off';
axis off;
print(loopfilename,"-dpng");
end
Kind regards,
Robert
Simone Cotta Ramusino
on 17 Oct 2022
Robert U
on 17 Oct 2022
Hi Simone Cotta Ramusino,
I attached the saved file which I got with the code posted in the comment above with your example data. I ran it in R2021b.

When magnifying, I cannot see any shades. Looks ok to me.

Kind regards,
Robert
Simone Cotta Ramusino
on 17 Oct 2022
Simone Cotta Ramusino
on 17 Oct 2022
Robert U
on 17 Oct 2022
Hi Simone Cotta Ramusino,
as written above, I only changed the smoothing on and off with that particular line:
fig.GraphicsSmoothing = 'off';
I did not change anything else. I did not use '-painters' renderer in comparison to your initial post.
Unfortunately, I cannot run the example in R2022b. Sorry.
Kind regards,
Robert
Simone Cotta Ramusino
on 17 Oct 2022
Robert U
on 17 Oct 2022
opengl info
Version: '4.6.0 NVIDIA 472.98'
Vendor: 'NVIDIA Corporation'
Renderer: 'Quadro P2000/PCIe/SSE2'
RendererDriverVersion: '30.0.14.7298'
RendererDriverReleaseDate: '05-Jan-2022'
MaxTextureSize: 32768
Visual: 'Visual 0x09, (RGBA 32 bits (8 8 8 8), Z depth 16 bits, Hardware acceleration, Double buffer, Antialias 8 samples)'
Software: 'false'
HardwareSupportLevel: 'full'
SupportsGraphicsSmoothing: 1
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
Extensions: {394×1 cell}
MaxFrameBufferSize: 32768
Simone Cotta Ramusino
on 17 Oct 2022
Simone Cotta Ramusino
on 18 Oct 2022
Simone Cotta Ramusino
on 19 Oct 2022
Robert U
on 21 Oct 2022
Hi Simone Cotta Ramusino,
I found another post here: https://de.mathworks.com/matlabcentral/answers/54750-getting-better-high-resolution-images leading to the website https://matlab.fandom.com/wiki/FAQ#How_do_I_save_my_figure,_axes,_or_image?_I'm_having_trouble_with_the_built_in_MATLAB_functions.
I tried https://github.com/altmany/export_fig but without changing settings I did not succeed to remove the artifacts in the bigger brick export. Maybe, investing more effort is necessary.
Kind regards,
Robert
Simone Cotta Ramusino
on 21 Oct 2022
Simone Cotta Ramusino
on 19 Oct 2022
0 votes
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

