Пример #1
0
void dgauy_gaux_filter(float sigma){
  int i,j,k,length_gaussian,length_deriv;
  float gaussian[SIZE1],deriv_gaussian[SIZE1];
  matrix dgauy, dgauy_gaux, total_resul, m; 
  float rmin,rmax,rng;

  /* Fill in the image with background color */
  for(i=0; i<IMAGE_WIDTH; i++)
	for(j=0; j<IMAGE_HEIGHT; j++)
	    XPutPixel(theXImage_2,i,j,bg);

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(draw_1),XtWindow(draw_2),0,0,0,0,True);

  /* Associate the watch cursor with the main window */
  XDefineCursor(XtDisplay(draw_1), XtWindow(main_window), theCursor);

  /* Flush the request buffer and wait for all events */
  /* and errors to be processed by the server.        */
  XSync(XtDisplay(draw_1), False);
  
  get_gaussian(sigma,gaussian,&length_gaussian);

  get_derigaussian(sigma,deriv_gaussian,&length_deriv);
 
  for(i=0;i<IMAGE_WIDTH;i++)
    for(j=0;j<IMAGE_HEIGHT;j++)
       m[i][j]= XGetPixel(theXImage_1,i,j);

  /* Calculates convolution of the image with the derivative of a */
  convo_vectory(m,deriv_gaussian,length_deriv,&rmax,&rmin,dgauy);


  /*Calculate the smoothing of a Gaussian(y) in x direction */
  convo_vectorx(dgauy,gaussian,length_gaussian,&rmax,&rmin,
		dgauy_gaux);

  rng=(rmax-rmin);

  for(i=0;i<IMAGE_WIDTH;i++)
	for(j=0;j<IMAGE_HEIGHT;j++)
		total_resul[i][j]=sqrt(pow(dgauy_gaux[i][j],2));		

  for(i=0;i<IMAGE_WIDTH;i++)
	for(j=0;j<IMAGE_HEIGHT;j++)
        XPutPixel(theXImage_2,i,j,(unsigned long)
		 (((float)(total_resul[i][j]-rmin)/rng)*255.0));
  
  /* Copy image into pixmap */
  XPutImage(XtDisplay(draw_2), thePixmap_2,image_gc_2, theXImage_2,
		 0, 0, 0, 0, theXImage_2->width, theXImage_2->height);
  
  /* Disassociate the watch cursor from the main window */
  XUndefineCursor(XtDisplay(draw_1), XtWindow(main_window));

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(draw_2),XtWindow(draw_2),0,0,0,0,True);

}
Пример #2
0
void bptt_update_connections(Net *net,Connections *c,int t)
{
  Group *gfrom,*gto;
  Real *invec,*outvec,*w,dot,*outs,weight;
  int i,j,nto,nfrom,d;

  /* if we reset activation and t=0, don't do anything */
  if (c->to->resetActivation==1 && t==0)
    return;
  gfrom=c->from;
  gto=c->to;

  invec=gto->inputs[t];
  /* we won't be here with t==0 unless we're not resetting */
  nto=gto->numUnits;
  nfrom=gfrom->numUnits;
  
  if (c->weightNoiseType==NO_NOISE)
    {
      for(i=0;i<nto;i++)
	{
	  if (t==0)  /* already zeroed if t=0 */
	    {
	      d=net->time-1;
	    }
	  else d= t - gto->delays[i];
	  if (d>=0) 
	    {
	      outvec=gfrom->outputs[d];
	      w=c->weights[i];
	      dot=0.0;
	      outs=outvec;
	      for(j=0;j<nfrom;j++)
		{
		  dot += *w++ * *outs++;
		}
	      invec[i] +=dot;
	    }
	}
    }
  else if (c->weightNoiseType==ADDITIVE_NOISE)
    {
      for(i=0;i<nto;i++)
	{
	  if (t==0)  /* already zeroed if t=0 */
	    {
	      d=net->time-1;
	    }
	  else d= t - gto->delays[i];
	  if (d>=0)
	    {
	      outvec=gfrom->outputs[d];
	      w=c->weights[i];
	      dot=0.0;
	      outs=outvec;
	      for(j=0;j<nfrom;j++)
		{
		  weight = *w++ + 
		    (get_gaussian() * c->weightNoise);
		  dot += weight * *outs++;
		}
	      invec[i] +=dot;
	    }
	}
    }
  else if (c->weightNoiseType==MULTIPLICATIVE_NOISE)
    {
      for(i=0;i<nto;i++)
	{
	  if (t==0)  /* already zeroed if t=0 */
	    {
	      d=net->time-1;
	    }
	  else d= t - gto->delays[i];
	  if (d>=0)
	    {
	      outvec=gfrom->outputs[d];
	      w=c->weights[i];
	      dot=0.0;
	      outs=outvec;
	      for(j=0;j<nfrom;j++)
		{
		  weight = *w++ * 
		    (1.0 + (get_gaussian() * c->weightNoise));
		  dot += weight * *outs++;
		}
	      invec[i] +=dot;
	    }
	}
    }
  else 
    Error0("Unknown weightNoiseType");
}
Пример #3
0
void dbm_forward(Net *net,Example *ex,int doclamp)
{
  int i,t,nu,nt,ng;
  int nc,c,g;
  Group *gto;
  float prev;

  nt=net->time;
  ng=net->numGroups;
  nc=net->numConnections;
  for(t=0;t<nt;t++)
    {
      /* first, zero out the input field */
      for(g=0;g<ng;g++)
	{
	  gto=net->groups[g];
	  nu=gto->numUnits;
	  for(i=0;i<nu;i++)
	    {
	      gto->inputs[t][i]=0;
	    }
	  gto->temperature = 
	    1.0/(gto->temperature + gto->temporg * 
		 pow(gto->tempmult,t));
	}
      
      for(c=0;c<nc;c++)
	{
	  if (!is_group_in_net(net->connections[c]->from,net))
	    Error1("dbm_update: Group %s is not in net",
		   net->connections[c]->from->name);

	  if (!is_group_in_net(net->connections[c]->to,net))
	    Error1("dbm_forward: Group %s is not in net",
		   net->connections[c]->to->name);
	  
	  dbm_update_connections(net,net->connections[c],t);
	}
      
      /* now, having computed all the inputs from the
	 previous time tick's outputs, update the current
	 time tick's outputs from its inputs (got that?). */
      for(g=0;g<ng;g++)
	{
	  gto=net->groups[g];
	  apply_example_clamps(ex,gto,t);
	  nu=gto->numUnits;
	  for(i=0;i<nu;i++)
	    {
	      /* is it a bias */
	      if (gto->bias)
		gto->outputs[t][i]=gto->biasValue;
	      /* does it have a target */
	      else if (VAL(gto->exampleData[i]))
		{
		  gto->outputs[t][i]=gto->exampleData[i];
		  if (gto->clampNoise != 0)
		    {
		      gto->outputs[t][i] += 
			gto->clampNoise * get_gaussian();
		      gto->outputs[t][i]=
			clip_output(gto,gto->outputs[t][i]);
		    }
		}
	      else if (VAL(gto->exampleData[i]) &&
		       (doclamp == CLAMP_TARGETS))
		{
		  gto->outputs[t][i]=gto->exampleData[i];
		  if (gto->clampNoise != 0)
		    {
		      gto->outputs[t][i] += 
			gto->clampNoise * get_gaussian();
		      gto->outputs[t][i]=
			clip_output(gto,gto->outputs[t][i]);
		    }
		}
	      /* else compute normally */
	      else
		{
		  if (t==0) prev=0.0;
		  else prev = gto->outputs[t-1][i];
		  
		  gto->outputs[t][i] = 
		    (1.0-net->integrationConstant) * prev + 
		    net->integrationConstant * 
		    bptt_unit_activation(gto,i,t);
		  
		  if (gto->activationNoise != 0)
		    {
		      gto->outputs[t][i] 
			+= gto->activationNoise * get_gaussian();
		      gto->outputs[t][i]=
			clip_output(gto,gto->outputs[t][i]);
		    }
		}
	    }
	}
    }
  return;
}
Пример #4
0
/* total Gradient= sqrt(pow(dgauxgauy,2)+pow(dgauygaux,2)) */
void total_gradient_filter(float sigma){
  int i,j,k,length_gaussian,length_deriv;
  float gaussian[SIZE1],deriv_gaussian[SIZE1];
  matrix dgaux, dgauy, dgaux_gauy, dgauy_gaux,
	 total_gradient, m;
  float rmin,rmax,rng;
  if (yhs_files_open == 0 || yhs_filename[0] == 0 || yhs_files_open > 19 || running == 1) return;
running=1;
XtUnmapWidget(sigma_dialog);
XtUnmanageChild(sigma_dialog);
XFlush(XtDisplay(sigma_dialog));

fprintf(stderr,"\nProcessing filter...");
  get_gaussian(sigma,gaussian,&length_gaussian);

  get_derigaussian(sigma,deriv_gaussian,&length_deriv);

  for(i=0;i<IMAGE_WIDTH;i++)
    for(j=0;j<IMAGE_HEIGHT;j++)
       m[i][j]= array1[i][j]; 

  /* calculates the convolution of the image with the derivative of a */
  /* Gaussian for the rows */
  convo_vectorx(m,deriv_gaussian,length_deriv,&rmax,&rmin,dgaux);

  /*convo_vectorx(m,gaussian,length_gaussian,&rmax,&rmin,gaussian_x);
  convo_vectory(gaussian_x,deriv_gaussian,length_deriv,&rmax,&rmin,final_gaussian_x);*/

  /* calculate the smoothing of a gaussian(x) in y direction. */
  convo_vectory(dgaux,gaussian,length_gaussian,&rmax,&rmin,
		dgaux_gauy);

  fprintf(stderr,"Maximum is %f, Minimum is %f..",rmax,rmin);
 
  /* calculates the convolution of the image with the derivative of a */
  /* gaussian for the columns */
  convo_vectory(m,deriv_gaussian,length_deriv,&rmax,&rmin,dgauy);

   /* convo_vectory(m,gaussian,length_gaussian,&rmax,&rmin,gaussian_y);
    convo_vectorx(gaussian_y,deriv_gaussian,length_deriv,&rmax,&rmin,final_gaussian_y);*/


  /* calculates the smoothing of a gaussian(y) in x direction */
  convo_vectorx(dgauy, gaussian, length_gaussian, &rmax, &rmin,
		dgauy_gaux);

  for(i=0;i<IMAGE_WIDTH;i++)  
    for(j=0;j<IMAGE_HEIGHT;j++){
	total_gradient[i][j]=sqrt(pow(dgaux_gauy[i][j],2)+
				  pow(dgauy_gaux[i][j],2));
	if (i==0 && j==0){rmax = total_gradient[0][0];
			  rmin = rmax;}
        if (total_gradient[i][j]>rmax) rmax=total_gradient[i][j];
        if (total_gradient[i][j]<rmin) rmin=total_gradient[i][j];
  }
  
  rng=(rmax-rmin);  

  for(i=0;i<IMAGE_WIDTH;i++)
    for(j=0;j<IMAGE_HEIGHT;j++)
        array2[i][j]=(int)(((float)(total_gradient[i][j]-rmin)/rng)*255.0);
  
   /* Contorno de la Imagen a negro */
     for(i=0; i<IMAGE_WIDTH; i++)
	for(j=0; j< IMAGE_HEIGHT; j++) {
		array2[i][0]=0;
		array2[i][1]=0;
		array2[i][511]=0;
		array2[0][j]=0;
		array2[1][j]=0;
		array2[511][j]=0;
	  };
fprintf(stderr,"..completed.\nProcessing watershed...");
watershed_callback(NULL,NULL,NULL);
fprintf(stderr,"..completed.\n");
running=0;
}
Пример #5
0
/* Procedure that aply a gaussian matrix filter to the image */
void gaussian_filter(float sigma){
  int i,j,start,length_gaussian,lon;
  float value, rmax,rmin,rng, mask_gaussian[SIZE2][SIZE2];
  float gaussian[SIZE1];
  matrix m,resul;

  /* Fill in the image with background color */
  for(i=0; i<IMAGE_WIDTH; i++)
	for(j=0; j<IMAGE_HEIGHT; j++)
	    XPutPixel(theXImage_2,i,j,bg);

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(draw_1),XtWindow(draw_2),0,0,0,0,True);

  /* Associate the watch cursor with the main window */
  XDefineCursor(XtDisplay(draw_1), XtWindow(main_window), theCursor);

  /* Flush the request buffer and wait for all events */
  /* and errors to be processed by the server.        */
  XSync(XtDisplay(draw_1), False);

  /* Storing the matrix we want to process in m */
  for(i=0;i<IMAGE_WIDTH;i++)
    for(j=0;j<IMAGE_HEIGHT;j++)
       m[i][j]= XGetPixel(theXImage_1,i,j);

  get_gaussian(sigma, gaussian, &length_gaussian);

  fprintf(stderr,"la long es %i \n",length_gaussian);

  /* Only the values higher than 0.01 are desirable */
  start=0;
  value=pow(gaussian[0],2);
  while (value<0.01){
        start += 1;
  	value = pow(gaussian[start],2);
  }	
     
  for(i=start;i<=length_gaussian-start;i++)
     for(j=start;j<=length_gaussian-start;j++){
	   mask_gaussian[i-start][j-start]= gaussian[i]*gaussian[j];	  
           fprintf(stderr,"la %i,%i es %f \n",i-start,j-start,
		mask_gaussian[i-start][j-start]);

	 }

  length_gaussian = length_gaussian - (2*start);

  fprintf(stderr,"la long es %i \n",length_gaussian);

  convolution(m, mask_gaussian, length_gaussian, &rmax, &rmin, resul);

  rng = (float) (rmax - rmin);

  lon = length_gaussian/2;
  
  /* Now compute the convolution, scaling. */
  for (i=lon; i<IMAGE_WIDTH-lon; i++)
   for (j=lon; j<IMAGE_HEIGHT-lon; j++)
        XPutPixel(theXImage_2,i,j,
		(unsigned long) (((float)(resul[i][j]-rmin)/rng)*255.0));
  
  /* Copy image into pixmap. */
  XPutImage(XtDisplay(draw_2), thePixmap_2,image_gc_2, theXImage_2,
	0, 0, 0, 0, theXImage_2->width, theXImage_2->height);
  
  /* Disassociate the watch cursor from the main window */
  XUndefineCursor(XtDisplay(draw_1), XtWindow(main_window));

  /* Clear the drawing window so the image is displayed again */
  XClearArea(XtDisplay(draw_2),XtWindow(draw_2),0,0,0,0,True);

}