Convert mesh to BW image?
5 views (last 30 days)
Show older comments
Hello everybody,
[Problem]: I want to convert triangulated mesh to bw image.
Original Images:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/156463/image.jpeg)
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()?
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173124/image.jpeg)
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
Image Analyst
on 4 Mar 2016
That doesn't even look right. Are you sure you're using the correct triangles?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/182503/image.jpeg)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/182504/image.jpeg)
See Also
Categories
Find more on Surface and Mesh Plots 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!