problem in perimeter

1 回表示 (過去 30 日間)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 11 月 30 日
hi,
I have a matrix as follows:
I =
0 0 0 0 0 0
0 0 1 1 0 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 1 1 0 0
0 0 0 0 0 0
I want to have another matrix as follows
I =
0 2 2 2 2 0
2 2 1 1 2 2
2 1 1 1 1 2
2 1 1 1 1 2
2 2 1 1 2 2
0 2 2 2 2 0
i.e. replace every position with 2 within distance 1 from the perimeter of region 1. Thanks

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2011 年 11 月 30 日
Sorry. Here I modified my code
clear all; clc
I =[0 0 1 1 0 0;
0 1 1 1 1 0;
0 1 1 1 1 0;
0 0 1 1 0 0;
0 0 0 0 0 0];
J = zeros(size(I,1)+2, size(I,2)+2);
K = zeros(size(I,1)+2, size(I,2)+2);
J(2:size(J,1)-1, 2:size(J,2)-1) = I;
for x = 2 : size(J,1)-1
for y = 2 : size(J,2)-1
neighbour = [J(x-1,y-1) J(x-1,y) J(x-1,y+1) ...
J(x,y-1) J(x,y+1) ...
J(x+1,y-1) J(x+1,y) J(x+1,y+1)];
if (find(neighbour))
K(x,y) = 2;
end
end
end
L = K - J;
L(1,:) = []; L(end,:) = [];
L(:,1) = []; L(:,end) = []
And the result :
L =
2 2 1 1 2 2
2 1 1 1 1 2
2 1 1 1 1 2
2 2 1 1 2 2
0 2 2 2 2 0
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 11 月 30 日
Thanks

サインインしてコメントする。

その他の回答 (3 件)

Chandra Kurniawan
Chandra Kurniawan 2011 年 11 月 30 日
clear all; clc;
I =[0 0 0 0 0 0;
0 0 1 1 0 0;
0 1 1 1 1 0;
0 1 1 1 1 0;
0 0 1 1 0 0;
0 0 0 0 0 0];
J = zeros(size(I,1)+2, size(I,2)+2);
K = zeros(size(I,1)+2, size(I,2)+2);
J(2:7,2:7) = I;
for x = 2 : 7
for y = 2 : 7
neighbour = [J(x-1,y-1) J(x-1,y) J(x-1,y+1) ...
J(x,y-1) J(x,y+1) ...
J(x+1,y-1) J(x+1,y) J(x+1,y+1)];
if (find(neighbour))
K(x,y) = 2;
end
end
end
L = K - J;
L(1,:) = []; L(end,:) = [];
L(:,1) = []; L(:,end) = []
And you will get L =
L =
0 2 2 2 2 0
2 2 1 1 2 2
2 1 1 1 1 2
2 1 1 1 1 2
2 2 1 1 2 2
0 2 2 2 2 0
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 11 月 30 日
but if my matrix is as follows:
I =[0 0 1 1 0 0;
0 1 1 1 1 0;
0 1 1 1 1 0;
0 0 1 1 0 0;
0 0 0 0 0 0];
It does not work, how to extend your idea for any matrix I give, I am not good in matlab. could u help me in this regard.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2011 年 11 月 30 日
variant use conv2 without Image Processing Toolbox
t = conv2(I,ones(3),'same')>0
out = t + 0
out(t>0&t~=I) = 2
or
out = 2*(conv2(I,ones(3),'same')>0+0)-I
variant use with function imdilate by Image Processing Toolbox
out = imdilate(I,ones(3))
out(out~=I) = 2
or
out = 2*imdilate(I,ones(3))-I
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 12 月 1 日
It also works fine

サインインしてコメントする。


Image Analyst
Image Analyst 2011 年 11 月 30 日
If you have the Image Processing Toolbox you can call imdilate() and then bwperim() and then combine the perimeter image with the original by multiplying the perimeter image by 2 and adding to the original image.
  1 件のコメント
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 12 月 1 日
would u please mention the code, since the parameter of imdilate is not clear to me.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Segmentation and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by