问题描述
除了存在绘制矢量场的特殊功能外,我还遇到了奇怪的Matlab行为:绘制图像(使用imagesc
或imshow
)并用彩色线条覆盖(使用plot
或line
)有时会导致背景图像的删除.
Apart from the fact there exist special functions to plot vector fields, I have encountered a strange Matlab behaviour: Plotting an image (with imagesc
or imshow
) and overlaying it with colored lines (with plot
or line
) leads at some point to an erasement of the background image.
%% some data...
% random image
Image = rand(200,400);
% 900 lines of random color
color1 = rand(1,900);
color2 = rand(2,900);
color3 = rand(3,900);
% some positions
x = 31:60;
y = 31:60;
[X,Y] = meshgrid(x,y);
%% plot process
% plot Image (with 'imshow' or 'imagesc')
imshow(Image);
hold on;
% plot the lines (with 'line' or 'plot')
for i = 1:900
line([X(i), X(i)+1],[Y(i),Y(i)+2],'color',[color1(i),color2(i),color3(i)]);
if i == 100 % nothings happens to background image after 100 vectors
pause();
elseif i == 200 % gradually starts to change...
pause();
end
end
% ... at the end it is completely erased
结果:100行
结果:200行
结果:900行
妙方:将图片另存为PNG可以恢复图片(但会破坏线分辨率).
Nice side fact Saving the image as PNG restores the image (but destroys the line resolution).
推荐答案
这不是正确的 answer ,因为它不能完全解释为什么会发生这种情况,但是它提供了一种解决方法并对怪异行为进行了更多观察.
This is not properly an answer, as it doesn't exactly explain why this is happening, but it provides a workaround, along with some more observations of the weird behaviour.
我尝试了您的示例,并且确实是:
I tried your example and indeed:
- HG2之前版本(R2013a):行为与您描述的相同
- HG2(R2015a):没问题,一切都在那里.
经过反复试验,我发现这是HG2之前版本中painter
渲染器的特定行为.
After a few trial and error, I worked out that it is a specific behaviour of the painter
renderer in pre HG2 versions.
如果将渲染器更改为默认的painter
以外的任何其他选项,则会取回图像和叠加的线.
If you change the renderer to any other than the default painter
, you get back your image and your superimposed lines.
set(gcf,'Renderer','zbuffer')
%// OR
set(gcf,'Renderer','opengl')
观察:
请注意,我也尝试过:
Observations:
Note that I also tried to:
- 首先显示线条(没问题),然后显示图像(并使用
uistack
重新排序)=>相同的黑色图像. - 使用多轴=>黑框
- display the lines first (no problem), then the image (and reorder using
uistack
) => same black image. - use multiple axes => black frame
并向您展示持久性是怎么回事:
And to show you how persistent is the glitch:
- 如果删除所有行,则图像不会重新出现(=黑框).
- 如果删除所有图形对象,则重新显示图像=>黑框
- 如果您
cla
甚至clf
,则重新显示图像=>黑框
- if you delete all the lines, the image does not reappear (=black frame).
- if you delete all graphics objects, then re display the image => black frame
- if you
cla
or evenclf
then re display the image => black frame
我发现获取显示图像的唯一方法是如上所述更改渲染器.
The only way I found to get the image displayed is to change the renderer as described above.
最初,我以为在保存图形时会在后台进行渲染器的更改,从而使最终输出得以完全显示.不幸的是,通过多做一些探索似乎并不那么简单.
Initially, I thought the change of renderer was happening behind the scene when you were saving the figure, thereby allowing the final output to be fully displayed. Unfortunately, by exploring a bit more it doesn't seem to be so simple.
我尝试使用print
(而不是saveas
)使用其他版本,因为它允许您选择渲染器.对于每个渲染器,我选择了2种格式,使用ghostscript
引擎的PDF
和使用Matlab引擎的PNG
:
I tried different version with print
(instead of saveas
) since it allows you to select the renderer. For each renderer I chose 2 formats, PDF
which uses the ghostscript
engine, and PNG
which uses the Matlab engine:
%%
print(1,'-dpng','-painters','testimageP.png')
print(1,'-dpng','-zbuffer' ,'testimageZ.png')
print(1,'-dpng','-opengl' ,'testimageO.png')
%%
print(1,'-dpdf','-painters','testimageP.pdf')
print(1,'-dpdf','-zbuffer' ,'testimageZ.pdf')
print(1,'-dpdf','-opengl' ,'testimageO.pdf')
好吧,结果出来后我仍然不确定发生了什么.所有这些保存的数字都会在顶部显示正确的图像和线条...但是:
Well, after results I am still unsure of what is happening. All these saved figures show the proper image and the lines on top... But:
3张png
图像(Matlab
engine )完全相似.它们甚至没有显示出饱和度上的细微差别,就像您手动切换渲染器时所观察到的那样.这使我认为Matlab选择忽略我的渲染器规范.它只是决定哪一个最相关,然后继续打印相同数字的3倍.所以我认为可能是未使用painter
渲染器的原因,这就是显示图像的原因.
The 3x png
images (Matlab
engine) are exactly similar. They do not even show slight difference in saturation like you can observe when you switch the renderer manually. This made me think that Matlab chose to ignore my renderer specification. It just decided which one was the most relevant and went ahead printing 3 times the same figure. So I thought may be the painter
renderer wasn't used and that's why the images were shown.
不是那么快.在3x pdf
图像(ghostscript
engine )上...我可以观察到3张图片之间的细微差别...因此渲染器之间的渲染并不相同. painter
用于其中之一,并成功渲染了图像.
Well not so fast. On the 3x pdf
images (ghostscript
engine) ... I can observe the small nuances between the 3 pictures ... so the renderer wasn't the same between them. The painter
was used on one of them, and successfully rendered the image.
因此,总而言之,painter
渲染器似乎仅在应用于(HG2之前的)图形时才出现故障!
So in conclusion, it seems the painter
renderer is only glitchy when applied to a (pre-HG2) figure!
这篇关于覆盖线条后图像逐渐被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!