        subroutine centre_gravite(coord,image,nx,ny,dimxx,dimyy,aire_max,fene,valeur,cgravx,cgravy)

            implicit none

            integer      valeur,posx,posy,xmin,xmax,ymin,ymax,fene
            integer      xl,yl,dimxx,dimyy,aire_max,nx,ny

            real         cgravx, cgravy, XG,YG,compt
            real image(nx,ny),coord(2),obj(2*fene+1,2*fene+1)

            posx=coord(1) 
            posy=coord(2) 
              
            xmin=posx-fene      
            xmax=posx+fene      
            ymin=posy-fene       
            ymax=posy+fene       
       
            if(xmin .lt. 1)  xmin=1 
            if(xmax .gt. nx) xmax=nx
            if(ymin .lt. 1)  ymin=1 
            if(ymax .gt. ny) ymax=ny 

           obj=0
           obj=image(xmin:xmax,ymin:ymax)  

            compt=0.

            XG=0.
            YG=0.
            cgravx=0
            cgravy=0
  
           do yl=1,dimyy 
             do xl=1,dimxx    
              if(obj(xl,yl) .eq. valeur) then 
                XG= real(xl)+xmin-1+ XG
                YG= real(yl)+ymin-1+ YG                 
                compt= compt +1
                if ( compt .gt. aire_max-1) goto 10  
              endif
             end do      
           end do

 10       continue

          cgravx=(XG/compt)  
          cgravy=(YG/compt) 

         end subroutine
