필터 지우기
필터 지우기

from 0 1 matrix to boundaries and vertices

조회 수: 4 (최근 30일)
Andrea Somma
Andrea Somma 2022년 12월 19일
편집: Matt J 2022년 12월 19일
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
  댓글 수: 1
Andrea Somma
Andrea Somma 2022년 12월 19일
Thank you! I will download image processing toolbox and will try it

댓글을 달려면 로그인하십시오.

추가 답변 (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