from 0 1 matrix to boundaries and vertices

4 次查看(过去 30 天)
I have a matrix like the one is attached here I want to overwrite on the ones:
  • 2 if the cell is a edge
  • 3 if the cell is a vertex
like from:
1 1 1 1 1
1 1 1 1 1
0 1 1 1 0
to:
3 2 2 2 3
3 3 1 3 3
0 3 2 3 0
any idea?
  3 个评论
Andrea Somma
Andrea Somma 2022-12-19
like a polygon if the line breaks then is a vertex, otherwise if the line of 1 is near a line of zeros then is a edge like this:
from:
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 0
to:
3 2 2 2 2 2 2 3
2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2
3 2 3 3 2 2 2 3
0 0 2 2 0 0 0 0
0 0 3 3 0 0 0 0
Andrea Somma
Andrea Somma 2022-12-19
this is a bit involved but works let me know if someone can come up with a faster solution
load("matlab.mat")
nx = size(domain,2);
ny = size(domain,1);
% storing old domain
odomain = domain;
%% matrix boundaries
ev = zeros(size(domain));
for i = 2:nx-1
j = 1;
if domain(j,i) == 1
ev(j,i) = 1;
end
if domain(j,i) == 1 && (domain(j,i-1)==0 || domain(j,i+1)==0)
ev(j,i) = 2;
end
end
for i = 2:nx-1
j = ny;
if domain(j,i) == 1
ev(j,i) = 1;
end
if domain(j,i) == 1 && (domain(j,i-1)==0 || domain(j,i+1)==0)
ev(j,i) = 2;
end
end
for i = 2:ny-1
j = 1;
if domain(i,j) == 1
ev(i,j) = 1;
end
if domain(i,j) == 1 && (domain(i-1,j)==0 || domain(i+1,j)==0)
ev(i,j) = 2;
end
end
for i = 2:ny-1
j = nx;
if domain(i,j) == 1
ev(i,j) = 1;
end
if domain(i,j) == 1 && (domain(i-1,j)==0 || domain(i+1,j)==0)
ev(i,j) = 2;
end
end
if domain(1,1)==1
ev(1,1) = 2;
end
if domain(1,nx)==1
ev(1,nx) = 2;
end
if domain(ny,1)==1
ev(ny,1) = 2;
end
if domain(ny,nx)==1
ev(ny,nx) = 2;
end
%% internal points
for i = 2:nx-1
for j = 2:ny-1
if sum([domain(j-1,i) domain(j+1,i) domain(j,i-1) domain(j,i+1) ...
domain(j-1,i+1) domain(j+1,i-1) domain(j-1,i-1) domain(j+1,i+1)]) == 5
ev(j,i) = 1;
elseif sum([domain(j-1,i) domain(j+1,i) domain(j,i-1) domain(j,i+1) ...
domain(j-1,i+1) domain(j+1,i-1) domain(j-1,i-1) domain(j+1,i+1)]) == 6
ev(j,i) = 1;
elseif sum([domain(j-1,i) domain(j+1,i) domain(j,i-1) domain(j,i+1) ...
domain(j-1,i+1) domain(j+1,i-1) domain(j-1,i-1) domain(j+1,i+1)]) == 7
ev(j,i) = 2;
end
end
end
%% resulting domain
domain = domain + ev;
domain(odomain<1) = 0;
imagesc(odomain)
figure
imagesc(domain)

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2022-12-19
编辑:Matt J 2022-12-19
A=load('domain').domain; A=imresize(A,1/20);
BW=logical(A);
A(BW)=2;
BW=bwmorph(BW,'remove');
A(A&~BW)=1;
BW=edgeLengthen(BW,5,0)+edgeLengthen(BW,5,1)>0;
v=bwmorph(BW,'branchpoints');
A(v(2:end-1,2:end-1))=3; %final result
imshow(A,[])
function BW=edgeLengthen(BW,n,rowwise)
BW=padarray(BW,[1,1]);
se0=ones(1,n-2);
se1=ones(1,n+2);
if ~rowwise, se0=se0'; se1=se1'; end
BW=imerode(BW,se0);
BW=imdilate(BW,se1);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Filtering and Enhancement 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by