problem in perimeter
조회 수: 1 (최근 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
댓글 수: 0
채택된 답변
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
추가 답변 (3개)
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!