www.pudn.com > source.zip > segment.cpp


#include "gui.h" 
#include "randomc.h" 
 
double small_class_punish_fn (double class_percentage)  
{ 
  return pow (10*class_percentage,-3.0); 
} 
 
void ImageOperations::InitOutImage() 
{ 
  int i;//, j; 
  T_color_init       = COLOR_INIT_TEMP; 
  T_combine_init     = COMBINE_INIT_TEMP; 
  T_texture_init     = TEXTURE_INIT_TEMP; 
  T_color = T_color_init; 
  T_combine = T_combine_init; 
  T_texture = T_texture_init; 
 
  criteria      = 0.01; 
  R             = 1.0; 
  R1            = 0.5; 
  alpha_combined = 1.0; 
 
  Gamma = new double[10]; 
  Beta =  new double[10]; 
  for(i = 0; i < 10; i++){ 
	  Gamma[i] = 1.5; 
	  Beta[i] = 10; 
  } 
 
 
 
    no_combined_region = no_color_region * no_texture_region; 
	log_prior = new double[no_combined_region]; 
	for (i = 0; i < no_combined_region; i++) 
		log_prior[i] = small_class_punish_fn(1 / no_combined_region); 
	log_numclass_prior = log(sqrt(2.0 * M_PI * L_conf)) + 
								0.5 * (L_conf - L_mean) * 
    (L_conf - L_mean) / L_conf;	 
	 
 
	beta_color = 10; 
	beta_texture = 10; 
 
 
	K_color = new double[no_color_region]; 
	K_texture = new double[no_texture_region]; 
     
    for (i = 0; i < no_color_region; i++){ 
	  K_color[i] = log(sqrt(pow(2.0 * M_PI, 3) * 
		  DetColor[i])); 
	} 
 
	for (i = 0; i < no_texture_region; i++){ 
	  K_texture[i] = log(sqrt(pow(2.0 * M_PI, 3) * 
		  DetTexture[i])); 
	} 
 
 
    /* Allocate corresponding label field */ 
 
	Color_Layer = new gray*[width]; 
	Texture_Layer = new gray*[width]; 
	X_Layer = new gray*[width]; 
	for (i = 0; i < width; i++){ 
		Color_Layer[i]= new gray[height]; 
		Texture_Layer[i]= new gray[height]; 
		X_Layer[i]= new gray[height]; 
	} 
	iterations = 0; 
 
	ml(height, width, no_color_region, Color_Layer, T_color, COLOR); 
	ml(height, width, no_texture_region, Texture_Layer, T_texture, TEXTURE); 
	ml(height, width, no_combined_region, X_Layer, T_combine, COMBINED); 
 
	CreateOutput(dim2(X_Layer,COMBINED)); 
	CreateOutput2(dim2(Color_Layer,COLOR)); 
	CreateOutput3(dim2(Texture_Layer,TEXTURE)); 
 
 
 
} 
 
 
 
 
int ImageOperations::ml(int num_rows, int num_cols, int num_classes, 
	gray **label, double T,int layer) 
{ 
  int      row, col, Class; 
  double   energy, e; 
 
  	   
		  for (row = 0; row < num_rows; row++){ 
			  for (col = 0; col < num_cols; col++){ 
				if(layer == COLOR){ 
					energy = get_color_pot_1_order(f, row, col, 0);				 
					for (label[col][row] = 0, Class = 1; Class < num_classes; Class++){ 
						if ((e = get_color_pot_1_order(f, row, col, Class)) < energy) { 
							energy             = e; 
							label[col][row] = Class; 
						} 
					} 
				} 
				if(layer == TEXTURE){ 
					energy = get_texture_pot_1_order(f, row, col, 0);				 
					for (label[col][row] = 0, Class = 1; Class < num_classes; Class++){ 
						if ((e = get_texture_pot_1_order(f, row, col, Class)) < energy) { 
							energy             = e; 
							label[col][row] = Class; 
						} 
					} 
				} 
				if(layer == COMBINED){ 
					/*energy = local_combined_interclue_energy(f, row, col, 0);				 
					for (label[col][row] = 0, Class = 1; Class < num_classes; Class++){ 
						if ((e = local_combined_interclue_energy(f, row, col, Class)) < energy) { 
							energy             = e; 
							label[col][row] = Class; 
						} 
					}*/ 
					label[col][row] =  (Texture_Layer[col][row] * no_color_region)+Color_Layer[col][row]; 
					//label[col][row] =  (col + row) % no_combined_region; 
 
				} 
			  } 
		   
	   
  } 
  return(0); 
}  
 
