Esempio n. 1
0
int shortems(int n,int p,int nclass,double *pi,double **X,double **Mu,  
             double **LTSigma,int maxshortiter,double shorteps)
{
  /*initializing as per Beiernacki, Celeaux, Govaert~(2003) */

  int i,iter,totiter=0;
  double *oldpi,**oldMu,**oldLTSigma,oldllh=-Inf,llhval;
  MAKE_VECTOR(oldpi,nclass);
  MAKE_MATRIX(oldMu,nclass,p);
  MAKE_MATRIX(oldLTSigma,nclass,p*(p+1)/2);
  do {
/* Modified by Wei-Chen Chen on 2009/03/08.
    i=randomEMinit(X,n,p,nclass,oldpi,oldMu,oldLTSigma);
    i = mb_randomEMinit(X, n, p, nclass, oldpi, oldMu, oldLTSigma);
*/
    i = randomEMinit(X, n, p, nclass, oldpi, oldMu, oldLTSigma);

    iter=maxshortiter-totiter;
    iter=shortemcluster(n,p,nclass,oldpi,X,oldMu,oldLTSigma,iter,shorteps,
			&llhval);
    if (llhval >= oldllh) {
      int i;
      oldllh=llhval;
      cpy(oldMu,nclass,p,Mu);
      cpy(oldLTSigma,nclass,p*(p+1)/2,LTSigma);
      for(i=0;i<nclass;i++) pi[i]=oldpi[i];
    }
    totiter+=iter;
  }  while (totiter < maxshortiter);
  FREE_MATRIX(oldMu);
  FREE_MATRIX(oldLTSigma);
  FREE_VECTOR(oldpi);
  return totiter; 
}
Esempio n. 2
0
void prcomp(int n, int m,double **x,double **UtX,double *D)
{
  int i,j;
  double *mu,*nu,*ltsigma,**xx,**V,**Vt;

  MAKE_VECTOR(mu,m);
  MAKE_VECTOR(ltsigma,m*(m+1)/2);  
  meandispersion(x,n,m,mu,ltsigma);
  FREE_VECTOR(ltsigma);
  MAKE_MATRIX(xx,n,m);
  for(i=0;i<n;i++) {
    for(j=0;j<m;j++) xx[i][j]=(x[i][j]-mu[j])/sqrt(n-1.);
  }

  MAKE_MATRIX(V,m,m);
  i=svdd(xx,n,m,D,UtX,V);
  MAKE_MATRIX(Vt,m,m);
  matrpose(V,m,m,Vt);
  MAKE_VECTOR(nu,m);
  i=matxvec(Vt,m,m,mu,m,nu);
  FREE_MATRIX(Vt);
  FREE_VECTOR(mu);
  FREE_MATRIX(V);
  FREE_MATRIX(xx);

  for(i=0;i<n;i++) {
    for(j=0;j<m;j++) {
      UtX[i][j]*=D[j];
      UtX[i][j]+=nu[j];
    }
  }
  FREE_VECTOR(nu);
  return;
}
Esempio n. 3
0
/* shortems() for model-based initializer. */
void shortems_mb(int n,int p,int nclass,double *pi,double **X,double **Mu,  
    double **LTSigma,int maxshortiter,double shorteps){
  int i, iter, totiter = 0, n_par = p * (p + 1) / 2;
  double *oldpi, **oldMu, **oldLTSigma, oldllh = -Inf, llhval;

  MAKE_VECTOR(oldpi, nclass);
  MAKE_MATRIX(oldMu, nclass, p);
  MAKE_MATRIX(oldLTSigma, nclass, n_par);

  do{
    mb_init(X, n, p, nclass, oldpi, oldMu, oldLTSigma);

    iter = maxshortiter - totiter;
    iter = shortemcluster(n, p, nclass, oldpi, X, oldMu, oldLTSigma, iter,
                          shorteps, &llhval);
    if(llhval >= oldllh){
      oldllh = llhval;
      cpy(oldMu, nclass, p, Mu);
      cpy(oldLTSigma, nclass, n_par, LTSigma);
      for(i = 0; i < nclass; i++) pi[i] = oldpi[i];
    }

    totiter += iter;
  }  while(totiter < maxshortiter);

  FREE_MATRIX(oldMu);
  FREE_MATRIX(oldLTSigma);
  FREE_VECTOR(oldpi);
} /* End of shortems_mb(). */
Esempio n. 4
0
int ss_shortems(int n, int p, int nclass, double *pi, double **X, double **Mu,  
    double **LTSigma, int maxshortiter, double shorteps, int *lab, int labK){
  /*initializing as per Beiernacki, Celeaux, Govaert~(2003) */

  int i, j, iter, totiter = 0, n_par = p * (p + 1) / 2;
  int nonlab_total = 0, lab_index[n];
  double *oldpi, **oldMu, **oldLTSigma, oldllh = -Inf, llhval;
  double **labMu;

  MAKE_VECTOR(oldpi, nclass);
  MAKE_MATRIX(oldMu, nclass, p);
  MAKE_MATRIX(oldLTSigma, nclass, n_par);
  MAKE_MATRIX(labMu, labK, p);

  for(i = 0; i < n; i++){
    if(lab[i] == -1) lab_index[nonlab_total++] = i;
  }
  labInitMus(n, p, labK, X, lab, labMu);

  do{
    for(i = 0; i < labK; i++){
      for(j = 0; j < p; j++) oldMu[i][j] = labMu[i][j];
    }

    iter = maxshortiter - totiter;

/* Modified by Wei-Chen Chen on 2009/03/08.
    ss_randomEMinit(X, n, p, nclass, oldpi, oldMu, oldLTSigma,
                    lab, labK, nonlab_total, lab_index);
    ss_mb_randomEMinit(X, n, p, nclass, oldpi, oldMu, oldLTSigma,
                       lab, labK, nonlab_total, lab_index);
*/
    ss_randomEMinit(X, n, p, nclass, oldpi, oldMu, oldLTSigma,
                    lab, labK, nonlab_total, lab_index);

    iter = ss_shortemcluster(n, p, nclass, oldpi, X, oldMu, oldLTSigma, iter,
                             shorteps, &llhval, lab);
    if(llhval >= oldllh){
      int i;
      oldllh = llhval;
      cpy(oldMu, nclass, p, Mu);
      cpy(oldLTSigma, nclass, n_par, LTSigma);
      for(i = 0; i < nclass; i++) pi[i] = oldpi[i];
    }
    totiter += iter;
  } while(totiter < maxshortiter);
  FREE_MATRIX(oldMu);
  FREE_MATRIX(oldLTSigma);
  FREE_VECTOR(oldpi);
  FREE_MATRIX(labMu);
  return totiter; 
} /* End of ss_shortems(). */
Esempio n. 5
0
int shortemcluster(int n, int p, int k, double *pi, double **X,
    double **Mu, double **LTSigma, int maxiter, double eps, double *llhdval,
    int *conv_iter, double *conv_eps){
  int iter, i, n_par =  p * (p + 1) / 2;
  double *backup_pi, **backup_Mu, **backup_LTSigma;
  double **gamm, llhd, oldllhd, llh0;

  MAKE_VECTOR(backup_pi, k);
  MAKE_MATRIX(backup_Mu, k, p);
  MAKE_MATRIX(backup_LTSigma, k, n_par);
  MAKE_MATRIX(gamm, n, k);

  estep_gamma(n, p, k, X, gamm, Mu, LTSigma);
  llhd = lnlikelihood_gamma(n, k, gamm, pi);
  llh0 = llhd;
  iter = 0;
  do{
    oldllhd = llhd;
    norm_gamma(n, k, gamm, pi);

    for(i = 0; i < k; i++) backup_pi[i] = pi[i];
    cpy(Mu, k, p, backup_Mu);
    cpy(LTSigma, k, n_par, backup_LTSigma);

    mstep(X, n, p, k, pi, Mu, LTSigma, gamm);
    estep_gamma(n, p, k, X, gamm, Mu, LTSigma);
    llhd = lnlikelihood_gamma(n, k, gamm, pi);

    if(oldllhd > llhd){
      for(i = 0; i < k; i++) pi[i] = backup_pi[i];
      cpy(backup_Mu, k, p, Mu);
      cpy(backup_LTSigma, k, n_par, LTSigma);
      llhd = oldllhd;
      iter--;
      break;
    }

    iter++;
    *conv_eps = fabs((oldllhd - llhd) / (llh0 - llhd));
  } while((*conv_eps > eps) && (iter < maxiter));
  *llhdval = llhd;
  *conv_iter = iter;

  FREE_VECTOR(backup_pi);
  FREE_MATRIX(backup_Mu);
  FREE_MATRIX(backup_LTSigma);
  FREE_MATRIX(gamm);
  return iter;
}
Esempio n. 6
0
int shortemcluster_org(int n,int p,int k,double *pi,double **X,double **Mu,  
		   double **LTSigma,int maxiter,double eps,double *llhdval)
{
  int iter;
  double **gamm,llhd,oldllhd,llh0;
  /*same as emcluster, only difference being in how convergence is handled,
   done as per Biernacki et al*/
  MAKE_MATRIX(gamm,n,k);
  llhd=lnlikelihood(n,p,k,pi,X,Mu,LTSigma);
  llh0=llhd;
  iter=0;
  do  {
    oldllhd=llhd;
    estep(n,p,k,X,gamm,pi,Mu,LTSigma);
    mstep(X,n,p,k,pi,Mu,LTSigma,gamm);
    llhd=lnlikelihood(n,p,k,pi,X,Mu,LTSigma);
    iter++;
  } while (((oldllhd-llhd)/(llh0-llhd) > eps) && (iter<maxiter));
/* Marked by Wei-Chen Chen on 2009/01/21.
   This value, (oldllhd-llhd)/(llhd-llh0), is always negative.

  } while ((((oldllhd-llhd)/(llhd-llh0)) > eps) && (iter<maxiter));
*/
  (*llhdval)=llhd;
  FREE_MATRIX(gamm);
  return iter;
}
Esempio n. 7
0
void XAXt2(double **X, int p, double **A, double ***Res, int k){

	double **Res1, **Res2;

	MAKE_MATRIX(Res1, p, p);	
	MAKE_MATRIX(Res2, p, p);

	tA(X, p, p, Res2);

	multiply(X, p, p, A, p, p, Res1);
	multiply2(Res1, p, p, Res2, p, p, Res, k);

	FREE_MATRIX(Res1);
 	FREE_MATRIX(Res2);

}
int decode(const struct VEC* in_vec, struct  VEC* out_vec , BIT** submatrix)
{
	if(!in_vec || !out_vec || !submatrix)
		return ERR_Pointer;
	size_t i,j;
	size_t dsize = in_vec->d_size;
	BIT **inv_submatrix = NULL;	
	
	BYTE* a = NULL;

	
	inv_submatrix = inv(submatrix, CLOUMN);
	
	for(i=0; i<CLOUMN ; ++i)
	{
		a = out_vec->base[i];
		memset(a,0,dsize);

		for(j=0; j<CLOUMN; ++j)
		{
			if(inv_submatrix[i][j])
			{
				ADD_VEC(a, (in_vec->base)[j] , dsize);
			}			
		}
		out_vec->base[i] = a;
	}
	FREE_MATRIX(inv_submatrix,CLOUMN,CLOUMN);
	return 0;	
}
Esempio n. 9
0
int ZXY(double **Z, int az, int bz, double **X, int ax, int bx, double **Y, int ay, int by, double **Res){

	double **Res1;

	MAKE_MATRIX(Res1, az, bx);	

	multiply(Z, az, bz, X, ax, bx, Res1);
	multiply(Res1, az, bx, Y, ay, by, Res);

	FREE_MATRIX(Res1);
 
        return 0;
    
}
Esempio n. 10
0
/* Model-based initializer. */
void mb_init(double **X, int n, int p, int nclass, double *pi, double **Mu,
    double **LTSigma){
  int i, min_n = p + 1, n_nclass = (int) ceil((double) n / (double) nclass);
  int G, nc[nclass], i2, i3, j = 1;
  int tmp_G, tmp_class, class[n];
  double lambda, max_prob, prob[n], **gamm;
  double tmp_prob, tmp_n, tmp_pi, tmp_gamm;
  double class_prob[n], tmp_class_prob;
  PARAM param;
  void *pt_param;

  /* Find lambda, the expected size of neighbor. */
  if(n_nclass < min_n){
    //WCC printf("n is too small, or p or k is too large.\n"); 
    //WCC exit(1);
    error("n is too small, or p or k is too large.\n"); 
  }
  param.n_nclass = (double) (n_nclass - min_n);
  param.lower_bound = 1e-6;
  param.upper_bound = param.n_nclass;
  param.tol = 1e-6;
  param.maxit = 1000;
  pt_param = &param;
  lambda = find_lambda(pt_param);


  tmp_n = (double) n;
  MAKE_MATRIX(gamm, n, nclass);
  tmp_prob = 1.0 / (double) n;

  do{
    /* Initial prob. */
    for(i = 0; i < n; i++) prob[i] = tmp_prob;

    /* Note: cut_sub() will overwrite prob. */
    cut_sub(X, n, p, 1, min_n, lambda, prob, Mu, LTSigma);
    pi[0] = 1.0;
    for(i = 0; i < n; i++) class[i] = 0;
    nc[0] = n;

    for(G = 2; G <= nclass; G++){
      max_prob = 0.0;
      tmp_G = G - 1;

      for(i = 0; i < n; i++){
        prob[i] = mixllhd(p, tmp_G, X[i], pi, Mu, LTSigma);
        if(prob[i] > max_prob) max_prob = prob[i];
      }
      for(i = 0; i < n; i++) prob[i] = max_prob - prob[i];

      /* Drop the 75% points around cluster centers. */
      for(i = 0; i < G; i++){
        i3 = 0;
        for(i2 = 0; i2 < n; i2++){
          if(class[i2] == i) class_prob[i3++] = prob[i2];
        }
        tmp_class_prob = mb_quantile(i3, class_prob, 0.75);
        for(i2 = 0; i2 < n; i2++){
          if(class[i2] == i && prob[i2] < tmp_class_prob) prob[i2] = 0.0; 
        }
      }

      /* Note: cut_sub() will overwrite prob. */
      cut_sub(X, n, p, G, min_n, lambda, prob, Mu, LTSigma);

      /* Assume uniform for pi, do one estep, and reassign new pi. */
      tmp_pi = 1.0 / (double) G;
      for(i = 0; i < G; i++) pi[i] = tmp_pi;

      /* Run one estep to update pi. */
      estep(n, p, G, X, gamm, pi, Mu, LTSigma);
      for(i = 0; i < G; i++) nc[i] = 0;
      for(i = 0; i < n; i++){
        tmp_gamm = 0.0;
        tmp_class = 0;
        for(i2 = 0; i2 < G; i2++){
          if(tmp_gamm < gamm[i][i2]){
            tmp_gamm = gamm[i][i2];
            tmp_class = i2;
          }
        }
        class[i] = tmp_class;
        nc[tmp_class] = nc[tmp_class] + 1;
      }

      j = 1;
      for(i = 0; i < G; i++){
        if(nc[i] < min_n){
          j = 0;
	  break;
	}
      }
      if(j == 0) break;

      for(i = 0; i < G; i++) pi[i] = (double) nc[i] / tmp_n;
    }
  } while(j == 0);

  FREE_MATRIX(gamm);
} /* End of mb_init(). */
Esempio n. 11
0
void AllPerms(int size,int **perms){

	int sch, i, j, v, w, finish, flag, ind;
	double **pat;
	int *cn;

	sch = 0;
	i = 0;
	j = -1;
	flag = 0;
	finish = 0;
	ind = 0;

	MAKE_MATRIX(pat, size, size);
	for (v=0; v<size; v++){
		for (w=0; w<size; w++){
			pat[v][w] = 0;
		}
	}

	MAKE_VECTOR(cn, size);
	for (v=0; v<size; v++){
		cn[v] = 0;
	}
  

	while (finish == 0){
    
		if (j != (size-1)){
			j = j+1;
		} else {
			if (flag == 1){
				j = 0;
				i = i+1;
				flag = 0;
			}
		}
    
		if (pat[i][j] == 0){
			for (v=0; v<size; v++){
				pat[i][v]=1;
				pat[v][j]=1;
			}
      
			sch = sch + 1;
			cn[sch-1] = j;
			flag = 1;
		}

		if ((sch == size) & (flag == 1)){
      
			for (v=0; v<size; v++){
				perms[ind][v] = cn[v];
			}

			ind++;
			flag = 0;
			sch = sch - 1;
			i = i - 1;
			j = cn[sch-1];
			sch = sch-1;
      
			for (v=0; v<size; v++){
				for (w=0; w<size; w++){
					pat[v][w] = 0;
				}
			}

			for (v=0; v<sch; v++){
				for (w=0; w<size; w++){
					pat[v][w] = 1;
					pat[w][cn[v]] = 1;
				}
			}    
      
		}


		if ((j == size - 1) & (flag == 0)){
			i = i - 1;
			
			sch = sch-1;

			if (sch >= 0){

				j = cn[sch];

				for (v=0; v<size; v++){
					for (w=0; w<size; w++){
						pat[v][w] = 0;
					}
				}

				if (sch > 0){
					for (v=0; v<sch; v++){
						for (w=0; w<size; w++){
							pat[v][w] = 1;
							pat[w][cn[v]] = 1;
						}
					}

				}
				
			}

			if (i >= 0){
				pat[i][j] = 1;
			}
		}

		if (sch == -1){
			finish = 1;
		}

	}

	FREE_MATRIX(pat);
	FREE_VECTOR(cn);

}
Esempio n. 12
0
void conv_2d_x_sym_odd_opt

