Looking for a way to patch 44k polygons fast

조회 수: 11 (최근 30일)
YT
YT 2018년 10월 15일
댓글: YT 2018년 10월 15일
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