Esempio n. 1
0
void bprint_ellipse3(fpnum *mvec,unsigned char *pvec){

	fpnum cogx,cogy;
	fpnum cov[]={0,0,0,0};
	fpnum D[]={0,0};
	fpnum E[]={0,0,0,0};

	cogx=mvec[1];
	cogy=mvec[2];
	cov[0]=mvec[3];
	cov[1]=mvec[4];
	cov[2]=mvec[4];
	cov[3]=mvec[5];
	/* Decompose covariance matrix */
	//D is eigenvalues (length 2)
	//E is eigenvectors (length 4)
	
	eigendec(cov,D,E);
	
	
	printf("Ellipse pos [%f,%f] D=[%f,%f] E=[%f,%f,%f,%f]\n",cogx,cogy,D[0],D[1],E[0],E[1],E[2],E[3]);

}
Esempio n. 2
0
/*
 * Compute ratios of mask pixels inside ellipses
 *
 * bf_mask    Binary mask
 * bf_mvec    Input mvec (6xN)
 * bf_ratios  Resultant ratios
 *
 */
void inside_mask_ratios(ibuffer *bf_mask,buffer *bf_mvec,buffer *bf_ratios) {
int nblobs,k,area_e,area_em,rows,cols;
fpnum *ratios,*mvec,*cmvec;
int *mask;
int x,y,xl,xh,yl,yh;
fpnum xmax,ymax,xd,yd,qf,qf0,xd2a12;
fpnum cogx,cogy;
fpnum cov[]={0,0,0,0};
fpnum D[]={0,0};
fpnum E[]={0,0,0,0};
fpnum a11,a12,a22;

  nblobs = bf_mvec->cols; 
  mvec   = bf_mvec->data;
  ratios = bf_ratios->data;
  mask   = bf_mask->data;
  rows   = bf_mask->rows;
  cols   = bf_mask->cols;

  for(k=0;k<nblobs;k++) {
    /* Reset area counts */
    area_em=area_e=0;
    
    /* Extract centroid and covariance matrix */
    cmvec  = &mvec[k*6];
    cogx   = cmvec[1];
    cogy   = cmvec[2];
    cov[0] = cmvec[3];
    cov[1] = cmvec[4];
    cov[2] = cmvec[4];
    cov[3] = cmvec[5];
    /* Decompose covariance matrix */
    eigendec(cov,D,E);
    
    xmax=2.0*sqrt(D[0]*SQR(E[0])+D[1]*SQR(E[2]));
    /* Interval to check, with matlab offset removed */
    xl=(int)ceil(cogx-xmax-1.0);if(xl<0) xl=0;
    xh=(int)floor(cogx+xmax-1.0);if(xh>cols-1) xh=cols-1;
    ymax=2.0*sqrt(D[0]*SQR(E[1])+D[1]*SQR(E[3]));
    /* Interval to check, with matlab offset removed */
    yl=(int)ceil(cogy-ymax-1.0);if(yl<0) yl=0;
    yh=(int)floor(cogy+ymax-1.0);if(yh>rows-1) yh=rows-1;
    
    /* A= .25*E*D^-1*E' */
    a11=.25*(E[0]*E[0]/D[0]+E[2]*E[2]/D[1]);
    a12=.25*(E[1]*E[0]/D[0]+E[2]*E[3]/D[1]);
    /* a21=.25*(E[0]*E[1]/D[0]+E[2]*E[3]/D[1]);*/
    a22=.25*(E[1]*E[1]/D[0]+E[3]*E[3]/D[1]);
    
    /* (x-m)'*A*(x-m)<1 */
    for(x=xl;x<=xh;x++) {
      xd=(fpnum)x-cogx+1.0;   /* Add matlab offset again */
      qf0=xd*xd*a11;
      xd2a12=xd*2*a12;
      for(y=yl;y<=yh;y++) {
	yd=(fpnum)y-cogy+1.0; /* Add matlab offset again */
	qf=qf0;
	qf+=yd*(xd2a12+yd*a22);
	if(qf<1.0) {
	  /* We're now inside ellipse */
	  area_e++;
	  if(mask[x*rows+y]>0) area_em++;
	}
      }
    }
    
    /* Store ration of areas */
    ratios[k]=(fpnum)area_em/area_e;
  }
}
Esempio n. 3
0
/*
** Draw a filled ellipse in an uint8 RGB image.
**
** bf_img  Image buffer to paint in
** mvec    Moment list (6x1)
** pvec    Property (colour) list (3x1)
**
*/
void bdraw_ellipse3(bbuffer *bf_img,fpnum *mvec,unsigned char *pvec)
{
int x,y,k;
int rows,cols,pixels,ndim;
int xl,xh,yl,yh;
int cind;
fpnum xmax,ymax,xd,yd,qf,qf0,xd2a12;
fpnum cogx,cogy;
fpnum cov[]={0,0,0,0};
fpnum D[]={0,0};
fpnum E[]={0,0,0,0};
fpnum a11,a12,a22;
unsigned char *img;

  rows=bf_img->rows;
  cols=bf_img->cols;
  ndim=bf_img->ndim;
  pixels=rows*cols;
  img=bf_img->data;

  /* Extract centroid and covariance matrix */
  cogx=mvec[1];
  cogy=mvec[2];
  cov[0]=mvec[3];
  cov[1]=mvec[4];
  cov[2]=mvec[4];
  cov[3]=mvec[5];
  /* Decompose covariance matrix */
  eigendec(cov,D,E);

  xmax=2.0*sqrt(D[0]*SQR(E[0])+D[1]*SQR(E[2]));
  /* Interval to check, with matlab offset removed */
  xl=(int)ceil(cogx-xmax-1.0);if(xl<0) xl=0;
  xh=(int)floor(cogx+xmax-1.0);if(xh>cols-1) xh=cols-1;
  ymax=2.0*sqrt(D[0]*SQR(E[1])+D[1]*SQR(E[3]));
  /* Interval to check, with matlab offset removed */
  yl=(int)ceil(cogy-ymax-1.0);if(yl<0) yl=0;
  yh=(int)floor(cogy+ymax-1.0);if(yh>rows-1) yh=rows-1;

  /* A= .25*E*D^-1*E' */
  a11=.25*(E[0]*E[0]/D[0]+E[2]*E[2]/D[1]);
  a12=.25*(E[1]*E[0]/D[0]+E[2]*E[3]/D[1]);
  /* a21=.25*(E[0]*E[1]/D[0]+E[2]*E[3]/D[1]);*/
  a22=.25*(E[1]*E[1]/D[0]+E[3]*E[3]/D[1]);

  /* (x-m)'*A*(x-m)<1 */
  for(x=xl;x<=xh;x++) {
    xd=(fpnum)x-cogx+1.0;   /* Add matlab offset again */
    qf0=xd*xd*a11;
    xd2a12=xd*2*a12;
    for(y=yl;y<=yh;y++) {
      yd=(fpnum)y-cogy+1.0; /* Add matlab offset again */
      qf=qf0;
      qf+=yd*(xd2a12+yd*a22);
      if(qf<1.0) {
	cind=x*rows+y;
	for(k=0;k<ndim;k++) {
	  img[cind+k*pixels]=pvec[k];
	}
      }
    }
  }
}