How to convert 2D triangulation to binary image?
3 次查看(过去 30 天)
显示 更早的评论
Does anyone know of an easy way to convert a 2D triangulation to a binary image, which represents the inside?
Let my clarify my question with a picture: http://i41.tinypic.com/2gvvvnl.jpg The goal is to go from left to right...
My triangulation is given its Points and the ConnectivityList (i.e., it was created with the matlab function "triangulation").
It is important that no holes get filled and all features are preserved.
BEST ANSWER SO FAR:
% Make some data
pts = rand(50,2);
DT = delaunayTriangulation(pts);
% Find a central triangle to remove
[~,holeTrias] = min(sum(abs(DT.incenter - 0.5),2));
holeTrias = [holeTrias DT.neighbors(holeTrias)];
triasMask = ~ismember(1:DT.size(1), holeTrias);
T = triangulation(DT.ConnectivityList(triasMask,:), DT.Points);
% Define query points
X = 0:0.05:1;
Y = 0:0.05:1;
[xMat,yMat] = meshgrid(X,Y);
IN = false(size(xMat));
for t = 1:size(T,1)
vertsXY = T.Points(T.ConnectivityList(t,:),:);
IN = IN | inpolygon(xMat,yMat, vertsXY(:,1), vertsXY(:,2));
end
% Show the result
figure
patch('faces',T.ConnectivityList,'vertices',T.Points,'FaceColor','g')
hold on
plot(xMat(IN),yMat(IN),'b.',xMat(~IN),yMat(~IN),'r.')
0 个评论
采纳的回答
Sven
2013-8-21
编辑:Sven
2013-8-21
Hi Geert,
Here's something that does what you want. It's not optimised for speed as it simply iterates naively through triangles, but it's at least correct.
% Make some data
pts = rand(50,2);
DT = delaunayTriangulation(pts);
% Find a central triangle to remove
[~,holeTrias] = min(sum(abs(DT.incenter - 0.5),2));
holeTrias = [holeTrias DT.neighbors(holeTrias)];
triasMask = ~ismember(1:DT.size(1), holeTrias);
T = triangulation(DT.ConnectivityList(triasMask,:), DT.Points);
% Define query points
X = 0:0.05:1;
Y = 0:0.05:1;
[xMat,yMat] = meshgrid(X,Y);
% Query each point
IN = false(size(xMat));
for i = 1:numel(IN)
for t = 1:size(T,1)
vertsXY = T.Points(T.ConnectivityList(t,:),:);
if inpolygon(xMat(i),yMat(i), vertsXY(:,1), vertsXY(:,2))
IN(i) = true;
break;
end
end
end
% Show the result
figure
patch('faces',T.ConnectivityList,'vertices',T.Points,'FaceColor','g')
hold on
plot(xMat(IN),yMat(IN),'b.',xMat(~IN),yMat(~IN),'r.')
How does that work for you?
5 个评论
T. Dunker
2022-7-6
with R2018b and later this simplifies to
%% Make some data
pts = rand(50,2);
DT = delaunayTriangulation(pts);
% Find a central triangle to remove
[~,holeTrias] = min(sum(abs(DT.incenter - 0.5),2));
holeTrias = [holeTrias DT.neighbors(holeTrias)];
triasMask = ~ismember(1:DT.size(1), holeTrias);
T = triangulation(DT.ConnectivityList(triasMask,:), DT.Points);
% Define query points
X = 0:0.02:1;
Y = 0:0.02:1;
%% use polyshape
tic
[xMat,yMat] = meshgrid(X,Y);
bp = boundaryshape(T);
IN = reshape(isinterior(bp, xMat(:), yMat(:)), size(xMat));
toc
%%
colormap(gray(2))
imagesc(X, Y, IN)
hold on;
patch('faces', T.ConnectivityList, 'vertices', T.Points, ...
'FaceColor', 'r', 'FaceAlpha', .2)
hold off;
axis equal;
axis tight;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Triangulation Representation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!