필터 지우기
필터 지우기

Convert mesh to BW image?

조회 수: 4 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2016년 3월 1일
댓글: Image Analyst 2016년 3월 4일
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