void ImageOperations::CalculateMeanAndVariance(int region) 
{ 
  wxString txt = "";  
   
  if (in_image != NULL) 
    { 
 
	   
	   
      int x, y, w, h; 
      int i, j; 
      ((MyFrame *)frame)->GetRegion(x, y, w, h, region); 
 
      double sumL = 0, sum2=0, sumU = 0, sumV = 0; 
	  double sum3 = 0; 
 
	   
      for (i=y; igaussians->Clear(); 
	  txt = ""; 
	  txt << "Color Selection no" << region+1 << "\n"; 
	  txt << "Component\tMean\t\tVariance\n"; 
	  ((MyFrame *)frame)->gaussians->AppendText(txt); 
 
	  txt = ""; 
      txt << "L" << "\t\t" << ColorMean[0][region] << "\t\t" << ColorVariance[0][0][region] << "\n"; 
	  ((MyFrame *)frame)->gaussians->AppendText(txt); 
	  txt = ""; 
	  txt << "U" << "\t\t" << ColorMean[1][region] << "\t\t" << ColorVariance[1][1][region] << "\n"; 
	  ((MyFrame *)frame)->gaussians->AppendText(txt); 
	  txt = ""; 
	  txt << "V" << "\t\t" << ColorMean[2][region] << "\t\t" << ColorVariance[2][2][region] << "\n"; 
	  ((MyFrame *)frame)->gaussians->AppendText(txt); 
	   
	 
 
	  for (i=0; i<3; i++){ 
		for (j=0; j<3; j++) { 
			temp1[i+1][j+1] = ColorVariance[i][j][region]; 
			if(i == j){ 
				temp2[i+1][j+1] = 1; 
			}else{ 
				temp2[i+1][j+1] = 0; 
			} 
		} 
	  } 
		int d = 1; 
		dekompf(temp1,temp2,3,&d); 
		inverz(temp1, temp2, 3);  
		DetColor[region] = det(temp1,3,d); 
		 
		for (i=0; i<3; i++){ 
			for (j=0; j<3; j++) { 
				InvColor[i][j][region] = temp2[i+1][j+1]; 
			} 
 
		}	 
 
    } 
} 
 
void ImageOperations::CalculateMeanAndVariance2(int region,int region2) 
{ 
	wxString txt = "";  
	if (in_image != NULL) 
    { 
      int x, y, w, h; 
      int i, j; 
      ((MyFrame *)frame)->GetRegion(x, y, w, h, region); 
	  int rgn; 
	  rgn = region-region2;	   
 
      double sum = 0, sum2=0; 
	  double sum3 = 0; 
  
	  for(int k = 0; k < no_g_feature; k++){ 
		  sum=0; 
		  for (i=y; igaussians->Clear(); 
	  txt = ""; 
	  txt << "Gabor Selection no" << rgn+1 << "\n"; 
	  txt << "Component\tMean\t\tVariance\n"; 
	  ((MyFrame *)frame)->gaussians->AppendText(txt); 
	   
	  for(int l=0; l < no_g_feature; l++){ 
		for(int k=0; k < no_g_feature; k++){ 
		  sum2 = 0; 
 
		  for (i=y; igaussians->AppendText(txt); 
	  } 
 
		int d = 1; 
		for (i=0; i 0.0) { 
			  class_count++; 
			  log_prior[i] = small_class_punish_fn(log_prior[i] 
							/(width * height)); 
			} 
			else 
				log_prior[i] = MAXDOUBLE; 
 
		  log_numclass_prior = log(sqrt(2.0 * M_PI * L_conf)) + 
								0.5 * (L_conf - L_mean) * 
								(L_conf - L_mean) / L_conf; 
 
			result = 0; 
 
			 
			int oldr; 
			for (i=0; i local_color_energy4(1, i, j, r)) 
						{							 
								Color_Layer[j][i] = r;				 
							 
						} 
					 } 
					 if(Color_Layer[j][i] != oldr){changed_pix++; } 
 
					 oldr = Texture_Layer[j][i]; 
					 for (r=0; r local_texture_energy4(1, i, j, r)) 
						{							 
								Texture_Layer[j][i] = r;			 
							 
						} 
					 } 
					 if(Texture_Layer[j][i] != oldr){changed_pix++; } 
 
					 oldr = X_Layer[j][i]; 
					 for (r=0; r local_combined_energy(1, i, j, r)) 
						{							 
								X_Layer[j][i] = r;							 
						} 
					 } 
					 if(X_Layer[j][i] != oldr){changed_pix++;} 
				 
			} 
			EC = global_color_energy(); 
			ET = global_texture_energy(); 
			EX = global_combined_energy(); 
 
			summa_deltaEC += fabs(E_oldC-EC); 
			summa_deltaET += fabs(E_oldT-ET); 
			summa_deltaEX += fabs(E_oldX-EX); 
			E_oldC = EC; 
			E_oldT = ET; 
			E_oldX = EX; 
 
			 
			global_en = EX; 
			iterations++; 
			Ced_Pix = changed_pix; 
			frame->Update(); 
			frame->Refresh(); 
			 
			CreateOutput(dim2(X_Layer,COMBINED)); 
			CreateOutput2(dim2(Color_Layer,COLOR)); 
			CreateOutput3(dim2(Texture_Layer,TEXTURE)); 
 
		 
		}while ((changed_pix >= maxpixel_value) && (max_iterations > iterations));//&&(summa_deltaET > t)&&(summa_deltaEX > t)); 
} 
 
 
void ImageOperations::Metropolis(bool mmd) 
{ 
  InitOutImage(); 
  int changed_pix = 0; 
  wxString txt; 
  int i, j; 
  int r; 
  double alpha = 0.1; 
  double kszi = log(alpha);  // This is for MMD. When executing 
			    // Metropolis, kszi will be randomly generated. 
 // double summa_deltaE; 
  TRandomMersenne rg(time(0));  // create instance of random number generator 
	double summa_deltaEC; 
	double summa_deltaET; 
	double summa_deltaEX; 
	double E_oldC, E_oldT, E_oldX; 
	double EC, ET, EX; 
 
	 
	 
	double t = 20; 
	E_oldC = global_color_energy(); 
	E_oldT = global_texture_energy(); 
	E_oldX = global_combined_energy();	 
 
	do { 
		 
		summa_deltaEC = 0.0; 
		summa_deltaET = 0.0; 
		summa_deltaEX = 0.0; 
		changed_pix = 0; 
		for (i = 0; i < no_combined_region; i++) 
		  log_prior[i] = 0.0; 
		  for (row = 0; row < height; row++){ 
			  for (col = 0; col < width; col++){ 
				  log_prior[X_Layer[col][row]]++; 
			  } 
		  } 
		  int class_count = 0; 
		  for (i = 0; i < no_combined_region; i++) 
			if (log_prior[i] > 0.0) { 
			  class_count++; 
			  log_prior[i] = small_class_punish_fn(log_prior[i] 
							/(width * height)); 
			} 
			else 
				log_prior[i] = MAXDOUBLE; 
 
		  log_numclass_prior = log(sqrt(2.0 * M_PI * L_conf)) + 
								0.5 * (L_conf - L_mean) * 
								(L_conf - L_mean) / L_conf;	 
 
 
   
		  
		 for (i=0; iRefresh(); 
			CreateOutput(dim2(X_Layer,COMBINED)); 
			CreateOutput2(dim2(Color_Layer,COLOR)); 
			CreateOutput3(dim2(Texture_Layer,TEXTURE)); 
			 
 
	}while ((changed_pix >= maxpixel_value) && (max_iterations > iterations)); 
} 
 
 
void ImageOperations::Gibbs() 
{ 
  InitOutImage(); 
  int i, j; 
  int changed_pix = 0; 
  int counter = 0; 
  wxString txt; 
  double *EkC,*EkT,*EkX;		       // array to store local energies 
  int s; 
  double sumEC, sumET, sumEX; 
  double z; 
  double r; 
 
  double summa_deltaEC; 
  double summa_deltaET; 
  double summa_deltaEX; 
  double E_oldC, E_oldT, E_oldX; 
  double EC, ET, EX; 
 
  TRandomMersenne rg(time(0)); // make instance of random number generator 
 
  EkC = new double[no_color_region]; 
  EkT = new double[no_texture_region]; 
  EkX = new double[no_combined_region]; 
 
 
  	double t = 20; 
	E_oldC = global_color_energy(); 
	E_oldT = global_texture_energy(); 
	E_oldX = global_combined_energy(); 
 
  do 
    { 
	  changed_pix = 0; 
      summa_deltaEC = 0.0; 
	  summa_deltaET = 0.0; 
	  summa_deltaEX = 0.0; 
 
	  		for (i = 0; i < no_combined_region; i++) 
		  log_prior[i] = 0.0; 
		  for (row = 0; row < height; row++){ 
			  for (col = 0; col < width; col++){ 
				  log_prior[X_Layer[col][row]]++; 
			  } 
		  } 
		  int class_count = 0; 
		  for (i = 0; i < no_combined_region; i++) 
			if (log_prior[i] > 0.0) { 
			  class_count++; 
			  log_prior[i] = small_class_punish_fn(log_prior[i] 
							/(width * height)); 
			} 
			else 
				log_prior[i] = MAXDOUBLE; 
 
		  log_numclass_prior = log(sqrt(2.0 * M_PI * L_conf)) + 
								0.5 * (L_conf - L_mean) * 
								(L_conf - L_mean) / L_conf; 
 
      for (i=0; i r) // choose new label with probabilty exp(-U/T). 
					{ 
						if(Color_Layer[j][i] != s){ 
							Color_Layer[j][i] = s; 
							changed_pix++;					 
						} 
						break; 
					} 
			  } 
		  } 
      EC = global_color_energy(); 
      summa_deltaEC += fabs(E_oldC-EC); 
      E_oldC = EC; 
 
      T_color *= 0.98;         // decrease temperature 
 
	  for (i=0; i r) // choose new label with probabilty exp(-U/T). 
					{ 
						if(Texture_Layer[j][i] != s){ 
							Texture_Layer[j][i] = s; 
							changed_pix++; 
						} 
						break; 
					} 
			  } 
		  } 
      ET = global_texture_energy(); 
      summa_deltaET += fabs(E_oldT-ET); 
      E_oldT = ET; 
 
      T_texture *= 0.98;         // decrease temperature 
 
	  for (i=0; i r) // choose new label with probabilty exp(-U/T). 
					{ 
						if(X_Layer[j][i] != s){ 
							X_Layer[j][i] = s; 
							changed_pix++; 
						} 
						 
						break; 
					} 
			  } 
		  } 
      EX = global_combined_energy(); 
      summa_deltaEX += fabs(E_oldX-EX); 
      E_oldX = EX; 
 
      T_combine *= 0.98;         // decrease temperature 
	  global_en = EX; 
	  iterations++; 
	  Ced_Pix = changed_pix; 
	  frame->Update(); 
	  frame->Refresh(); 
 
	  CreateOutput(dim2(X_Layer,COMBINED)); 
	  CreateOutput2(dim2(Color_Layer,COLOR)); 
	  CreateOutput3(dim2(Texture_Layer,TEXTURE)); 
    } while ((changed_pix > maxpixel_value) && (max_iterations > iterations)); // stop when energy change is small 
 
  delete EkC; 
  delete EkT; 
  delete EkX; 
}