(
                       /******************************************************/
    int     masksize,  /* in     : size of convolution mask                  */
    float   *mask,     /* in     : convolution mask                          */
    int     nx,        /* in     : data dimension in x direction             */
    int     ny,        /* in     : data dimension in y direction             */
    int     bx,        /* in     : boundary in x direction                   */
    int     by,        /* in     : boundary in y direction                   */
    float   **f,       /* in     : original data                             */
    float   **v        /* out    : processed data                            */
                       /******************************************************/
)

/* convolution in x-direction with odd symmetric convolution mask */
/* since the values are stored in y-direction in the cache, a loop unrolling */
/* scheme is applied */

{

                            /*************************************************/
int    i, j, k,p;           /* loop variables                                */
float  *sum;                /* for summing up                                */
float  **help;              /* array of rows with suitable boundary size     */
int    tmp1,tmp2,tmp3,tmp4; /* time saver                                    */
int    UNROLL;              /* number of rows that are convolved in parallel */
int    inner_loop_max;      /* number of loops for parrallel computation     */
int    inner_loop_rest;     /* number of remaining rows                      */
                            /*************************************************/


/* set number of rows convolved in parallel */
UNROLL=32; 

/* allocate storage for that many rows */
ALLOC_MATRIX (1, nx+masksize+masksize,UNROLL, &help);

/* allocate storagy for that many results */
ALLOC_VECTOR (1, UNROLL, &sum);

/* compute number of loops required if the desired number of rows is */
/* convolved in parallel */
inner_loop_max=ny/UNROLL;

/* compute number of remaining rows that have to be convolved thereafter */
inner_loop_rest=ny-(inner_loop_max*UNROLL);

/* time saver indices */ 
tmp1=masksize-1;
tmp2=tmp1+nx;
tmp3=tmp2+1;
tmp4=tmp1-(bx-1);

/*****************************************************************************/
/* (1/2) As long as the desired number of rows can be convolved in parallel  */
/*       use loop unrolling scheme that respects cache direction             */
/*****************************************************************************/

 for (j=0; j<inner_loop_max; j++)
 {
     /* copy rows in vector array */
     for (i=bx; i<nx+bx; i++)
	 for (k=0; k<UNROLL; k++)
	 {
	     help[i+tmp4][k] = f[i][j*UNROLL+k+by];
	 }
     
     /* mirror boundaries of each of these rows */
     for (p=1; p<=masksize; p++)
	 for (k=0; k<UNROLL; k++)
	 {
	     help[masksize-p][k] = help[tmp1+p][k];
	     help[tmp2    +p][k] = help[tmp3-p][k];
	 }
     
     /* convolution step for each of these rows */
     for (i=masksize; i<=tmp2; i++)
     {
	 /* convolve different rows in parallel */
	 for (k=0; k<UNROLL; k++)
	 {	    
	     sum[k] = mask[0] * help[i][k];
	 }
	 
	 for (p=1; p<=masksize; p++)
	     for (k=0; k<UNROLL; k++)
	     {
		 sum[k] += mask[p] * (help[i+p][k] + help[i-p][k]);
	     } 
	 
	 /* write back results in parallel */
	 for (k=0; k<UNROLL; k++)	
	 {	
	     v[i-tmp4][j*UNROLL+k+by] = sum[k];     
	 }
     }
 } /* for j */

/*****************************************************************************/
/* (2/2) Convolve the remaining number of rows in parallel using the same    */
/*       loop unrolling scheme                                               */
/*****************************************************************************/

 if (inner_loop_rest>0)
 {
     /* copy rows in vector array */
     for (i=bx; i<nx+bx; i++)
	 for (k=0; k<inner_loop_rest; k++)
	 {
	     help[i+tmp4][k] = f[i][j*UNROLL+k+by];
	 }
     
     /* mirror boundaries for each of these rows */
     for (p=1; p<=masksize; p++)
	 for (k=0; k<inner_loop_rest; k++)
	 {
	     help[masksize-p][k]      = help[tmp1+p][k];
	     help[tmp2+p][k] = help[tmp3-p][k];
	 }

     /* convolution step for each of these rows */
     for (i=masksize; i<=tmp2; i++)
     {
	 /* convolve different rows in parallel */
	 for (k=0; k<inner_loop_rest; k++)
	 {	    
	     sum[k] = mask[0] * help[i][k];
	 }

	 for (p=1; p<=masksize; p++)
	     for (k=0; k<inner_loop_rest; k++)
	     {
		 sum[k] += mask[p] * (help[i+p][k] + help[i-p][k]);
	     } 

	  /* write back results in parallel */
	 for (k=0; k<inner_loop_rest; k++)
	 {	    
	     v[i-tmp4][j*UNROLL+k+by] = sum[k];
	 }
     }
 }

/* disallocate storage for the rows */
FREE_MATRIX (1, nx+masksize+masksize,UNROLL, help);

/* disallocate storage for the results */
FREE_VECTOR (1, UNROLL, sum);
 
return;
}
Esempio n. 13
0
int main (int argc, char* argv[])
{
        int dims[10];
        char out_file[80];
/* ---------- set boundary and grid size ----------------------------------- */

        g_bx=1;
        g_by=1;

        g_hx=1;
        g_hy=sqrt(3)/2;

/* ---------- read in arguments -------------------------------------------- */


        if (argc <3) {
                fprintf(stderr, "Not sufficient arguments: \n");
                fprintf(stderr, "<APP> <LF H5 in file> <LF in dataset> <LF H5 out file>\n");
                exit(1);
        }
        strcpy(lf_h5,argv[1]);
        strcpy(lf_dataset,argv[2]);
        strcpy(out_file,argv[3]);
        /*TODO Group truth*/
        //strcpy(g_ref,argv[4]);


/* set default parameters */
        g_g_pixel_ratio = 1;

        g_g_max_disp=2.0;

        g_direct_compute=0;
        //
        g_e_sigma=1;
        //half kernel size
        g_m_alpha=2;

        g_m_type=0;

        g_active_param=1000;

        g_u_ref=NULL;
        g_v_ref=NULL;

        g_n_omega=1.95;

        g_n_warp_levels=6;
        g_n_warp_eta=0.5;

        g_n_iter = 800; // number of iteration




/* ---------- read in information of light field ------------------- */


        data = hdf5_read_lf(lf_h5, lf_dataset,W,H,S,T,C);

        //exit(1);
        g_nx=W;
        g_ny=H;
        /* extract two view */
        // RGB image from three views;
        float * view00 = new float[W*H*3];
        float * view03 = new float[W*H*3];
        float * view0_3 = new float[W*H*3];
        float * view30 = new float[W*H*3];
        float * view_30 = new float[W*H*3];
        // grayscale image from three view;
        float * img_c;
        float * img_l;
        float * img_r;
        float * img_u;
        float * img_d;
        // the center view index.
        int center = floor((S-1)/2.0);
        for(int j = 0; j<H; j++) {
                for(int i =0; i<W; i++) {
                        for(int c=0; c<3; c++) {
                                view00[j*W*3+i*3+c]=data[c*W*H*S*T + i*H*S*T + j*S*T + (center)*T + center];
                                view03[j*W*3+i*3+c]=data[c*W*H*S*T + i*H*S*T + j*S*T + (center)*T + center-5];
                                view0_3[j*W*3+i*3+c]=data[c*W*H*S*T + i*H*S*T + j*S*T + (center)*T + center-3];
                                view30[j*W*3+i*3+c]=data[c*W*H*S*T + i*H*S*T + j*S*T + (center)*T + center+3];
                                view_30[j*W*3+i*3+c]=data[c*W*H*S*T + i*H*S*T + j*S*T + (center)*T + center+5];
                        }
                }
        }
        img_c = convertRGBtoGray(W,H,view00);
        img_l = convertRGBtoGray(W,H,view_30);
        img_r = convertRGBtoGray(W,H,view30);
        img_d = convertRGBtoGray(W,H,view0_3);
        img_u = convertRGBtoGray(W,H,view03);
        free(view00);
        free(view03);
        free(view0_3);
        free(view30);
        free(view_30);
        /* Write gray image  */
        writeImageGray("view_c.png",W,H,img_c,"Image center");
        writeImageGray("view_l.png",W,H,img_l,"Image Left");
        writeImageGray("view_r.png",W,H,img_r,"Image Right");
        writeImageGray("view_u.png",W,H,img_u,"Image Up");
        writeImageGray("view_d.png",W,H,img_d,"Image Down");

/* ---------- memory allocation ------------------------------------ */

        ALLOC_MATRIX(2, g_nx+2*g_bx, g_ny+2*g_by, &g_f1,   &g_f2);
        ALLOC_MATRIX(2, g_nx+2*g_bx, g_ny+2*g_by, &g_f1_s, &g_f2_s);
        ALLOC_MATRIX(1, g_nx+2*g_bx, g_ny+2*g_by, &g_f2_bw);
        ALLOC_MATRIX(2, g_nx+2*g_bx, g_ny+2*g_by, &g_u,    &g_v);

        //if (argc==5)
        //        ALLOC_MATRIX(2, g_nx+2*g_bx, g_ny+2*g_by, &g_u_ref,   &g_v_ref);

        ALLOC_CUBIX(1, 2*g_nx+2*g_bx, g_ny+2*g_by, 3, &g_p6);
        ALLOC_CUBIX(1, g_nx+2*g_bx, g_ny+2*g_by, 3, &g_disp);
        //g_pixels = (GLubyte *) malloc (2*(g_nx+1)*g_ny*3*sizeof(GLubyte));
        g_pixels = new GLubyte[2*(g_nx+1)*g_ny*3*sizeof(GLubyte)];
        uvbuffer = new float[g_nx*g_ny*2];


/* ---------- read in image pair ------------------------------------------- */
        //
        // calculate_flow_write_img(img_c,img_l,"flow_left.png",out_file,"/flow/left");
        // calculate_flow_write_img(img_c,img_r,"flow_right.png",out_file,"/flow/right");
        // calculate_flow_write_img(img_c,img_u,"flow_up.png",out_file,"/flow/up");
        // calculate_flow_write_img(img_c,img_d,"flow_down.png",out_file,"/flow/down");
/* ---------- read in ground truth displacement field ---------------------- */

        //if (argc==5)
        //        read_barron_data(g_ref,g_u_ref,g_v_ref,g_nx,g_ny,g_bx,g_by);

//
/* ---------- M A I N   L O O P -------------------------------------------- */

        for(int j=0; j<g_ny; j++) {
                for(int i=0; i<g_nx; i++) {
                        g_f1[i+g_bx][j+g_by]= img_c[j*g_nx+i]*255;
                        g_f2[i+g_bx][j+g_by]= img_u[j*g_nx+i]*255;
                }
        }

// open OpenGL window */
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

        glutInitWindowSize((int)round(2*g_nx*g_g_pixel_ratio),
                           (int)round(g_ny*g_g_pixel_ratio));
        glutCreateWindow("CORRESPONDENCE PROBLEMS - VISUALISATION FRONTEND");

// register handle routines
        glutDisplayFunc(handleDraw);
        glutIdleFunc(handleComputeNothing);
        glutKeyboardFunc(handleKeyboard);
        glutSpecialFunc(handleKeyboardspecial);
        glutMouseFunc(handleMouse);
//
// main
        handleComputeDisplacements();
        handleDraw();
        showParams();
        glutMainLoop();




/* ---------- free memory -------------------------------------------------- */

        FREE_MATRIX (2, g_nx+2*g_bx, g_ny+2*g_by, g_f1,   g_f2);
        FREE_MATRIX (2, g_nx+2*g_bx, g_ny+2*g_by, g_f1_s, g_f2_s);
        FREE_MATRIX (1, g_nx+2*g_bx, g_ny+2*g_by, g_f2_bw);
        FREE_MATRIX (2, g_nx+2*g_bx, g_ny+2*g_by, g_u,    g_v);
        //
        // printf("free matrix  \n");
        //
        // //TODO handle ground truth.
        // // if (argc==5)
        // //         FREE_MATRIX(2,   g_nx+2*g_bx, g_ny+2*g_by, g_u_ref, g_v_ref);
        //
        FREE_CUBIX (1, 2*g_nx+2*g_bx, g_ny+2*g_by, 3, g_p6);
        FREE_CUBIX (1, g_nx+2*g_bx, g_ny+2*g_by, 3, g_disp);


        free(g_pixels);
        free(uvbuffer);

        printf("\n\n\n");


        return(0);
}
Esempio n. 14
0
void read_barron_data
(
                            /************************************************/
    char  *filename,        /* in : file name                               */
    float **u,              /* in : x-component of vector data              */
    float **v,              /* in : y-component of vector data              */
    int   nx,               /* in : size in x-direction                     */
    int   ny,               /* in : size in y-direction                     */
    int   bx,               /* in : boundary in x-direction                 */
    int   by                /* in : boundary in y-direction                 */
                            /************************************************/
)

