Advertisement
HugoBallee

bases.m

Feb 24th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.74 KB | None | 0 0
  1. % bases.m
  2. % Hugo Gallée
  3. % 24/02/2012
  4.  
  5. clear all; close all; % ferme tout et clear les variables
  6.  
  7. % Ouvre l'image
  8. myImage = imread('Chocolat.jpg');
  9. myImage = double(myImage);
  10. [Nlin, Ncol] = size(myImage);
  11.  
  12. figure(1);
  13. image(myImage);
  14. colormap(gray(256));
  15. axis('image');
  16.  
  17. % % image inverse (negatif)
  18. % myImageInverted = 255 - myImage;
  19. % figure(2);
  20. % image(myImageInverted);
  21. % colormap(gray(256));
  22. % axis('image');
  23.  
  24.  
  25. % % Selection et affichage d'une zone
  26. % % Using ginput
  27. % figure(1);
  28. % [x, y] = ginput(2);
  29. % x = round(x);
  30. % y = round(y);
  31. % detail = myImage( y(1) : y(2), x(1) : x(2));
  32. % figure(3);
  33. % image(detail);
  34. % colormap(gray(256));
  35. % axis('image');
  36. %
  37. % % Using imcrop
  38. % figure(2)
  39. % detail2 = imcrop;
  40. % figure(4);
  41. % image(detail2);
  42. % colormap(gray(256));
  43. % axis('image');
  44.  
  45. h = histogramme(myImage); % Mise en vecteur
  46. histo = hist(h,256); % Histogramme de l'image
  47. histo = histo / (Nlin * Ncol); % Normalisation de l'histogramme (Integrale / somme = 1)
  48. %figure(11);
  49. %plot(histo);
  50.  
  51.  
  52. % Seuillage selon la méthode de Fisher (pas vue en cours ...)
  53. % http://bnazarian.free.fr/MyUploads/IN_GBM_05_SEGMENTATION.PDF
  54. % voir p.5
  55. seuilMin = Inf;
  56. WMin = Inf;
  57. for seuil=1:255 % Il faut essayer chaque seuil possible pour trouver le meilleur
  58.     t0 = 0; % Integrale / somme de la 1ere partie
  59.     t1 = 0; % Integrale / somme de la 2eme partie
  60.     m0 = 0; % Moyenne de la 1ere partie
  61.     m1 = 0;
  62.     v0 = 0; % Variance de la 1ere partie (en gros c'est le coefficient d'hétérogénéité de cette partie)
  63.     v1 = 0;
  64.     for j = 1:seuil
  65.         t0 = t0 + histo(j);
  66.         m0 = m0 + (j * histo(j));
  67.     end
  68.     for j = seuil+1:256
  69.        t1 = t1 + histo(j);
  70.        m1 = m1 + (j * histo(j));
  71.     end
  72.    
  73.     m0 = m0 / t0;
  74.     m1 = m1 / t1;
  75.    
  76.     for j = 1:seuil
  77.         v0 = v0 + (histo(j) * (j - m0)^2);
  78.     end
  79.     for j = seuil+1:256
  80.         v1 = v1 + (histo(j) * (j - m1)^2);
  81.     end
  82.    
  83.     v0 = v0 / t0;
  84.     v1 = v1 / t1;
  85.    
  86.     W = t0 * v0 + t1 * v1;
  87.     if (W < WMin)
  88.         WMin = W;
  89.         seuilMin = seuil;
  90.     end
  91. end
  92.  
  93. seuil = seuilMin;
  94. % On binarise l'image
  95. imageBinaire = zeros(Nlin,Ncol);
  96. for lin=1:Nlin
  97.     for col=1:Ncol
  98.         if (myImage(lin,col) < seuil)
  99.             imageBinaire(lin,col) = 1;
  100.         end
  101.     end
  102. end
  103.  
  104. figure(21);
  105. image(uint8(imageBinaire));
  106. colormap(gray(2));
  107. axis('image');
  108.  
  109. % imageDilatee = bwmorph(imageBinaire, 'dilate');
  110. % figure(22);
  111. % image(uint8(imageDilatee));
  112. % colormap(gray(2));
  113. % axis('image');
  114. %
  115. % imageErodee = bwmorph(imageBinaire, 'erode');
  116. % figure(23);
  117. % image(uint8(imageErodee));
  118. % colormap(gray(2));
  119. % axis('image');
  120. %
  121. % imageOuverte = bwmorph(imageBinaire, 'open');
  122. % figure(25);
  123. % image(uint8(imageOuverte));
  124. % colormap(gray(2));
  125. % axis('image');
  126.  
  127. imageFermee = bwmorph(imageBinaire, 'close'); % Fermeture de l'image
  128. % figure(24);
  129. % image(uint8(imageFermee));
  130. % colormap(gray(2));
  131. % axis('image');
  132.  
  133. % On compte les labels (formes)
  134. [imageLabels, nbLabels] = bwlabel(imageFermee);
  135. imageColoree = ind2rgb(imageLabels + 1, [0 0 0; lines(nbLabels)]);
  136. figure(31);
  137. imshow(imageColoree);
  138. axis('image');
  139.  
  140. % Pour chaque label créer une image séparée
  141. for j=1:nbLabels
  142.     [Coo_lin,Coo_col] = find(imageLabels == j);
  143.    
  144.     % Sauter cette itération si l'image est trop petite (artefact)
  145.     if max(Coo_lin) - min(Coo_lin) <= 10 || max(Coo_col) - min(Coo_col) <= 10
  146.         continue
  147.     end
  148.    
  149.     choc = ones(max(Coo_lin) - min(Coo_lin) + 3,max(Coo_col) - min(Coo_col) + 3) * 255;
  150.     for i=1:size(Coo_lin)
  151.         choc(Coo_lin(i) - min(Coo_lin) + 2,Coo_col(i) - min(Coo_col) + 2) = myImage(Coo_lin(i),Coo_col(i));
  152.     end
  153.    
  154.     figure(50 + j);
  155.     image(choc);
  156.     colormap(gray(256));
  157.     axis('image');
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement