source: cherche_centre/04_cherche_centre.py @ 34

Last change on this file since 34 was 28, checked in by meynadie, 16 years ago

ajout scripts centrage

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#!/bin/env python
2# encoding: utf-8
3
4import pyfits
5from optparse import OptionParser
6
7import numpy
8from scipy import *
9
10parser = OptionParser()
11
12parser.add_option("-t","--threshold",dest="threshold", default=0.5,
13                  help="threshold for edge detection (relative to max, default 0.5)")
14
15parser.add_option("-w","--width",dest="hw_fen_int", default=5,
16                  help="window for measuring central intensity, size will be 2*hw_fen_int+1")
17
18(options, args) = parser.parse_args ()
19
20if (len(args) != 1):
21    print "Usage : cherche_centre.py [-t threshold] [-w width] image.fits"
22    exit(1)
23
24
25# Ouverture de l'image fits en argument
26hdulist = pyfits.open(args[0])
27scidata = hdulist[0].data
28
29
30# Estimation fond (4 coins)
31tf = 256 # taille fond, en pix
32fond_bas = .5 * (mean(scidata[:tf,:tf]) + mean(scidata[:tf,2048-tf:]))
33fond_haut= .5 * (mean(scidata[2048-tf:,:tf]) + mean(scidata[2048-tf:,2048-tf:]))
34
35
36if (abs(fond_haut - fond_bas) > .5 * (fond_haut + fond_bas) ):
37    print "Ereur : offsets déséquilibrés. Corriger le fond."
38    exit(3)
39
40def filtre_median(profil,hw):
41    tmp = numpy.zeros(2048)
42    for i in range(hw,2048-hw-1):
43        tmp[i] = median(profil[i-hw:i+hw+1])
44    return tmp
45
46# Moyenne suivant lignes et colonnes
47moyenne_h = filtre_median(sum(scidata, axis=0) / 2048.,5) 
48moyenne_v = filtre_median(sum(scidata, axis=1) / 2048.,5)
49
50
51
52of = open("profils"+args[0][:-5]+".csv",'w')
53for i in range(2048):
54    of.write("%4i %f %f\n" % (i, moyenne_h[i], moyenne_v[i]))
55of.close()
56
57def get_rayon_centre(profil):
58    # Détermination du seuil, ici 10%
59    seuil = max(profil) * float(options.threshold)
60    # Méthode bête
61    bord1 = 0
62    for i in range(2047):
63        if ((profil[i] < seuil) and (profil[i+1] >= seuil)):
64            bord1 = i
65
66    bord2 = 2048
67    for i in range(2047,1,-1):
68        if((profil[i] < seuil) and (profil[i-1] >= seuil)):
69            bord2 = i
70
71    flag=""
72    if ((bord1 == 0) or (bord2 == 2048)) :
73        flag = "*"
74        print "Attention ! un seul bord détecté !"
75        rayon = 0
76        centre = 0
77    else:
78        rayon = (bord2 - bord1) / 2.
79        centre = (bord1 + bord2) / 2.
80    return (rayon,centre,flag)
81
82
83(rayon_h,centre_h,flag) = get_rayon_centre(moyenne_h)
84(rayon_v,centre_v,flag) = get_rayon_centre(moyenne_v)
85
86
87hw_fen_int = int(options.hw_fen_int)
88
89int_centre = sum(sum(scidata[int(centre_v)-hw_fen_int:int(centre_v)+hw_fen_int+1,
90                             int(centre_h)-hw_fen_int:int(centre_h)+hw_fen_int+1]))/((2*hw_fen_int + 1)**2)
91                             
92
93
94print "%13s %4i %6.1f %4i %6.1f %6.0f %1s" % (args[0],
95                                              rayon_h,
96                                              centre_h+1, # fits commence à 1
97                                              rayon_v,
98                                              centre_v+1, # fits commence à 1
99                                              int_centre,
100                                              flag)
Note: See TracBrowser for help on using the repository browser.