如何避免matlab图中矩形贴片元素中的弯曲拐角? [英] How to avoid curved corners in rectangular patch element in matlab figure?

查看:125
本文介绍了如何避免matlab图中矩形贴片元素中的弯曲拐角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在一个matlab图中生成一组矩形块时,一些矩形边被渲染为曲线或剪切而非尖锐,这是不需要的。这取决于规模,放大图像往往会消除这种影响。我认为这可能与混叠/压缩效果有关。奇怪的是,使用矩形这个问题似乎消失了。



下面是中间放大问题的示例(其他问题,如虚线边框,不应该也存在):



代码是从另一个答案到另一个 question

  H = Hadamard(48 ); %#现在将行列两倍矩阵
A =(1 + H)/ 2;
B =(1-H)/ 2;
C = [A; B]; %#下面的代码随机置换矩阵行内的元素
[nRows,nCols] = size(C);
[junk,idx] = sort(rand(nRows,nCols),2); %#将列索引转换为线性索引
idx =(idx-1)* nRows + ndgrid(1:nRows,1:nCols); %#重新排列任何矩阵
E = C;
E(:) = E(idx);
[X Y] = find(逻辑(E));
xl =长度(X);
yl =长度(Y);
图,对于ii = 1:xl
patch(X(ii)+ [0 0 1 1],Y(ii)+ [0.15 0.9 0.9 0.1],[1 1'],'Edgecolor',[1 1 1])$ ​​b $ b end
axis([0 max(X)+1 0 max(Y)+1])$ ​​b $ b axis ')
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[]' YTick',[])

我的问题是:
(1)以及如何)摆脱在所示示例中看到的斑块对象的弯曲拐角和其他毛刺,在用于在屏幕上显示整个图形的低到中等程度的放大率。
(2)最重要的是能够生成缺少弯曲拐角的图像文件(jpg,png,pdf ...)。我看到的所有格式看起来都会保留不想要的效果。回答2回答(1)本质上不重要,回答(1)大概解答(2)。

编辑



矩形被使用,这似乎是matlab渲染引擎的问题?注意:这个例子是用R14生成的,但是链接到的问题的OP有类似的问题(matlab版本未知)。



我经历了各种光照和边缘表示选项可用于贴片对象但未观察到改善。

解决方案

这个问题可能会重演,例如有类似的问题被问到这里

答案似乎是避免在绘制矩形时明确使用 patch 。改为使用 fill 或者 rectangle 。据我所知,以下生成该图的方法提供了几乎相同的结果:

 加载had.mat%< - 加载数组中包含感兴趣矩阵的数据E 
[XY] = find(logical(E));
xl =长度(X);
yl =长度(Y);
图,对于ii = 1:xl
矩形('Position',[X(ii)Y(ii)+。2 1 0.8],'facecolor',[1 ([X(ii)X(ii)X(ii)+1 X(ii)+1],[Y(ii)+ 1],'edgecolor',[1 1 1] (ii)+0.8 Y(ii)+0.2],[111],'edgecolor',[111],'marker','。','markersize',1 )
end
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick', [],'YTick',[])
set(gcf,'Renderer','zbuffer')


When I generate a set of rectangular patches in a matlab figure, some of the rectangle edges are rendered curved or clipped rather than sharp, which is unwanted. This depends on the scale, zooming into the image tends to eliminate the effect. I thought this might have to do with an aliasing/compression effect. Curiously, using rectangle the problem seems to go away.

Here is an example of the problem at intermediate magnification (other problems such as dashed borders which shouldn't be there are also evident):

The code is from an answer to another question:

H=Hadamard(48); %# now to row-double the matrix 
A=(1+H)/2;
B=(1-H)/2;
C=[A; B]; %# the code below randomly permutes elements within the rows of the matrix 
[nRows,nCols] = size(C); 
[junk,idx] = sort(rand(nRows,nCols),2); %# convert column indices into linear indices 
idx = (idx-1)*nRows + ndgrid(1:nRows,1:nCols); %# rearrange whatever matrix 
E = C; 
E(:) = E(idx);
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
     patch(X(ii) + [0 0 1 1],Y(ii) + [0.15 0.9 0.9 0.1],[1 1 1],'Edgecolor',[1 1 1])
end
axis([0 max(X)+1 0 max(Y)+1])
axis('square')
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])

My questions are: (1) Is it possible (and how) to get rid of the curved corners and other glitches of patch objects seen in the example shown, at low to intermediate degrees of magnification used to display the entire figure on screen. (2) Most important is to be able to generate an image file (jpg, png, pdf...) which lacks the curved corners. All formats I looked into appear to conserve the unwanted effect. Answering 2 makes answering (1) essentially unimportant, and answering (1) presumably solves (2).

Edit

Since the problem goes away when rectangle is used, this appears to be a problem with the matlab rendering engine? Note: the example was generated with R14 but the OP of the question linked to had similar problems (matlab version unknown).

I went through the various lighting and edge representation options available for patch objects but no improvement was observed.

解决方案

The question is a likely repeat, for instance a similar questions was asked here.

The answer appears to be to avoid explicit use of patch when drawing rectangles. Use either fill or just rectangle instead. The following ways of generating the figure provide nearly equivalent results, as far as I could tell:

load had.mat % <-- load the data containing the matrix of interest in array E
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
      rectangle('Position',[X(ii) Y(ii)+.2 1 0.8],'facecolor',[1 1 1],'edgecolor',[1 1 1])
%     fill([X(ii) X(ii) X(ii)+1 X(ii)+1], [Y(ii)+0.2 Y(ii)+0.8 Y(ii)+0.8 Y(ii)+0.2],[1 1 1],'edgecolor',[1 1 1],'marker','.','markersize',1)
end
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])
set(gcf,'Renderer','zbuffer')

这篇关于如何避免matlab图中矩形贴片元素中的弯曲拐角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