centroidWgt.c 5.54 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "nrutil.h"

//y is the input image, nx and ny are the pixels along x and y axis;
//yt and residu are the required matrix for the function
//para contains the needed parameters: centx=para[3], centy=para[4]


void star_gaus(float **y,int nx,int ny,double *para);

void main()
{
    int i, j;
    int IMAGE_WIDTH, IMAGE_HEIGHT;
    float center_x, center_y;
    float R, dis;
    float **Gauss_map;
    double *para;     
    //double para[10];     

    IMAGE_WIDTH = 180;
    IMAGE_HEIGHT = 180;
    center_x = IMAGE_WIDTH/2 -0.;
    center_y = IMAGE_HEIGHT/2+0.;

    R = sqrt(center_x*center_x + center_y*center_y);
    //Gauss_map = np.zeros((IMAGE_HEIGHT, IMAGE_WIDTH))
    Gauss_map = matrix(0, IMAGE_HEIGHT-1, 0, IMAGE_WIDTH-1);
    para = dvector(0,10);

    for(i=0; i<IMAGE_HEIGHT; i++)
    {
        for(j=0; j<IMAGE_WIDTH; j++)
        {
            //#dis = np.sqrt((i-center_y)**2+(j-center_x)**2)
            dis = (i-center_y)*(i-center_y) + (j-center_x)*(j-center_x);
            //dis = sqrt(dis);
            Gauss_map[i][j] = exp(-0.5*dis/R);
        }
    }
    star_gaus(Gauss_map, IMAGE_HEIGHT, IMAGE_WIDTH, para);
    printf("ok?\n");
    printf("para:%f %f\n", para[3], para[4]);

    //free_matrix(Gauss_map,0,IMAGE_HEIGHT-1,0,IMAGE_WIDTH-1);
    //free_dvector(para,0,4);
}


void centroidWgt(float *y, int nx, int ny, double *para)
{
  int i, j, k;
  float **yy;
  double **basef;
  double **coff;

  yy = matrix(0, nx-1, 0, ny-1);
  for(i=0; i<nx; i++)
    for(j=0; j<ny; j++)
    {
      k = j + i*nx;
      yy[i][j] = y[k];
    }

  star_gaus(yy, nx, ny, para);
}


//void star_gaus(float **y,int nx,int ny,float **yt,float **residu,double *para)
void star_gaus(float **y,int nx,int ny,double *para)
{
  void gasfit_2D(double **x, double *y,int np,double *para,double bg0, int fbg);
  double **xs,*ys,ymax;
  int i,j,np,npt,im,jm,**xi,k;
  double kc,bgc,sigmac,xc,yc,sigma2v,det;
  double bg0=0;
  int fbg=0;
  np=nx*ny;
  xs=dmatrix(0,np-1,0,1);
  ys=dvector(0,np-1);
  xi=imatrix(0,np-1,0,1);
  ymax=y[0][0];
  for(i=0;i<nx;i++)for(j=0;j<ny;j++){
      if(ymax<y[i][j]){ymax=y[i][j];im=i;jm=j;}
    }printf("x=%d,y=%d,ymax=%e\n",im,jm,ymax);

  int cutPix;
  cutPix = 23;
  npt=0;
  for(i=-cutPix;i<=cutPix;i++){
    for(j=-cutPix;j<=cutPix;j++){
      xs[npt][0]=xi[npt][0]=i+im;
      xs[npt][1]=xi[npt][1]=j+jm;
      ys[npt]=y[im+i][jm+j];
      npt++;
    }
  }
  printf("check:: %f %f %f %d\n",ys[0], ys[10], ys[100], npt );
  gasfit_2D(xs, ys,npt,para,bg0,fbg);
  kc=para[0];sigmac=para[1];bgc=para[2];xc=para[3];yc=para[4];
  printf("xc, yc: %f %f\n",yc, xc);
  //printf("%e %e %e %e %e\n",kc,sigmac,bgc,xc,yc);
  /*
  sigma2v=-1./(2.*sigmac*sigmac);
  for(i=0;i<nx;i++){
    for(j=0;j<ny;j++){
    det=DSQR(i-xc)+DSQR(j-yc);
    yt[i][j]=kc*exp(det*sigma2v)+bgc;
    residu[i][j]=y[i][j]-yt[i][j];
    }
  }
  */
  free_dmatrix(xs,0,np-1,0,1);
  free_imatrix(xi,0,np-1,0,1);
  free_dvector(ys,0,np-1);
}

