problem in perimeter

7 views (last 30 days)
Mohammad Golam Kibria
Mohammad Golam Kibria on 30 Nov 2011
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

Accepted Answer

Chandra Kurniawan
Chandra Kurniawan on 30 Nov 2011
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

More Answers (3)

Chandra Kurniawan
Chandra Kurniawan on 30 Nov 2011
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 Comment
Mohammad Golam Kibria
Mohammad Golam Kibria on 30 Nov 2011
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.

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 30 Nov 2011
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

Image Analyst
Image Analyst on 30 Nov 2011
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 Comment
Mohammad Golam Kibria
Mohammad Golam Kibria on 1 Dec 2011
would u please mention the code, since the parameter of imdilate is not clear to me.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!