Exemple #1
0
main(int argc, char *argv[]){
  int i,j,k;
  int nr, n_run;
  int n,m;
  double **A, **At, **G, **Gt, **GtG, **U, **Ut, **UtU, *w, **V, norm;
  double *v;
  
  FILE *stream;
  if (argc <4){
    fprintf(stderr,"Usage: %s m n n_run <datfile>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  m = atoi(argv[1]); 
  n = atoi(argv[2]);
  n_run = atoi(argv[3]);
  if (argc == 5){
    if ((stream = fopen(argv[4], "r")) == NULL){
      fprintf(stderr, "Can't open file %s.\n", argv[4]);
      exit(EXIT_FAILURE);
    }
  } else {
    stream = stdin;
  }
  
  A = ALLOC_MATRIX(m,n);
/*   for(i=0; i<m; ++i){ */
/*     for(j=0; j<n; ++j){ */
/*       if (fscanf(stream,"%lf", A[i]+j) != 1){ */
/* 	fprintf(stderr,"Error occured in reading the data.\n"); */
/* 	exit(EXIT_FAILURE); */
/*       } */
/*     } */
/*   } */

  for(i=0; i<m; ++i){  
      for(j=0; j<n; ++j){ 
	  A[i][j] = RANDOM(-10.0,10.0);
      }
  }

  /* print A */
  U = ALLOC_MATRIX(m,n);
  COPY_VECTOR(A[0], U[0], m*n);
  for(i=0; i<m; ++i){
    for(j=0; j<n; ++j)
      printf("%8.2f ", U[i][j]);
    printf("\n");
  }

  printf("Using Gram-Schmidt\n");
  /* gram-schmidt */
  At = RECT_TRANSPOSE(A, m,n);
  Gt = ALLOC_MATRIX(n,m);
  for(i=0; i<n_run; ++i){
      COPY_VECTOR(At[0], Gt[0], n*m);
      GRAM_SCHMIDT(Gt,n,m);
  }

  printf("Orthogonal vectors are \n");
  for (i=0; i<n; ++i){
      for(j=0, norm=0.0; j<m; ++j){
	  norm += SQR(Gt[i][j]);
	  printf("%.6f ", Gt[i][j]);
      }
      printf(" %f\n",norm);
  }

  G = RECT_TRANSPOSE(Gt,n,m);
  GtG = ALLOC_MATRIX(n,n);
  printf("S=\n");
  for(i=0; i<n; ++i){
      for(j=0; j<n; ++j){
	  GtG[i][j]=0.0;
	  for(k=0; k<m; ++k)
	      GtG[i][j]+=Gt[i][k]*G[k][j];
	  printf("%8.1e ", GtG[i][j]);
      }
      printf("\n");
  }

  /* project random vector against orthogonal basis */
  v = ALLOC_VECTOR(m);
  for(i=0; i<m; ++i)
      v[i] = RANDOM(-10,10);
  PROJECT(v, m, Gt, n);
  for(i=0; i<n; ++i)
      printf("v.G[%2d] = %9.2e\n", i, DOTP(v,Gt[i],m));
}
Exemple #2
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);
}
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;
}
Exemple #4
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");
}