1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
function pushbutton2_Callback(hObject1, eventdata1, handles1)
global n handles
%global vect_shapes handles;
img = imread(n);
img = rgb2ind(img,2); % ici seulement 2 couleurs (noir et blanc)
BW = (img == 0);
[L,num]=bwlabel(BW,8); % L: la matrice de l'image après étiquetage des composantes connexes
% num: le nombre de composantes connexe dans l'image
[r,c] = meshgrid(1:size(L,2),1:size(L,1));
figure;
imagesc(L); %affichage de la matrice L
hold on
text(r(:),c(:),num2str(L(:)),'horizontalalignment','center')
axis image off
BW1=(img == 1);
%parcours de tous les composantes connexes pour les encadrer
for compo=1:num
[ligne,colonne]=find(L==compo);
l_min=min(ligne);
l_max=max(ligne);
c_min=min(colonne);
c_max=max(colonne);
vect_shapes(compo) = imcrop(BW1,[c_min l_min c_max-c_min l_max-l_min]);
for i=c_min:c_max
BW1(l_min,i)=0;
BW1(l_max,i)=0;
end
for j=l_min:l_max
BW1(j,c_min)=0;
BW1(j,c_max)=0;
end
end
figure(5);
imshow(vect_shapes(1));
figure;
imshow(BW1); |
Partager