Convert mesh to BW image?

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

Nothing is attached. Have you tried passing the coordinates into poly2mask()?
Ivan Shorokhov
Ivan Shorokhov on 1 Mar 2016
Edited: Ivan Shorokhov on 1 Mar 2016
@Image Analyst Please check again. I will try poly2mask() now.

Sign in to comment.

Answers (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 Comments

Ivan Shorokhov
Ivan Shorokhov on 1 Mar 2016
Edited: Ivan Shorokhov on 1 Mar 2016
@Image Analyst, Thank you so much, I will try it tomorrow morning and let you know, if it is working or not and Accept your answer. Thank you one more time!
Ivan Shorokhov
Ivan Shorokhov on 2 Mar 2016
Edited: Ivan Shorokhov on 2 Mar 2016
@Image Analyst,now I see what you mean in terms of active contour. But just now I want to burn tr.ConnectivityList, tr.Points into black background. The method you have showed above is using only Points. Any ideas how I can do it?
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.
Hello @Image Analyst,
I found the way of doing it, I'm burning each triangle one by one, by using poly2mask function.
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
columns = ceil(max(x)); rows = ceil(max(y));
k = 3:4:length(x);
mask2 = zeros(rows,columns, length(k));
for f = 1:length(k);
x12 = x((k(f)-2):k(f)); y12 = y((k(f)-2):k(f));
mask2(:,:,f) = poly2mask(x12, y12,rows,columns);
end
reconstructed1 = sum(double(mask2), 3);
I would appreciate a lot, if you could modify the code, to make it more efficient as just now it is a bit slow.
Thank you for all your help!
Image Analyst
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
Ivan Shorokhov on 4 Mar 2016
Edited: Ivan Shorokhov on 4 Mar 2016
@Image Analyst, Elapsed time is 1.574048 seconds. Any ideas how I can speed it up?
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:

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 1 Mar 2016

Commented:

on 4 Mar 2016

Community Treasure Hunt

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

Start Hunting!