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!