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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 23 20:35:11 PDT 2016


Author: hao2309
Date: 2016-06-23 20:35:11 -0700 (Thu, 23 Jun 2016)
New Revision: 68756

Modified:
   sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h
   sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
   sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
   sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
Log:
add color, history, and etc

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h	2016-06-24 02:42:02 UTC (rev 68755)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h	2016-06-24 03:35:11 UTC (rev 68756)
@@ -61,7 +61,8 @@
 	/* ===parameters for ms_setting=== */	
 	double ms_spatial_bandwidth; /* "Moving window radiuns (1-32 in number of pixel) for mean shift" */
 	double ms_range_bandwidth; /* Nomarlized range bandwidth for mean shift */
-	char *ms_prefix; /* a suffix to be appended to the input bands when writing out shifted band values */
+	double ms_iter_threshold;/* Threshold when stop iteration */
+	char *ms_suffix; /* a suffix to be appended to the input bands when writing out shifted band values */
 	/* ===end parameters for ms_setting=== */
 	
     /* region info */

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-24 02:42:02 UTC (rev 68755)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-24 03:35:11 UTC (rev 68756)
@@ -292,7 +292,6 @@
     int row, col, t, n1, n2;
     int n_changes, window_size;
 	int window_row_north, window_row_south, window_col_west,window_col_east, current_window_row, current_window_col;
-    double alpha2;
     struct ngbr_stats Rin, Rout, Rwin;
     double weight, sum_of_weights, diff2;
 	SEGMENT *bands_tmp;
@@ -322,7 +321,6 @@
      *******************************************************/
 	
 	/*use local threshold for temporal testing,*/