void gasfit_2D(double **x, double *y,int np,double *para,double bg0,int fbg)
{
  void search_2D(double **x,double *y,int np,double kc,double kd,double sigmac,
              double sigmad,double bgc,double bgd,double xc, double xd, 
		 double yc, double yd,double *para,double bg0,int fbg);
  int i,j,k,imax=0,isigma;
  double ymax,ymin,kc,kd,sigmac,sigmad,bgc,bgd,ysigma,xc,yc,xd,yd;
  double det,dett;
  ymin=ymax=y[imax];
  for(i=1;i<np;i++){
    if(ymax<y[i]){imax=i;ymax=y[i];}
    if(ymin>y[i])ymin=y[i];
    
  }
  //printf("ymax=%e,imax=%d\n",ymax,imax);
  kc=ymax;kd=ymax/12.;
  det=ysigma=kc*exp(-0.5);//printf("det=%e,kc=%e\n",det,kc);
  for(i=0;i<np;i++){
    dett=fabs(ysigma-y[i]);//printf("dett=%e,det=%e,i=%d\n",dett,det,i);
    //if(dett<det && y[i]!=kc){det=dett;isigma=i;}
    if(dett<det){det=dett;isigma=i;}
  }
  xc=x[imax][0];yc=x[imax][1];printf("isigma=%d,imax=%d\n",isigma,imax);
  sigmac=sqrt(DSQR(xc-x[isigma][0])+DSQR(yc-x[isigma][1]))*1.;
  xd=yd=sigmac*0.25;
  sigmad=0.25*sigmac;
  bgc=0.;bgd=fabs(ymin);
  //printf("sigma=%e\n",sigmac);
  for(i=0;i<4;i++){
    printf("i = %d\n", i);
    //printf("%e %e %e %e %e k2=%e\n",kc,sigmac,bgc,xc,yc,para[5]);
    search_2D(x,y,np,kc,kd,sigmac,sigmad,bgc,bgd,xc,xd,yc,yd,para,bg0,fbg);
    kd*=0.33;sigmad*=0.33;bgd*=0.33;xd*=0.33;yd*=0.33;
    kc=para[0];sigmac=para[1];bgc=para[2];xc=para[3];yc=para[4]; 
    
  }
  if(fbg==0)para[2]=bg0;
}
void search_2D(double **x,double *y,int np,double kc,double kd,double sigmac,
	       double sigmad,double bgc,double bgd,double xc, double xd, 
	       double yc, double yd,double *para,double bg0,int fbg)
{
  double k,sigma,bg,k2,k20,sigma2v,det,xt,yt;
  int i,j,l,m,p,q;
  sigma2v=-1./(2.*sigmac*sigmac);
  bg=bgc;
  k20=0;
  for(m=0;m<np;m++){
    det=DSQR(x[m][0]-xc)+DSQR(x[m][1]-yc);
    det=kc*exp(det*sigma2v)+bgc-y[m];
    k20+=det*det;
  }             
  for(i=-4;i<=4;i++){
    k=kc+i*kd;
    if(k>0){
      for(j=-4;j<=4;j++){
	sigma=sigmac+j*sigmad;
	if(sigma>0){
	  sigma2v=-1./(2.*sigma*sigma);
	  for(p=-4;p<=4;p++){
	    xt=xc+p*xd;
	    for(q=-4;q<=4;q++){
	      yt=yc+q*yd;
	      k2=0;
	      if(fbg==0){
		bg=bg0;
		for(m=0;m<np;m++){
		  det=DSQR(x[m][0]-xt)+DSQR(x[m][1]-yt);
		  det=k*exp(det*sigma2v)+bg-y[m];
		  k2+=det*det;
		}
	      }
	      else{
		for(l=-4;l<=4;l++){
		  bg=bgc+l*bgd;
		  for(m=0;m<np;m++){
		    det=DSQR(x[m][0]-xt)+DSQR(x[m][1]-yt);
		    det=k*exp(det*sigma2v)+bg-y[m];
		    k2+=det*det;
		  }
		}
	      }
	      //printf("k20=%e k2=%e\n",k20,k2);
	      if(k2<=k20){k20=k2;para[5]=k2;
		para[0]=k;para[1]=sigma;para[2]=bg;para[3]=xt;para[4]=yt;}
	    }
	  }
	}	  
      }
    }
  }
}