/* reads barron file */

{
                           /*************************************************/
    FILE *file;            /* file pointer                                  */
    float help;            /* tmp variable                                  */
    float **uref;          /* tmp array                                     */
    float **vref;          /* tmp array                                     */
    int i,j;               /* loop variabeles                               */
    int nxref_and_offsetx; /* size in x-direction with crop offset          */
    int nyref_and_offsety; /* size in y-direction with crop offset          */
    int nxref_no_offsetx;  /* size in x-direction without crop offset       */
    int nyref_no_offsety;  /* size in y-direction without crop offset       */
    int offsetx;           /* crop offset in x-direction                    */
    int offsety;           /* crop offset in y-direction                    */
                           /*************************************************/


    printf("\n Trying to read barron %s ...",filename);

    /* try to open file */
    file = fopen(filename,"r");

    /* if file not found */
    if (file==NULL)
    {
	printf("... FAILED");
	printf("\n\n PROGRAM ABORTED !!! \n\n");
	exit(0);
    }

    /* read header */
    fread (&help, sizeof(float), 1, file);
    nxref_and_offsetx  = (int) help;
    fread (&help, sizeof(float), 1, file);
    nyref_and_offsety  = (int) help;
    fread (&help, sizeof(float), 1, file);
    nxref_no_offsetx  = (int) help;
    fread (&help, sizeof(float), 1, file);
    nyref_no_offsety  = (int) help;
    fread (&help, sizeof(float), 1, file);
    offsetx = (int) help;
    fread (&help, sizeof(float), 1, file);
    offsety = (int) help;

    /* compare dimensions for consistency */
    if ((nx!=nxref_no_offsetx)||(ny!=nyref_no_offsety))
    {
	printf("... WRONG DIMENSIONS");
	printf("\n\n PROGRAM ABORTED !!! \n\n");
	exit(0);
    }

    /* allocate memory for tmp array */
    ALLOC_MATRIX(2,nxref_and_offsetx,nyref_and_offsety,&uref,&vref);

    /* read barron data */
    for (j = 0; j < nyref_and_offsety; j++)
	for (i = 0; i < nxref_and_offsetx; i++)
	{
	    fread(&help, sizeof(float), 1, file);
	    uref[i][j] = (float) help;
	    fread(&help, sizeof(float), 1, file);
	    vref[i][j] = (float) help;
	}


    /* copy data without cropped border */
    for (i=bx; i<nx+bx; i++)
	for (j=by; j<ny+by; j++)
	{
	    u[i][j] = (float) uref[i-bx+offsetx][j-by+offsety];
	    v[i][j] = (float) vref[i-bx+offsetx][j-by+offsety];
	}

    /* free memory for tmp array */
    FREE_MATRIX(2,nxref_and_offsetx,nyref_and_offsety,uref,vref);


    /* close file */
    fclose(file);

    printf("... SUCCESS");
}