Ejemplo n.º 1
0
array convolve(const array& signal, const array& filter, const convMode mode, convDomain domain)
{
    unsigned sN = signal.numdims();
    unsigned fN = filter.numdims();

    switch(std::min(sN,fN)) {
        case 1: return convolve1(signal, filter, mode, domain);
        case 2: return convolve2(signal, filter, mode, domain);
        case 3: return convolve3(signal, filter, mode, domain);
        default: return convolve3(signal, filter, mode, domain);
    }
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
/*                                     ----- declarations */

   int         i;                /* do loop */

   int         ifx,ify;          /* do loop for filter */


   int        xdim, ydim;       /* x,y-pixel data size */
   int        pdim;             /* total-pixel data size */
   int        fsize;            /* filter size (pixel)*/

   float       *ipix;            /* input pixel array */
   float       *opix;            /* output pixel array */
   float       *gf;              /* gaussian filter array */


   struct imh  imhin={""};             /* imh header structure */
   struct icom icomin={0};             /* imh comment structure */

   struct imh  imhout={""};             /* imh header structure */
   struct icom icomout={0};             /* imh comment structure */

   /* for empty pixels (WHT) */
   int pixignr;

   /* define struct for file pointers */
   FILE *fpin,*fpout;
   char *fnamin,*fnamout;


   float s1in,s2in,a1in,a2in;
   float s1out,s2out,a1out,a2out;
   
   double A1,A2,A3,A4,S1,S2,S3,S4,d,v;

   if(argc!=9)
     {
       printf("Usage: dsmth file_in s1in s2in a1in file_out s1out s2out a2out  \n");
       exit(-1);
     }
   else
     {
       fnamin=argv[1];
       s1in=atof(argv[2]);
       s2in=atof(argv[3]);
       a1in=atof(argv[4]);
       a2in=1.0-a1in;
       fnamout=argv[5];
       s1out=atof(argv[6]);
       s2out=atof(argv[7]);
       a1out=atof(argv[8]);
       a2out=1.0-a1out;
     }

   if ((fpin = imropen(fnamin, &imhin, &icomin)) == NULL)
	{
	  fprintf(stderr,"\a\n Cannot open input file %s\n",fnamin);
	  exit(1);
	}
      xdim  = imhin.npx;
      ydim  = imhin.npy;
      pdim = xdim * ydim;
      ipix = (float*)calloc(pdim, sizeof(float));
      
      printf("\n> %s reading\n",fnamin);    
      if (imrall_tor(&imhin, fpin, ipix, pdim) == 0)
	{
	  fprintf(stderr,"\a\n Cannot read file \n");     
	  exit(1);
	}
      pixignr = imget_pixignr( &imhin );
      printf("smth:  %s PixIgnor= %d\n",fnamin,pixignr);

      /*                          ----- make a smoothing filter */
 

   /************ FILTER making up! **************/

   /* Function is  \Sum An/sqrt(Sn)*exp(-0.5*r^2/Sn) */
   
   A1=(a1out*s1out)/(a1in*s1in);
   A2=(a2out*s2out)/(a1in*s1in);
   A3=-(a1out*s1out)/(a1in*s1in)*(a2in*s2in)/(a1in*s1in);
   A4=-(a2out*s2out)/(a1in*s1in)*(a2in*s2in)/(a1in*s1in);
   S1=(s1out*s1out-s1in*s1in);
   S2=(s2out*s2out-s1in*s1in);
   S3=(s1out*s1out+s2in*s2in)-2.0*(s1in*s1in);
   S4=(s1out*s1out+s2in*s2in)-2.0*(s1in*s1in);
   d=(a2in*s2in)/(a1in*s1in)*exp(-M_PI*S1);
   
   if(S1<0 || S2<0 || S3<0 || S4<0 || 
          d*d>0.05 /* <---- */
      )
     {
       printf("Cannot smooth, we need deconvolution! \n");
       exit(-1);
     }

   printf("%f %f %f %f %f\n",A1,A2,A3,A4,A1+A2+A3+A4);
   printf("%f %f %f %f \n",S1,S2,S3,S4);

   /* renormalize */
   v=A1+A2+A3+A4;
   A1/=v;
   A2/=v;
   A3/=v;
   A4/=v;

   printf("%f %f %f %f %f\n",A1,A2,A3,A4,A1+A2+A3+A4);
   printf("%f %f %f %f \n",S1,S2,S3,S4);

   /* fsize(=2n+1), gf[] */
   for(i=1;i<101;i++)
     {
       v=gauss4(A1,S1,A2,S2,A3,S3,A4,S4,(double)i);
       if (v*PTPARAM<1.) break;
     }
   fsize=2*i+1;

   imh_inherit(&imhin,&icomin,&imhout,&icomout,fnamout);
   imclose(fpin,&imhin,&icomin);


   if (fsize == 1)
     {
       printf("\a\n *input seeing is near to output one \n");
       printf(" there is no need to smooth the image \n");
       opix=ipix;
     }
   else
     {
       printf("                  size = %d pix\n",fsize);
       
       /* Make filter */
       
       gf = (float*) gauss4flt(A1,S1,A2,S2,A3,S3,A4,S4,fsize);
       
       printf("\n> filter map \n");
       for (ify=0; ify<fsize; ify++) 
	 {
	   for (ifx=0; ifx<fsize; ifx++) 
	     {
	       printf(" %7.5f",gf[ify*fsize+ifx]);
	     }
	   printf("\n");
	 }      
       
       /*                               ----- smoothing the file */
       
       printf("\n> %s smoothing\n",fnamin);
       opix = (float*) calloc(pdim, sizeof(float));
       
       /* Convolve */
       /* 2002/06/06 replaced with convolve2 */
       convolve2(xdim,ydim,ipix, opix, fsize, gf,pixignr);
       
       free(ipix);
       free(gf);

       /*                                ----- write output file */
       
       /* 2002/06/06 updated */
       /*
	 (void) sprintf( line, "DSMTH:  filter size=%d",fsize);
	 (void) imaddcom( &icomout, line );
       */
       imaddhistf(&icomout,"DSMTH: (%f %f %f) => (%f %f %f)",
		  s1in,s2in,a1in,s1out,s2out,a1out);
       imaddhistf(&icomout,"DSMTH: filter size=%d",fsize);
     }

   if( (fpout=imwopen(fnamout, &imhout, &icomout)) == NULL )
     {
       fprintf(stderr,"Cannot open output file \n");
       exit(1);
     }
   
   printf("\n> %s writing --------------\n",fnamout); 

   imwall_rto(&imhout, fpout, opix);

   
   imclose(fpout,&imhout,&icomout);
   free(opix);
   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 
   return 0;
}