void TripleConvol::apply_filter(double* out, double* padded_img, Filter filter, int width, int height, int padding) { for(int y=0 + padding; y<height + padding; ++y) { for(int x=1 + padding; x<width + padding; x+=3) { convol(out, padded_img, filter, x, y, (width+padding*2)); } } }
void inter_image_kernel_to_psf(float *H, float* xinter, float *h, int nx, int ny) { int it_max = 3; int it=0; int i,j; float lambda_x, lambda_y; float scale_x, scale_y, nx2, ny2, nxS2, nyS2, tx, ty; int nxS,nyS; ImageFloat x, xn, xGrid,yGrid,scaled_x,aux; nx2 = (((float)nx)-1)/2; ny2 = (((float)ny)-1)/2; /*Create image with the kernel*/ x = new_imageFloat(nx,ny); xn = new_imageFloat(nx,ny); memcpy (x->val, xinter, nx * ny * sizeof (float)); memcpy (xn->val, xinter, nx * ny * sizeof (float)); /*lambda from ThinPlate Affine Part*/ lambda_x = H[0]; lambda_y = H[4]; /* Initialize scale=lambda^n, n=1*/ scale_x = lambda_x; scale_y = lambda_y; /*nxS = (int)(nx*scale_x+1); nyS = (int)(ny*scale_y+1); */ while (scale_x < 50 && scale_y < 50 && it< it_max) { nxS = ceil(nx*scale_x); nyS = ceil(ny*scale_y); nxS2 = (((float)nxS)-1)/2; nyS2 = (((float)nyS)-1)/2; xGrid = new_imageFloat (nxS, nyS); yGrid = new_imageFloat (nxS, nyS); tx = nx2 - nxS2/scale_x; ty = ny2 - nyS2/scale_y; /*Start from the center so the (tx,ty); is needed*/ for (i = 0; i < xGrid->nrow; i++) for (j = 0; j < xGrid->ncol; j++) { xGrid->val[i * xGrid->ncol + j] = ((float)j)/scale_x + tx; yGrid->val[i * xGrid->ncol + j] = ((float)i)/scale_y + ty; } scaled_x = bicubic (xGrid, yGrid, x, -0.5); free_imageFloat(xGrid); free_imageFloat(yGrid); printf("Iteration %d :: scale x: %f, y: %f\n",it, scale_x, scale_y); aux = convol(scaled_x,xn); free_imageFloat(xn); free_imageFloat(scaled_x); xn = new_imageFloat(aux->ncol,aux->nrow); memcpy (xn->val, aux->val, aux->ncol * aux->nrow * sizeof (float)); free_imageFloat(aux); scale_x = scale_x*lambda_x; scale_y = scale_y*lambda_y; it = it+1; } /*I added one extra scale...*/ scale_x = scale_x/lambda_x; scale_y = scale_y/lambda_y; xGrid = new_imageFloat (nx,ny); yGrid = new_imageFloat (nx,ny); tx = nxS2 - nx2*scale_x; ty = nyS2 - ny2*scale_y; /*I start from the center*/ for (i = 0; i < ny; i++) for (j = 0; j < nx; j++) { xGrid->val[i * xGrid->ncol + j] = j*scale_x + tx; yGrid->val[i * xGrid->ncol + j] = i*scale_y + ty; } aux = bicubic (xGrid, yGrid, xn, -0.5); /*h = (float *) malloc (nx * ny * sizeof (float));*/ memcpy (h, aux->val, nx * ny * sizeof (float)); free_imageFloat(aux); free_imageFloat(xGrid); free_imageFloat(yGrid); free_imageFloat(xn); free_imageFloat(x); }
/** * @brief PSF Estimation (Main Function) */ void two_photos_psf_estim (float *img1, int nx1, int ny1, float *img2, int nx2, int ny2, int s, int psf_ncol, int psf_nrow, float *h, float *k, int threshold, char *outprefix) { ImageFloat z_HR, z_LR, imgW, imgC, imgP; ImageFloat imgMask, imgCxs, imgCx, imgx; int i, j, np, ncol, nrow; float *p_HR, *p_LR; float max_val1, min_val1, max_val2, min_val2, min_val ,max_val, v; float *A, *b; char file_name[80]; char reverse; float offset_HR[2], offset_LR[2], H[9]; /*------------------------------------------------------------------------*/ /*- STEP 1--- IMAGE ALIGNMENT, GEOMETRIC TRANSFORMATION ESTIOMATION-------*/ /*------------------------------------------------------------------------*/ /* ORSA/Homography (SIFT) Estimate a Homography between the input images */ printf ("Running ORSA/Homography...\n"); orsa_homography_sift(img1, nx1, ny1, img2, nx2, ny2, 1.5, H, &p_LR, &p_HR, &np, &reverse); if (np == 0) { printf("Images do not match.\n"); exit(1); } if(!reverse) { printf(" First input image: farthest image\n"); printf("Second input image: closest image\n"); /* convert input image 2 to ImageFloat */ z_HR = new_imageFloat (nx2, ny2); memcpy (z_HR->val, img2, nx2 * ny2 * sizeof (float)); /* convert input image 1 to ImageFloat */ z_LR = new_imageFloat (nx1, ny1); memcpy (z_LR->val, img1, nx1 * ny1 * sizeof (float)); } else { printf(" First input image: closest image\n"); printf("Second input image: farthest image\n"); /* convert input image 1 to ImageFloat */ z_HR = new_imageFloat (nx1, ny1); memcpy (z_HR->val, img1, nx1 * ny1 * sizeof (float)); /* convert input image 1 to ImageFloat */ z_LR = new_imageFloat (nx2, ny2); memcpy (z_LR->val, img2, nx2 * ny2 * sizeof (float)); } /*------------------------------------------------------------------------*/ /*- STEP 2-------EXTRACT COMMON REGION (SUBIMAGES)------------------------*/ /*------------------------------------------------------------------------*/ /*Extract a rectangular sub image. The minimum rectangle that contains all *the 'p_HR' points in 'z_HR' and 'p_LR' in 'z_LR'. *Update the checkpoints location relative to the extracted image. */ imgP = extract_image_region (z_HR, p_HR, np, offset_HR); imgW = extract_image_region (z_LR, p_LR, np, offset_LR); /*------------------------------------------------------------------------*/ /*- STEP 3-------IMAGE INTERPOLATION: H_\frac{\lambda}{s} \tilde{v}_1-----*/ /*------------------------------------------------------------------------*/ closest_image_interpolation(H, s, imgW, imgP, &imgC, &imgMask, offset_LR, offset_HR); /*------------------------------------------------------------------------*/ /*- STEP 4-------GENERATING LINEAR SYSTEM MS_sUk = M\tilde{v}_2-----------*/ /*------------------------------------------------------------------------*/ /* A = MS_sUk, b = M\tilde{v}_2*/ make_Ab (imgC, imgW, imgMask, psf_ncol, psf_nrow, s, &A, &b, &ncol, &nrow); /*------------------------------------------------------------------------*/ /*- STEP 5-------SOLVING LINEAR SYSTEM k/ Ak = b -------------------------*/ /*------------------------------------------------------------------------*/ solve_lsd(A, b, k, ncol, nrow); /*------------------------------------------------------------------------*/ /*- STEP 6-------FROM INTER-IMAGE-KERNEL TO PSF---------------------------*/ /*------------------------------------------------------------------------*/ inter_image_kernel_to_psf(H, k, h, psf_ncol, psf_nrow); /* threshold==1 :threshold the final psf estimation*/ if (threshold) { for (i = 0; i < psf_ncol * psf_nrow; i++) h[i] = (h[i] > POSITIVE_TOL) ? h[i] : 0; } /*inter-image kernel should be sum_i x[i] = 1*/ normalize_area(k,psf_ncol * psf_nrow); /*PSF should be sum_i h[i] = 1*/ normalize_area(h,psf_ncol * psf_nrow); /*------------------------------------------------------------------------*/ /*----------SAVE INTERMEDIATE IMAGES, RESIDUAL----------------------------*/ /*------------------------------------------------------------------------*/ if(outprefix) { /*all output images are re-scaled to be in [0,255] normalizing with the max and min values of [imgC,imgW] values Difference image is normalized to [-0.05(max-min),0.05(max-min)] values of [imgC,imgW]. */ /*The normalization is done with the max value of imgC*/ max_val1 = 0; min_val1 = BIG_NUMBER; for(i=0; i< imgC->nrow; i++) for(j=0; j< imgC->ncol; j++) { v = imgC->val[ j + i * imgC->ncol]; if( v > max_val1 ) max_val1 = v; if( v < min_val1 ) min_val1 = v; } max_val2 = 0; min_val2 = BIG_NUMBER; for(i=0; i< imgW->nrow; i++) for(j=0; j< imgW->ncol; j++) { v = imgW->val[ j + i * imgW->ncol]; if( v > max_val2 ) max_val2 = v; if( v < min_val2 ) min_val2 = v; } /*normalize evey images with the same constants*/ max_val = (max_val2>max_val1)?max_val2:max_val1; min_val = (min_val2<min_val1)?min_val2:min_val1; strcpy(file_name,outprefix); strcat(file_name,"_imgC.pgm"); write_pgm_normalize_given_minmax_float(file_name, imgC->val, imgC->ncol, imgC->nrow, min_val, max_val); strcpy(file_name,outprefix); strcat(file_name,"_imgC.txt"); write_ascii_imageFloat (imgC, file_name); strcpy(file_name,outprefix); strcat(file_name,"_imgW.pgm"); write_pgm_normalize_given_minmax_float(file_name,imgW->val, imgW->ncol, imgW->nrow, min_val, max_val); strcpy(file_name,outprefix); strcat(file_name,"_imgW.txt"); write_ascii_imageFloat (imgW, file_name); strcpy(file_name,outprefix); strcat(file_name,"_mask.txt"); write_ascii_imageFloat (imgMask, file_name); strcpy(file_name,outprefix); strcat(file_name,"_mask.pgm"); write_pgm_normalize_float(file_name,imgMask->val, imgMask->ncol, imgMask->nrow); /*Compute difference image */ imgx = new_imageFloat(psf_ncol,psf_nrow); memcpy (imgx->val, k, psf_ncol * psf_nrow * sizeof (float)); imgCx = convol(imgC, imgx); /*Subsampling & Difference*/ imgCxs = new_imageFloat(imgW->ncol, imgW->nrow); for(i=0;i<imgW->nrow;i++) for(j=0;j<imgW->ncol;j++) if(imgMask->val[i*imgW->ncol + j]) imgCxs->val[i*imgCxs->ncol +j] = imgCx->val[s*i*imgCx->ncol + s*j] -imgW->val[i*imgW->ncol + j]; else imgCxs->val[i*imgCxs->ncol +j] = 0; /*The normalization of the image difference is done with so that the values are in [-0.05(max_val-min_val),0.05(max_val-min_val)] (i.e. the dynamical range is compressed to 10%)*/ strcpy(file_name,outprefix); strcat(file_name,"_diff.pgm"); write_pgm_normalize_given_minmax_float(file_name,imgCxs->val, imgCxs->ncol, imgCxs->nrow, -0.05*(max_val - min_val), 0.05*(max_val - min_val)); strcpy(file_name,outprefix); strcat(file_name,"_diff.txt"); write_ascii_imageFloat (imgCxs, file_name); } free_imageFloat (imgMask); free_imageFloat (imgW); free_imageFloat (imgC); free_imageFloat (imgP); free_imageFloat (z_LR); free_imageFloat (z_HR); free ((void *) p_HR); free ((void *) p_LR); free ((void *) A); free ((void *) b); }
void AmpEst(float *ampest, WavePar WP, complex *Refl, int nx, int nt, int nxs, int nts, float dt, float *xsyn, int Nsyn, float *xrcv, float *xsrc, float fxs2, float fxs, float dxs, float dxsrc, float dx, int ixa, int ixb, int ntfft, int nw, int nw_low, int nw_high, int mode, int reci, int nshots, int *ixpossyn, int npossyn, float *pmin, float *f1min, float *f1plus, float *f2p, float *G_d, int *muteW, int smooth, int shift, int above, int pad, int nt0, int *synpos, int verbose) { int l, i, j, ix, iw, nfreq, first=0; float Amax, *At, *wavelet, *iRN, *f1d, *Gp, Wmax, *Wt, *f1dw, Am, Wm; complex *Gdf, *f1df, *Af, *Fop; double tfft; nfreq = ntfft/2+1; wavelet = (float *)calloc(ntfft,sizeof(float)); Gdf = (complex *)malloc(nfreq*sizeof(complex)); f1df = (complex *)malloc(nfreq*sizeof(complex)); Af = (complex *)calloc(nfreq,sizeof(complex)); At = (float *)malloc(nxs*ntfft*sizeof(complex)); Wt = (float *)malloc(nxs*ntfft*sizeof(complex)); Fop = (complex *)calloc(nxs*nw*Nsyn,sizeof(complex)); iRN = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float)); f1d = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float)); f1dw = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float)); Gp = (float *)calloc(Nsyn*nxs*ntfft,sizeof(float)); freqwave(wavelet, WP.nt, WP.dt, WP.fp, WP.fmin, WP.flef, WP.frig, WP.fmax, WP.t0, WP.db, WP.shift, WP.cm, WP.cn, WP.w, WP.scale, WP.scfft, WP.inv, WP.eps, 0); Wmax = maxest(wavelet,WP.nt); if (verbose) vmess("Calculating amplitude"); //memcpy(f1d, G_d, Nsyn*nxs*ntfft*sizeof(float)); mode=-1; synthesis(Refl, Fop, f1min, iRN, nx, nt, nxs, nts, dt, xsyn, Nsyn, xrcv, xsrc, fxs2, fxs, dxs, dxsrc, dx, ixa, ixb, ntfft, nw, nw_low, nw_high, mode, reci, nshots, ixpossyn, npossyn, &tfft, &first, verbose); for (l = 0; l < Nsyn; l++) { for (i = 0; i < npossyn; i++) { j=0; Gp[l*nxs*nts+i*nts+j] = -iRN[l*nxs*nts+i*nts+j] + f1plus[l*nxs*nts+i*nts+j]; for (j = 1; j < nts; j++) { Gp[l*nxs*nts+i*nts+j] = -iRN[l*nxs*nts+i*nts+j] + f1plus[l*nxs*nts+i*nts+nts-j]; } } } applyMute(Gp, muteW, smooth, 2, Nsyn, nxs, nts, ixpossyn, npossyn, shift, pad, nt0); for (l = 0; l < Nsyn; l++) { for (i = 0; i < npossyn; i++) { ix = ixpossyn[i]; j=0; f1d[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j]; f1dw[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j]; for (j = 1; j < nts; j++) { f1d[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+j]; f1dw[l*nxs*nts+i*nts+j] = G_d[l*nxs*nts+ix*nts+nts-j]; } } } /*for (l = 0; l < Nsyn; l++) { for (i = 0; i < npossyn; i++) { ix = ixpossyn[i]; rc1fft(&Gp[l*nxs*ntfft+i*ntfft],Gdf,ntfft,-1); rc1fft(&f1d[l*nxs*ntfft+ix*ntfft],f1df,ntfft,-1); for (iw=0; iw<nfreq; iw++) { Af[iw].r += f1df[iw].r*Gdf[iw].r-f1df[iw].i*Gdf[iw].i; Af[iw].i += f1df[iw].r*Gdf[iw].i+f1df[iw].i*Gdf[iw].r; } } cr1fft(&Af[0],At,ntfft,1); //Amax = maxest(At,ntfft); Amax = At[0]; ampest[l] = (Wmax*Wmax)/(Amax/((float)ntfft)); memset(&Af[0],0.0, sizeof(float)*2*nfreq); vmess("Wmax:%.8f Amax:%.8f",Wmax,Amax); }*/ for (l = 0; l < Nsyn; l++) { Wm = 0.0; Am = 0.0; convol(&Gp[l*nxs*nts], &f1d[l*nxs*nts], At, nxs, nts, dt, 0); convol(&f1dw[l*nxs*nts], &f1d[l*nxs*nts], Wt, nxs, nts, dt, 0); for (i = 0; i < npossyn; i++) { Wm += Wt[i*nts]; Am += At[i*nts]; } ampest[l] = sqrtf(Wm/Am); } if (verbose) vmess("Amplitude calculation finished"); free(Gdf);free(f1df);free(Af);free(At);free(wavelet); free(iRN);free(f1d);free(Gp);free(Fop); return; }