フィルターのクリア

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 Center および File ExchangeImage Filtering and Enhancement についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by