Convert mesh to BW image?
Show older comments
Hello everybody,
[Problem]: I want to convert triangulated mesh to bw image.
Original Images:

Code used:
close all; clc; clear all;
load tr; % attached
triplot(tr.ConnectivityList,tr.Points(:,1),tr.Points(:,2)); grid on;
Wanted Output: I want to convert triangulated mesh to bw image by using ConnectivityList and Points?
Where
% ConnectivityList: list of faces #ConnectivityList x 3
% Points: vertex positions #V x 2
@Image Analyst
@Walter Roberson
I'll vote for all your answers and would gladly appreciate any comments.
Ivan
2 Comments
Image Analyst
on 1 Mar 2016
Nothing is attached. Have you tried passing the coordinates into poly2mask()?
Ivan Shorokhov
on 1 Mar 2016
Edited: Ivan Shorokhov
on 1 Mar 2016
Answers (1)
Image Analyst
on 1 Mar 2016
Poly2mask() won't work:
s=load('tr.mat')
tr = s.tr
x = tr.Points(:, 1);
y = tr.Points(:, 2);
subplot(1,2, 1);
scatter(x, y);
x = x - min(x) + 1;
y = y - min(y) + 1;
columns = ceil(max(x));
rows = ceil(max(y));
mask = poly2mask(x, y, rows, columns);
subplot(1,2,2);
imshow(mask);

You are going to have to use activecontour(). I don't have time now to make a demo for you with your data, but I've attached my generic demo below as an m-file.
7 Comments
Ivan Shorokhov
on 1 Mar 2016
Edited: Ivan Shorokhov
on 1 Mar 2016
Ivan Shorokhov
on 2 Mar 2016
Edited: Ivan Shorokhov
on 2 Mar 2016
Image Analyst
on 2 Mar 2016
Actually I tried that last night and it didn't seem to work very well. I also tried the envelope() function and that didn't work well either. Next I was going to try delaunay(), but I think I'll let you do that. I'm not sure if there's a way to do it but if you could somehow burn all the edges into an image then you'd be on your way.
Ivan Shorokhov
on 4 Mar 2016
Edited: Ivan Shorokhov
on 4 Mar 2016
Image Analyst
on 4 Mar 2016
Edited: Image Analyst
on 4 Mar 2016
I thought of that too, except using imline() to burn in lines, ORing a binary image into an accumulative binary image and then calling imfill(), but didn't suggest it because I thought it would be slow. How long does your method take?
Ivan Shorokhov
on 4 Mar 2016
Edited: Ivan Shorokhov
on 4 Mar 2016
Image Analyst
on 4 Mar 2016
That doesn't even look right. Are you sure you're using the correct triangles?

You can write the edges directly into the mask instead of summing whole images. It only takes .36 seconds:
s=load('tr.mat')
tr = s.tr
x = tr.Points(:, 1);
y = tr.Points(:, 2);
x = x - min(x) + 1;
y = y - min(y) + 1;
columns = ceil(max(x));
rows = ceil(max(y));
F=tr.ConnectivityList;
V = [tr.Points(:,1),tr.Points(:,2)];
x = V (:, 1);y = V (:, 2);
x = x(F)';y = y(F)'; nedges = size(x,2);
x = [x; NaN(1,nedges)]; y = [y; NaN(1,nedges)];
x = x(:); y = y(:); % nan separates triangles
x(x<1) = 1;
y(y<1) = 1;
columns = ceil(max(x));
rows = ceil(max(y));
mask = false(rows, columns);
tic
% k = 3:4:length(x);
k = 3:4:length(x);
for f = 1:length(k);
% f
x1 = x(k(f)-2);
x2 = x(k(f));
y1 = y(k(f)-2);
y2 = y(k(f));
numPointsAlongLine = 2.5 * sqrt((x2-x1)^2+(y2-y1)^2);
xLine = linspace(x1, x2, numPointsAlongLine);
yLine = linspace(y1, y2, numPointsAlongLine);
% Round to integer rows and columns
xLine = round(xLine);
yLine = round(yLine);
for p = 1 : length(xLine)
mask(yLine(p), xLine(p)) = true;
% imshow(mask, []);
end
% if mod(f, 500)
% imshow(mask, []);
% drawnow;
% end
end
imshow(mask, []);
toc
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
But it looks like it's missing some edges:

Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!