-	alpha2 = 0.0001;
     t = 0;
     n_changes = 1;
     while (t < globals->end_t && n_changes > 0) {
@@ -408,7 +406,7 @@
 					Segment_get(globals->bands_in, (void *)Rwin.mean,current_window_row, current_window_col);
 
 					/* check range bandwidth */
-					if ((globals->calculate_similarity)(&Rin, &Rwin, globals) <= pow((globals->ms_range_bandwidth * globals->max_diff),2)){
+					if ((globals->calculate_similarity)(&Rin, &Rwin, globals) <= pow((globals->ms_range_bandwidth),2)){
 						/* sum_of_weights should be place out of loop */
 						sum_of_weights += weight;
 						int n = globals->nbands - 1;
@@ -435,7 +433,7 @@
 		Segment_get(&globals->bands_seg2, (void *)Rout.mean,row, col);
 		// G_message(_("row: %d, col: %d, Rout.mean of seg2 is: %f"),row,col,Rout.mean[0]);
 		diff2 = (globals->calculate_similarity)(&Rin, &Rout, globals);
-		if (diff2 > alpha2)
+		if (diff2 > globals->ms_iter_threshold)
 		    n_changes++;
 		
 		

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-24 02:42:02 UTC (rev 68755)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-24 03:35:11 UTC (rev 68756)
@@ -11,7 +11,7 @@
 {
     struct Option *group, *seeds, *bounds, *output, 
                   *method, *similarity, *threshold, *min_segment_size,
-				  *ms_range_bandwidth, *ms_spatial_bandwidth, *ms_prefix, // added ms_prefix, ms_range_bandwidth, ms_spatial_bandwidth for mean shift
+				  *ms_range_bandwidth, *ms_spatial_bandwidth, *ms_iter_threshold, *ms_suffix, // added ms_suffix, ms_range_bandwidth, ms_spatial_bandwidth for mean shift
 #ifdef _OR_SHAPE_
 		  *shape_weight, *smooth_weight,
 #endif
@@ -52,12 +52,21 @@
     ms_spatial_bandwidth->description = _("Moving window radiuns (1-32 in number of pixel) for mean shift");
     ms_spatial_bandwidth->guisection = _("MS_Settings");
 	
-	ms_prefix = G_define_option();
-    ms_prefix->key = "ms_prefix";
-    ms_prefix->type = TYPE_STRING;
-    ms_prefix->required = NO;
-    ms_prefix->answer = "ms_shiftval_band";
-    ms_prefix->description = _("If also output the intermediam shifted-value band");
+	/* ======threshold when stop iteration=====*/
+	ms_iter_threshold = G_define_option();
+    ms_iter_threshold->key = "ms_iter_threshold";
+    ms_iter_threshold->type = TYPE_DOUBLE;
+    ms_iter_threshold->required = NO;
+    ms_iter_threshold->answer = "0.0001";
+    ms_iter_threshold->description = _("Threshold when stop iteration");
+    ms_iter_threshold->guisection = _("MS_Settings");
+	
+	ms_suffix = G_define_option();
+    ms_suffix->key = "ms_suffix";
+    ms_suffix->type = TYPE_STRING;
+    ms_suffix->required = NO;
+    ms_suffix->answer = "ms_shiftval_band";
+    ms_suffix->description = _("If also output the intermediam shifted-value band");
     ms_spatial_bandwidth->guisection = _("MS_Settings");
 	
 	ms_range_bandwidth = G_define_option();
@@ -179,6 +188,8 @@
     /* 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_iter_threshold = atof(ms_iter_threshold->answer);
+	
 	globals->ms_range_bandwidth = atof(ms_range_bandwidth->answer);
 	
     if (globals->alpha <= 0 || globals->alpha >= 1)
@@ -274,7 +285,7 @@
     globals->ncols = Rast_window_cols();
 	
 	/* for mean shift seg2 output */
-	globals->ms_prefix = ms_prefix->answer;
+	globals->ms_suffix = ms_suffix->answer;
 	
     /* debug help */
     if (outband->answer == NULL)

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-24 02:42:02 UTC (rev 68755)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-24 03:35:11 UTC (rev 68756)
@@ -16,14 +16,27 @@
  int write_ms_seg2(struct globals *globals)
  {
 	int *seg2_fd, row, col, n;
-	DCELL **outbuf2, *seg2;
+	DCELL **outbuf2, *seg2, *min_val, *max_val;
 	struct ngbr_stats;
 	char *shifted_out_name; 
+	struct Ref Ref;		/* group reference list */
+	struct Colors colors;
+    struct History hist;
 	
+	/* open input bands */
+	if (!I_get_group_ref(globals->image_group, &Ref))
+	    G_fatal_error(_("Group <%s> not found in the current mapset"),
+			  globals->image_group);
+	if (Ref.nfiles <= 0)
+	    G_fatal_error(_("Group <%s> contains no raster maps"),
+			  globals->image_group);
 	
 	seg2_fd = G_malloc(globals->nbands * sizeof(int));
+	
 	outbuf2 = (DCELL **) G_malloc(globals->nbands * sizeof(DCELL *));
 	seg2 = G_malloc(sizeof(DCELL) * globals->nbands);
+	min_val = G_malloc(sizeof(DCELL) * globals->nbands);
+	max_val = G_malloc(sizeof(DCELL) * globals->nbands);
 	
 	for (n=0;n<globals->nbands; n++){
 	outbuf2[n] = Rast_allocate_d_buf();
@@ -32,7 +45,8 @@
     G_debug(1, "preparing shifted value output raster");
     /* open output raster map */
 	for (n=0;n<globals->nbands; n++){
-	G_asprintf(&shifted_out_name, "%s_%d", globals->ms_prefix, n);
+	G_asprintf(&shifted_out_name, "%s_%s", Ref.file[n].name, globals->ms_suffix);
+	// G_asprintf(&shifted_out_name, "%s_%d", globals->ms_suffix, n);
     seg2_fd[n] = Rast_open_new(shifted_out_name, DCELL_TYPE);
 	}
     G_debug(1, "start data transfer from seg2 file to raster");
@@ -44,6 +58,8 @@
 	
 	for (n=0;n<globals->nbands; n++){
 	Rast_set_d_null_value(outbuf2[n], globals->ncols);
+	min_val[n] = 1;
+	max_val[n] = 0;
 	}
 
 	for (col = 0; col < globals->ncols; col++){
@@ -52,6 +68,12 @@
 		Segment_get(globals->bands_out, (void *)seg2, row, col);
 			for (n=0;n<globals->nbands; n++){
 				outbuf2[n][col] = seg2[n];
+				if (seg2[n] < min_val[n]){
+					 min_val[n] = seg2[n];
+				}
+				else{
+					max_val[n] = seg2[n];
+				}
 			}
 	    }
 	}
@@ -61,13 +83,30 @@
 	}
     }
     G_percent(1, 1, 1);
-    
+	
+	
     /* close and save segment id file */
+	
 	for (n=0;n<globals->nbands; n++){
     Rast_close(seg2_fd[n]);
 	G_free(outbuf2[n]);
+	}
+	
+	for (n=0;n<globals->nbands; n++){
+	G_asprintf(&shifted_out_name, "%s_%s", Ref.file[n].name, globals->ms_suffix);
+	/* set colors */
+    Rast_init_colors(&colors);
+	Rast_make_grey_scale_fp_colors(&colors, min_val[n], max_val[n]);
+	Rast_write_colors(shifted_out_name, G_mapset(), &colors);
+	
+
+	Rast_short_history(shifted_out_name, "raster", &hist);
+	Rast_command_history(&hist);
+	Rast_write_history(shifted_out_name, &hist);
+	}
+
+	Rast_free_colors(&colors);
 	G_free(shifted_out_name);
-	}
 	return TRUE;
  }
  
@@ -116,15 +155,8 @@
     Rast_close(out_fd);
     G_free(outbuf);
 
-	/* write out the shifted mean values(seg2) */
-	if (globals->method == ORM_MS){
-		if(write_ms_seg2(globals) != TRUE)
-		{
-			G_message(_("Unable to write out shifted bands value for seg2"));
-		}
-		
-	}
 	
+	
     /* set colors */
     Rast_init_colors(&colors);
     Rast_make_random_colors(&colors, 1, maxid);
@@ -133,7 +165,16 @@
     Rast_short_history(globals->out_name, "raster", &hist);
     Rast_command_history(&hist);
     Rast_write_history(globals->out_name, &hist);
-
+	
+	
+	/* write out the shifted mean values(seg2) */
+	if (globals->method == ORM_MS){
+		if(write_ms_seg2(globals) != TRUE)
+		{
+			G_message(_("Unable to write out shifted bands value for seg2"));
+		}
+		
+	}
     /* write goodness of fit */
     if (globals->out_band) {
 	int mean_fd;



More information about the grass-commit mailing list