Skip to content

Instantly share code, notes, and snippets.

@Ziggoto
Last active August 15, 2018 18:04
Show Gist options
  • Save Ziggoto/69372ca63e28cc8e0692 to your computer and use it in GitHub Desktop.
Save Ziggoto/69372ca63e28cc8e0692 to your computer and use it in GitHub Desktop.
Algoritimo de Fecho Convexo de imagens binárias
img = [
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 1 1 0 0 0 0 0;
0 0 0 1 1 1 1 1 0 0 0 0;
0 0 0 1 1 1 1 0 0 0 0 0;
0 0 0 0 0 1 1 0 1 0 0 0;
0 0 0 0 0 1 1 0 1 0 0 0;
0 0 0 0 0 1 1 1 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 1 1 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
];
s = size(img);
b1 = [1 0 0; 1 0 0; 1 0 0];
b2 = [1 1 1; 0 0 0; 0 0 0];
b3 = [0 0 1; 0 0 1; 0 0 1];
b4 = [0 0 0; 0 0 0; 1 1 1];
bc = [0 0 0; 0 1 0; 0 0 0];
function y = thitmiss(matrix, hit, miss)
parte1 = imerode(matrix, hit);
parte2 = imerode(~matrix, miss);
y = parte1 & parte2;
endfunction
a = thitmiss(img, b1, bc) | img;
a(:,1) = 0;
while(1>0)
a1 = thitmiss(a, b1, bc) | a;
a1(:,1) = 0;
if(a == a1)
break;
endif
a = a1;
end
c = thitmiss(img, b2, bc) | img;
c(1,:) = 0;
while(1>0)
c1 = thitmiss(c, b2, bc) | c;
c1(1,:) = 0;
if(c == c1)
break;
endif
c = c1;
end
d = thitmiss(img, b3, bc) | img;
d(:,s(2)) = 0;
while(1>0)
d1 = thitmiss(d, b3, bc) | d;
d1(:,s(2)) = 0;
if(d == d1)
break;
endif
d = d1;
end
e = thitmiss(img, b4, bc) | img;
e(s(1),:) = 0;
while(1>0)
e1 = thitmiss(e, b4, bc) | e;
e1(s(1),:) = 0;
if(e == e1)
break;
endif
e = e1;
end
fecho = a1 | c1 | d1 | e1;
figure, imshow(fecho);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment