Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % bases.m
- % Hugo Gallée
- % 24/02/2012
- clear all; close all; % ferme tout et clear les variables
- % Ouvre l'image
- myImage = imread('Chocolat.jpg');
- myImage = double(myImage);
- [Nlin, Ncol] = size(myImage);
- figure(1);
- image(myImage);
- colormap(gray(256));
- axis('image');
- % % image inverse (negatif)
- % myImageInverted = 255 - myImage;
- % figure(2);
- % image(myImageInverted);
- % colormap(gray(256));
- % axis('image');
- % % Selection et affichage d'une zone
- % % Using ginput
- % figure(1);
- % [x, y] = ginput(2);
- % x = round(x);
- % y = round(y);
- % detail = myImage( y(1) : y(2), x(1) : x(2));
- % figure(3);
- % image(detail);
- % colormap(gray(256));
- % axis('image');
- %
- % % Using imcrop
- % figure(2)
- % detail2 = imcrop;
- % figure(4);
- % image(detail2);
- % colormap(gray(256));
- % axis('image');
- h = histogramme(myImage); % Mise en vecteur
- histo = hist(h,256); % Histogramme de l'image
- histo = histo / (Nlin * Ncol); % Normalisation de l'histogramme (Integrale / somme = 1)
- %figure(11);
- %plot(histo);
- % Seuillage selon la méthode de Fisher (pas vue en cours ...)
- % http://bnazarian.free.fr/MyUploads/IN_GBM_05_SEGMENTATION.PDF
- % voir p.5
- seuilMin = Inf;
- WMin = Inf;
- for seuil=1:255 % Il faut essayer chaque seuil possible pour trouver le meilleur
- t0 = 0; % Integrale / somme de la 1ere partie
- t1 = 0; % Integrale / somme de la 2eme partie
- m0 = 0; % Moyenne de la 1ere partie
- m1 = 0;
- v0 = 0; % Variance de la 1ere partie (en gros c'est le coefficient d'hétérogénéité de cette partie)
- v1 = 0;
- for j = 1:seuil
- t0 = t0 + histo(j);
- m0 = m0 + (j * histo(j));
- end
- for j = seuil+1:256
- t1 = t1 + histo(j);
- m1 = m1 + (j * histo(j));
- end
- m0 = m0 / t0;
- m1 = m1 / t1;
- for j = 1:seuil
- v0 = v0 + (histo(j) * (j - m0)^2);
- end
- for j = seuil+1:256
- v1 = v1 + (histo(j) * (j - m1)^2);
- end
- v0 = v0 / t0;
- v1 = v1 / t1;
- W = t0 * v0 + t1 * v1;
- if (W < WMin)
- WMin = W;
- seuilMin = seuil;
- end
- end
- seuil = seuilMin;
- % On binarise l'image
- imageBinaire = zeros(Nlin,Ncol);
- for lin=1:Nlin
- for col=1:Ncol
- if (myImage(lin,col) < seuil)
- imageBinaire(lin,col) = 1;
- end
- end
- end
- figure(21);
- image(uint8(imageBinaire));
- colormap(gray(2));
- axis('image');
- % imageDilatee = bwmorph(imageBinaire, 'dilate');
- % figure(22);
- % image(uint8(imageDilatee));
- % colormap(gray(2));
- % axis('image');
- %
- % imageErodee = bwmorph(imageBinaire, 'erode');
- % figure(23);
- % image(uint8(imageErodee));
- % colormap(gray(2));
- % axis('image');
- %
- % imageOuverte = bwmorph(imageBinaire, 'open');
- % figure(25);
- % image(uint8(imageOuverte));
- % colormap(gray(2));
- % axis('image');
- imageFermee = bwmorph(imageBinaire, 'close'); % Fermeture de l'image
- % figure(24);
- % image(uint8(imageFermee));
- % colormap(gray(2));
- % axis('image');
- % On compte les labels (formes)
- [imageLabels, nbLabels] = bwlabel(imageFermee);
- imageColoree = ind2rgb(imageLabels + 1, [0 0 0; lines(nbLabels)]);
- figure(31);
- imshow(imageColoree);
- axis('image');
- % Pour chaque label créer une image séparée
- for j=1:nbLabels
- [Coo_lin,Coo_col] = find(imageLabels == j);
- % Sauter cette itération si l'image est trop petite (artefact)
- if max(Coo_lin) - min(Coo_lin) <= 10 || max(Coo_col) - min(Coo_col) <= 10
- continue
- end
- choc = ones(max(Coo_lin) - min(Coo_lin) + 3,max(Coo_col) - min(Coo_col) + 3) * 255;
- for i=1:size(Coo_lin)
- choc(Coo_lin(i) - min(Coo_lin) + 2,Coo_col(i) - min(Coo_col) + 2) = myImage(Coo_lin(i),Coo_col(i));
- end
- figure(50 + j);
- image(choc);
- colormap(gray(256));
- axis('image');
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement