[GRASS-SVN] r68682 - sandbox/bo/i.segment.gsoc2016/i.segment

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 14 09:49:20 PDT 2016


Author: hao2309
Date: 2016-06-14 09:49:20 -0700 (Tue, 14 Jun 2016)
New Revision: 68682

Modified:
   sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
   sandbox/bo/i.segment.gsoc2016/i.segment/open_files.c
   sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
Log:
added seg2 for the shifted value

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-14 14:40:47 UTC (rev 68681)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-14 16:49:20 UTC (rev 68682)
@@ -41,9 +41,6 @@
     double weight, sum_of_weights, diff2;
 	double min_val, max_val;
 
-	double ms_spatial_bandwidth = 3;
-	double ms_range_bandwidth = 0.1;
-	
 	/* G_fatal_error(_("Mean shift is not yet implemented"));
     return FALSE; */
 	
@@ -54,15 +51,22 @@
     /* TODO: need another segment structure holding output
      * initially, input and output are original band values */
 	/* set the pointers */
-	globals->bands_in = &globals->bands_seg; 
+	globals->bands_in = &globals->bands_seg2;
 	globals->bands_out = &globals->bands_seg;
+	/* bands_seg hold the input value actually, but in and out are change each time before the iteration starts */
 	
 	/*window size in pixel number*/
-	window_size = ms_spatial_bandwidth*2+1;
+	window_size = globals->ms_spatial_bandwidth*2+1;
     /* alpha2 = globals->alpha * globals->alpha; 
-	use local threshold for temporal testing,*/
-    alpha2 = 0.0001;
 	
