Looking for a way to patch 44k polygons fast

11 次查看(过去 30 天)
The following is a part of a function that patches triangles with the corresponding colours using a for-loop
for i = 1:numel(tri)/3
color(i, :) = img(center(i,1), center(i,2),:); %gets current color from img
patch(points(tri(i,:), 2), points(tri(i,:), 1), color(i,:)/256, 'EdgeColor','none'); %patches color on polygon
end
%size(tri) = 44721x3
%size(color) = 44271x3
%size(points) = 22429x2
%size(points(tri(i,:),1) = 3x1
%size(points(tri(i,:),2) = 3x1
%size(color(i,:)/256) = 1x3
The only problem is that patching this large number of polygons takes a long time (+/- 19s). Tried using `fill` instead of `patch` to see if that was faster, but sadly that was 13s slower.
Is there a way to speed up the patching process? For example by patching all the polygons in 1 sweep without looping?

采纳的回答

Greg Dionne
Greg Dionne 2018-10-15
Have you tried using just one patch? You can specify X and Y as matrices (this example taken from the doc page on patch )
x = [2 5; 2 5; 8 8];
y = [4 0; 8 2; 4 0];
c = [0; 1];
figure
patch(x,y,c)
colorbar
You can also use the 'Faces' and 'Vertices' syntax.
v = [2 4; 2 8; 8 4; 5 0; 5 2; 8 0];
f = [1 2 3; 4 5 6];
col = [0; 1];
figure
patch('Faces',f,'Vertices',v,'FaceVertexCData',col,'FaceColor','flat');
colorbar
  1 个评论
YT
YT 2018-10-15
Thank you for your answer. I forgot to look a bit further in the documentation, but this definatly helped me out. Went from 19s to 1.5seconds!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by