MATLAB: How to convert 2D triangulation to binary image

binary imageconnectivitylistconvertpointstriangulation

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.')

Best Answer

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?