from 0 1 matrix to boundaries and vertices

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Andrea Somma
Andrea Somma il 19 Dic 2022
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 il 19 Dic 2022
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)

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 19 Dic 2022
Modificato: Matt J il 19 Dic 2022
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
  1 Commento
Andrea Somma
Andrea Somma il 19 Dic 2022
Thank you! I will download image processing toolbox and will try it

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by