• [^] # Re: Détecter des formes

    Posté par (site web personnel) . En réponse au journal G'MIC 1.0.0 : Un outil extensible pour le traitement d'images.. Évalué à 4.

    un petit bout de code python qui fait ça (pour une ellipse ) un rapide topo, ça me sert à détecter les étoiles (gaussienne 2D) sur une image. (un "champ" )
    def select_star(self):
     """select a star in the field. Star choosen is the best correlation with predefined star"""
     
     self.star_ref=Star_Ref(self)
     try : 
     champ = self.dic[self.star.number] #picture shown
     except KeyError :
     champ = self.star.image
     
     champ,X,Y = self.qimage2numpy(champ) #make a numpy array from image
     #initialise la liste des coeff
     b =champ.shape
     champ.shape = X,Y
     #tout se joue ici : self.star_ref.data est le tableau qui contient ma forme de référence(pour toi : ellipse, carré, cercle..ce que tu veux)
     list_correl=self.iter_correl(champ,0,0, 200,200,6,self.star_ref.data) #regarde plus bas la fonction
     for i in arange(0,len(list_correl)):
     list_correl[i]=abs(list_correl[i])
     max_correl = max(list_correl)
     index_max = list_correl.index(max(list_correl))
     largeur_matrice = sqrt(len(list_correl))
    #petit trafic pour trouver le centre de l'ellipse
     X_centre = int(index_max/largeur_matrice)+10
     Y_centre = index_max-(X_centre-10)*largeur_matrice +10
     
     if max_correl>0.65: #je considère que si c'est plus petit que 0.65...c'est pas la même forme
     params = self.correl(list_correl,champ)
     self.star.centre_abs = (Y_centre, X_centre) #tu t'en fous
     #tu t'en fous height, center_x, center_y, width_x, width_y = params
    #tu t'en fous width = (width_x+ width_y)/2
    #tu t'en fous self.star.fwhm = 2.355*width
    #tu t'en fous self.star.width = width
    #tu t'en fous self.ui.label_fwhm.setText(self.tr("FWHM = "+str("%.0f") % self.star.fwhm + "px"))
    #tu t'en fous self.expose_Capture("actual", self.star.image, self.star.number)
    #tu t'en fous a=star_draw(self.ui.label_actual_pic, self.star)
    #tu t'en fous #print "show star"
    #tu t'en fous self.stars_picture.append(a)
    #tu t'en fous a.show()
     
     else : 
     self.ui.label_fwhm.setText(self.tr("No Star detected"))
    def iter_correl(self,champ,x_begin,y_begin, x_end,y_end, pas,ref):
     list_correl=[] # je découpe mon image en faisant passer ma forme de base dessus. j'itère pas sur tous les pixels.
     for i in arange(x_begin,x_end):
     if i+21<(x_end-x_begin) : #
     for j in arange(y_begin,y_end): 
     if j+21<y_end-y_begin : 
     if i*j%pas == 0 :
     champ_crop = champ[i:i+21,j:j+21] 
     b=champ_crop.copy()
     
     try :
     b.shape=(1,21*21) 
     y2=b
     a = corrcoef(ref,y2) #le calcul se fait ici !!!
     list_correl.append(a[0,1])
     except ValueError:
     print i,j
     
     else :
     list_correl.append(0)
     return list_correl
    
    si ça peut t'aider... Note : les include : from pylab import * note : si quelqu'un veut optimiser ça je prends ;)