problem in perimeter
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Chandra Kurniawan
il 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
Più risposte (3)
Chandra Kurniawan
il 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
Andrei Bobrov
il 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
il 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.
Vedere anche
Categorie
Scopri di più su Image Segmentation and Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!