Bonjour j'ai un programme qui ça marche tres bien pour un seul image mais quand j'ai utiliser toutes mes image il me donne des erreur, par fois qd j'ai fait l'exuction il me donne le resultat et quand je repete l'excution il me donne l'errer

voila mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
close all; clear all;clc;
    Matrice=[];Nbclasse=4;
    Index = [] ; 
    MeanR=[];  R=[];V=[];B=[];
    MeanV=[]; MeanB=[];Mean=[]; MeanT=[];
    %%%%%%%%%%%%
for i=1:4
     for j=1:5
 
   % seg=imread('C1-image1.tif'); %%%%%%pour une seule image
  seg=imread(sprintf('C%d-image%d.tif',i,j)); %%%%pour tout les image
  R=seg(:,:,1)
  V=seg(:,:,2);
  B=seg(:,:,3);
 
        [Matrice1 ,Index1]=matObs(seg,Matrice);
 
        %Matrice=cat(3,Matrice,Matrice1);%%%%%
        Index = [Index,Index1] ; 
       Matrice=[Matrice , Matrice1 ];
     end
end
 
 disp('la matrice d observation');
 Matrice
 Index;
 n1=23;%%%%% la taille de la fenetere 
 n2=27;
 K = ones ( n1 , n2 ) ./ (n1 * n2 ) ; %%%%fentre de taille n1*n2
 %seg=seg(:,:,2);
 
 IMeansR = imfilter ( R , K) ; %%%%
 IMeansV = imfilter ( V , K) ;
 IMeansB = imfilter ( B , K) ;
for e=1:20
     for s=1:50
     [x y] = ind2sub( size(seg) , Index(s)) ;
 
     Mean1 = IMeansR(x,y) ; 
     disp('vecteur de centre de gravioté selon la composante Rouge');
     MeanR = [MeanR,Mean1] ;
 
              Mean2 = IMeansV(x,y) ; 
               disp('vecteur de centre de gravioté selon la composante Verte');
              MeanV = [MeanV,Mean2] ;
 
                       Mean3 = IMeansB(x,y) ; 
                        disp('vecteur de centre de gravioté selon la composante Bleu');
                       MeanB = [MeanB,Mean3] ; 
   %disp('le vecteur des centres de gravité des observations de la classe');
   %%%%%%%%%%%le vecteur des centres de gravité des observations de toutes les classe
    Mean=[MeanR ;MeanV ; MeanB];
 
      % MeanT=[MeanT,Mean]
     end
end
disp('le vecteur des centres de gravité des observations de la classe');
Mean
la fonction matObs
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function [Matrice1, Index1]=matObs(seg,Matrice)
%Matrice1=matObs(seg,Matrice)
 
lll=0;
 
I = rgb2gray(seg) > 0 ;
[l,c]=size(I);
Matrice1=zeros(l,c);
%imagesc (I); figure(gcf);
Mask = I > 0 ; 
 
L = bwlabel(Mask) ; 
L_final = L .* 0 ; %%% L_final contient les pixel extraire
for ns =1:max(L(:))
    L1 = L == ns ; % ns est le nombre de point actual
    L1Idx = find (L1(:)) ; 
 
    while(size(lll)<51)
        kr = fix(1+rand() * length(L1Idx)) ;
        lll2=lll ~= kr;
        [a,b]=size(find(lll2));
        [a2,b2]=size(lll);
        if (b==b2)
        lll=[lll , kr ];
         L1Idx(kr) = 0 ; 
        end
    end
 
 
      % L1Idx(L1Idx == 0 ) = [] ; %% elimination des element null
 
    L1Idx(~ L1Idx)=[];
 
    L_final (L1Idx ) = ns ; 
end
 
Mask2 = L_final > 0  ;
D = Mask == 1 & Mask2 == 0 ; % D est un mask
 
Index1 = find(D);
 
 
%%% affichage des resultat :
% subplot(221) , imshow(seg) , title('image Original '); 
% subplot(222) , imshow(Mask) , title('BW Mask'); 
% subplot(223) , imshow(D) , title(' les Points extraitre'); 
 
 
 
% on peut applique le mask D pour trouve la valur des pixels extrait
% si vous applique D a l'image originale tu px trouve la valeur initiale de
% pixel
 Drgb = seg ;
 Drgb=double(Drgb);
 Drgb(: , : , 1) = Drgb( : , : , 1 ).* D ; % size(Drgb) 150 400 ?????????
 Drgb(: , : , 2) = Drgb( : , : , 2 ) .* D ;
 Drgb(: , : , 3) = Drgb( : , : , 3 ) .* D ;
%   %% Drgb est les pixels extrait de l'image couleur
 
%   subplot(224) , imshow(Drgb) , title('intensité initiale des pixels extraire'); 
% 
M1=Drgb(:,:,1);
L1=M1(:)';
M2=Drgb(:,:,2);
L2=M2(:)';
M3=Drgb(:,:,3);
L3=M3(:)';
 X=[L1;L2;L3];
%   size(X);% 450 400  ??????????????
 
[a,b]=size(X);
 
%X(:,any(X));
%%%%%%%%%ou bien
Matrice1=X(:,~~sum(X));
voila l'erreur qui me donner:
??? Attempted to access IMeansR(21,39); index out of bounds because size(IMeansR)=[40,38].

Error in ==> centre_classe at 48
Mean1 = IMeansR(x,y) ;