Convert mesh to BW image?

4 次查看(过去 30 天)
Ivan Shorokhov
Ivan Shorokhov 2016-3-1
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 个评论
Image Analyst
Image Analyst 2016-3-1
Nothing is attached. Have you tried passing the coordinates into poly2mask()?
Ivan Shorokhov
Ivan Shorokhov 2016-3-1
编辑:Ivan Shorokhov 2016-3-1
@Image Analyst Please check again. I will try poly2mask() now.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2016-3-1
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 个评论
Ivan Shorokhov
Ivan Shorokhov 2016-3-4
编辑:Ivan Shorokhov 2016-3-4
@Image Analyst, Elapsed time is 1.574048 seconds. Any ideas how I can speed it up?
Image Analyst
Image Analyst 2016-3-4
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:

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by