+	 /*******************************************************
+     *             Part 1 shift values                      *
+     *    Use mean-shift filter pass through the input      *
+     *******************************************************/
+	
+	/*use local threshold for temporal testing,*/
+	alpha2 = 0.0001;
+	
     t = 0;
     n_changes = 1;
     while (t < globals->end_t && n_changes > 0) {
@@ -127,22 +131,23 @@
 		/* calculate new band values */
 		sum_of_weights = 0;
 		
-		G_message(_("window_size is: %d"),window_size);
-		G_message(_("window_row_north is: %d"),window_row_north);
-		G_message(_("window_row_south is: %d"),window_row_south);
-		G_message(_("window_col_west is: %d"),window_col_west);
-		G_message(_("window_col_east is: %d"),window_col_east);
+		// G_message(_("globals->ms_spatial_bandwidth is: %f"),globals->ms_spatial_bandwidth);
+		// G_message(_("globals->ms_range_bandwidth is: %f"),globals->ms_range_bandwidth);
+		// G_message(_("window_row_north is: %d"),window_row_north);
+		// G_message(_("window_row_south is: %d"),window_row_south);
+		// G_message(_("window_col_west is: %d"),window_col_west);
+		// G_message(_("window_col_east is: %d"),window_col_east);
 		
 		/* get the current pixel value */
 		Segment_get(globals->bands_in, (void *)Rin.mean, row, col);
-		G_message(_("row: %d, col: %d, Rin.mean is: %f"),row,col,Rin.mean[0]);
+		// G_message(_("row: %d, col: %d, Rin.mean is: %f"),row,col,Rin.mean[0]);
 		
 		/* start of the moving window */
 		for (current_window_row = window_row_north;current_window_row<window_row_south;current_window_row++){
 			for (current_window_col = window_col_west;current_window_col<window_col_east;current_window_col++){
 				weight = 1;
 				/* adapt initial spatial and range bandwidths */
-				if (pow((current_window_row - row),2)+pow((current_window_col - col),2) <= pow(ms_spatial_bandwidth,2)){
+				if (pow((current_window_row - row),2)+pow((current_window_col - col),2) <= pow(globals->ms_spatial_bandwidth,2)){
 					
 					/* get the current moving window pixel value */
 					Segment_get(globals->bands_in, (void *)Rwin.mean,current_window_row, current_window_col);
@@ -150,12 +155,12 @@
 					// G_message(_("row: %d, col: %d, Rwin is: %f"),row,col,Rwin.mean[0]);
 
 					/* check range bandwidth */
-					if ((globals->calculate_similarity)(&Rin, &Rwin, globals) <= pow((ms_range_bandwidth * globals->max_diff),2)){
-						/* I tested sum_of_weights should be place out of loop because only the same index band pixels will take part in the calculation(Rout.mean[n] += Rwin.mean[n]*weight) */
+					if ((globals->calculate_similarity)(&Rin, &Rwin, globals) <= pow((globals->ms_range_bandwidth * globals->max_diff),2)){
+						/* sum_of_weights should be place out of loop */
 						sum_of_weights += weight;
 						int n = globals->nbands - 1;
-						do{
-							weight *=1;/* later can apply gaussian kernal */
+						weight *=1;
+						do{;/* later can apply gaussian kernal */
 							Rout.mean[n] += Rwin.mean[n]*weight;
 						} while(n--);
 					}
@@ -171,8 +176,7 @@
 		G_message(_("row: %d, col: %d, Rout.mean is: %f"),row,col,Rout.mean[0]);
 		/* write the output to bands_out*/
 		Segment_put(globals->bands_out, (void *)Rout.mean, row, col);
-		/* Segment_put(&globals->bands_seg2, (void *)Rout.mean, row, col); */
-		
+
 		/* if the squared difference between old and new band values 
 		 * is larger than alpha2, then increase n_changes */
 		
@@ -186,7 +190,7 @@
 			Rout.mean[n2] =0;
 		} while(n2--);
 		
-		G_message(_("row: %d, col: %d, diff2 is: %f"),row,col,diff2);
+		// G_message(_("row: %d, col: %d, diff2 is: %f"),row,col,diff2);
 	    }/* end col iteration */
 	}/* end row iteration */
     }/* end while iteration */
@@ -195,7 +199,30 @@
     else
 	G_message(_("Mean shift converged after %d iterations"), t);
     
-    /* identify connected components */
+	/*******************************************************
+    *             Part 2 cluster similar values            *
+    *         Refer clump.c to cluster super-pixels        *
+    *******************************************************/
+	G_message(_("Pass 1 of 2 for clusteing"));
+	G_percent_reset();
+	for (row = globals->row_min; row < globals->row_max; row++) {
+	    G_percent(row - globals->row_min,
+	              globals->row_max - globals->row_min, 4);
+	    for (col = globals->col_min; col < globals->col_max; col++) {
+		if (!(FLAG_GET(globals->candidate_flag, row, col)))
+		    continue;
+		}
+	}
+		/*  */
+		
+		
     
+    /*******************************************************
+    *         Part 3 identify connected components         *
+    *    Merge small super-pixel to bigger super-pixels    *
+    *******************************************************/
     return TRUE;
 }
+
+
+

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/open_files.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/open_files.c	2016-06-14 14:40:47 UTC (rev 68681)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/open_files.c	2016-06-14 16:49:20 UTC (rev 68682)
@@ -132,12 +132,13 @@
 	G_fatal_error("Unable to create input temporary files");
 	
 	/* add globals->bands_seg2 when using mean shift */
-	// if (globals->method == ORM_MS){
-	// if (Segment_open
-	// (&globals->bands_seg2, G_tempfile(), globals->nrows, globals->ncols, srows,
-	 // scols, inlen, nseg) != 1)
-	// G_fatal_error("Unable to create bands_seg2 input  files");
-	// }
+	if (globals->method == ORM_MS){
+	if (Segment_open
+	(&globals->bands_seg2, G_tempfile(), globals->nrows, globals->ncols, srows,
+	 scols, inlen, nseg) != 1)
+	G_fatal_error("Unable to create bands_seg2 input  files");
+	}
+	
     if (Segment_open
 	(&globals->rid_seg, G_tempfile(), globals->nrows, globals->ncols, srows,
 	 scols, outlen, nseg * 2) != 1)
@@ -182,10 +183,10 @@
 		G_fatal_error(_("Unable to write to temporary file"));
 
 		/* add globals->bands_seg2 when using mean shift */
-		// if (globals->method == ORM_MS){
-			// if(Segment_put(&globals->bands_seg2, (void *)globals->bands_val, row, col) !=1);
-			// G_fatal_error(_("Unable to initiate the shift-value band for mean-shift"));
-		// }
+		if (globals->method == ORM_MS){
+			if(Segment_put(&globals->bands_seg2, (void *)globals->bands_val, row, col) !=1)
+			G_fatal_error(_("Unable to initiate the shift-value band for mean-shift"));
+		}
 		
 		
 	    if (!is_null) {

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-14 14:40:47 UTC (rev 68681)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-14 16:49:20 UTC (rev 68682)
@@ -47,7 +47,7 @@
     ms_spatial_bandwidth->key = "ms_spatial_bandwidth";
     ms_spatial_bandwidth->type = TYPE_DOUBLE;
     ms_spatial_bandwidth->required = NO;
-    ms_spatial_bandwidth->answer = "3";
+    ms_spatial_bandwidth->answer = "2";
     ms_spatial_bandwidth->options = "1-32";
     ms_spatial_bandwidth->description = _("Moving window radiuns (1-32 in number of pixel) for mean shift");
     ms_spatial_bandwidth->guisection = _("MS_Settings");
@@ -186,7 +186,9 @@
 
     /* Note: this threshold is scaled after we know more at the beginning of create_isegs() */
     globals->alpha = atof(threshold->answer);
-
+	globals->ms_spatial_bandwidth = atof(ms_spatial_bandwidth->answer);
+	globals->ms_range_bandwidth = atof(ms_range_bandwidth->answer);
+	
     if (globals->alpha <= 0 || globals->alpha >= 1)
 	G_fatal_error(_("Threshold should be > 0 and < 1"));
 



More information about the grass-commit mailing list