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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jun 15 09:51:56 PDT 2016


Author: hao2309
Date: 2016-06-15 09:51:56 -0700 (Wed, 15 Jun 2016)
New Revision: 68695

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 codes to write_output.c to output the seg2-- shifted value band

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h	2016-06-15 15:18:08 UTC (rev 68694)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h	2016-06-15 16:51:56 UTC (rev 68695)
@@ -70,7 +70,8 @@
     long ncells, notnullcells;
 
     char *out_name;		/* name of output raster map */
-    char *seeds, *bounds_map;	/* optional segment seeds and polygon constraints/boundaries */
+    char *ms_bands_out; /* name of out put shifted band for ms */
+	char *seeds, *bounds_map;	/* optional segment seeds and polygon constraints/boundaries */
     CELL lower_bound, upper_bound;
     const char *bounds_mapset;
     char *out_band;		/* indicator for segment heterogeneity */

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-15 15:18:08 UTC (rev 68694)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-15 16:51:56 UTC (rev 68695)
@@ -36,10 +36,12 @@
     int row, col, current_window_row, current_window_col, t;
     int n_changes, window_size;
 	int window_row_north, window_row_south, window_col_west,window_col_east;
-    double alpha2;
+    double alpha2, cluster_threshold;
     struct ngbr_stats Rin, Rout, Rwin;
+	struct ngbr_stats Rleft, Rabove, Rright;
     double weight, sum_of_weights, diff2;
-	double min_val, max_val;
+	double min_val, max_val, old_val, new_val;
+	double *prev_cluster, *cur_cluster, *temp_cluster;
 
 	/* G_fatal_error(_("Mean shift is not yet implemented"));
     return FALSE; */
@@ -47,6 +49,9 @@
     Rin.mean = G_malloc(globals->datasize);
 	Rwin.mean = G_malloc(globals->datasize);
     Rout.mean = G_malloc(globals->datasize);
+	Rleft.mean = G_malloc(globals->datasize);
+	Rabove.mean = G_malloc(globals->datasize);
+	Rright.mean = G_malloc(globals->datasize);
 
     /* TODO: need another segment structure holding output
      * initially, input and output are original band values */
@@ -66,7 +71,7 @@
 	
 	/*use local threshold for temporal testing,*/
 	alpha2 = 0.0001;
-	
+	cluster_threshold = 0.1;
     t = 0;
     n_changes = 1;
     while (t < globals->end_t && n_changes > 0) {
@@ -179,7 +184,8 @@
 
 		/* if the squared difference between old and new band values 
 		 * is larger than alpha2, then increase n_changes */
-		
+		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)
 		    n_changes++;
@@ -203,7 +209,7 @@
     *             Part 2 cluster similar values            *
     *         Refer clump.c to cluster super-pixels        *
     *******************************************************/
-	G_message(_("Pass 1 of 2 for clusteing"));
+	G_message(_("Pass 1 of 2 for clustering"));
 	G_percent_reset();
 	for (row = globals->row_min; row < globals->row_max; row++) {
 	    G_percent(row - globals->row_min,
@@ -211,7 +217,36 @@
 	    for (col = globals->col_min; col < globals->col_max; col++) {
 		if (!(FLAG_GET(globals->candidate_flag, row, col)))
 		    continue;
+		/* try to connect current pixel to a existing cluster */
+		old_val = new_val = 0;
+		
+		/* get the current pixel value */
+		Segment_get(globals->bands_out, (void *)Rin.mean, row, col);
+		/* get left pixel value */
+		if (col-1 < globals->col_min){
+			col = globals->col_min +1;
+			}
+		Segment_get(globals->bands_out, (void *)Rleft.mean, row, col-1);
+		
+		/* compare with left value and set if same cluster */
+		if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= pow((cluster_threshold * globals->max_diff),2)){
+			old_val = Rin.mean[0] = Rleft.mean[0];
 		}
+		/* check diag */
+		
+		/* compare with above value and set if same cluster */
+		else{
+			/* get above pixel value */
+			if (row-1 < globals->row_min){
+				row = globals->row_min+1;
+				}
+			Segment_get(globals->bands_out, (void *)Rabove.mean, row-1, col);
+			if ((globals->calculate_similarity)(&Rin, &Rabove, globals) <= pow((cluster_threshold * globals->max_diff),2)){
+				temp_cluster = prev_cluster + col;
+				// cur_cluster = 
+			}
+		}
+		}
 	}
 		/*  */
 		

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-15 15:18:08 UTC (rev 68694)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-15 16:51:56 UTC (rev 68695)
@@ -11,7 +11,7 @@
 {
     struct Option *group, *seeds, *bounds, *output, 
                   *method, *similarity, *threshold, *min_segment_size,
-				  *ms_range_bandwidth, *ms_spatial_bandwidth, *ms_suffix, *ms_shiftval_band, // added ms_suffix, ms_range_bandwidth,ms_shiftval_band, ms_spatial_bandwidth for mean shift
+				  *ms_range_bandwidth, *ms_spatial_bandwidth, *ms_suffix, *ms_bands_out, // added ms_suffix, ms_range_bandwidth,ms_bands_out, ms_spatial_bandwidth for mean shift
 #ifdef _OR_SHAPE_
 		  *shape_weight, *smooth_weight,
 #endif
@@ -52,12 +52,12 @@
     ms_spatial_bandwidth->description = _("Moving window radiuns (1-32 in number of pixel) for mean shift");
     ms_spatial_bandwidth->guisection = _("MS_Settings");
 	
-	ms_shiftval_band = G_define_option();
-    ms_shiftval_band->key = "ms_bands_out";
-    ms_shiftval_band->type = TYPE_STRING;
-    ms_shiftval_band->required = NO;
-    ms_shiftval_band->answer = "ms_shiftval_band";
-    ms_shiftval_band->description = _("If also output the intermediam shifted-value band");
+	ms_bands_out = G_define_option();
+    ms_bands_out->key = "ms_bands_out";
+    ms_bands_out->type = TYPE_STRING;
+    ms_bands_out->required = NO;
+    ms_bands_out->answer = "ms_shiftval_band";
+    ms_bands_out->description = _("If also output the intermediam shifted-value band");
     ms_spatial_bandwidth->guisection = _("MS_Settings");
 	
 	ms_range_bandwidth = G_define_option();
@@ -77,7 +77,7 @@
     ms_suffix->description = _("a suffix to be appended to the input bands when writing out shifted band values");
     ms_suffix->guisection = _("MS_Settings");
 	
-	/* ==================end parameters for ms_setting===================== */
+	/* ===end parameters for ms_setting=== */
 	
     similarity = G_define_option();
     similarity->key = "similarity";
@@ -280,7 +280,10 @@
     /* other data */
     globals->nrows = Rast_window_rows();
     globals->ncols = Rast_window_cols();
-
+	
+	/* for mean shift seg2 output */
+	globals->ms_bands_out = ms_bands_out->answer;
+	
     /* debug help */
     if (outband->answer == NULL)
 	globals->out_band = NULL;
@@ -290,7 +293,7 @@
 	else
 	    G_fatal_error(_("Invalid output raster name for goodness of fit"));
     }
-
+	
     if (endt->answer) {
 	if (atoi(endt->answer) > 0)
 	    globals->end_t = atoi(endt->answer);

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-15 15:18:08 UTC (rev 68694)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-15 16:51:56 UTC (rev 68695)
@@ -57,6 +57,45 @@
     Rast_close(out_fd);
     G_free(outbuf);
 
+	/* write out the shifted mean values(seg2) */
+	if (globals->method == ORM_MS){
+	int seg2_fd;
+	DCELL *outbuf2, seg2;
+	outbuf2 = Rast_allocate_c_buf();
+
+    G_debug(1, "preparing shifted value output raster");
+    /* open output raster map */
+    seg2_fd = Rast_open_new(globals->ms_bands_out, DCELL_TYPE);
+
+    G_debug(1, "start data transfer from seg2 file to raster");
+
+    G_message(_("Writing out seg2 ..."));
+    maxid = 0;
+    for (row = 0; row < globals->nrows; row++) {
+
+	G_percent(row, globals->nrows, 9);
+
+	Rast_set_c_null_value(outbuf2, globals->ncols);
+	for (col = 0; col < globals->ncols; col++) {
+
+	    if (!(FLAG_GET(globals->null_flag, row, col))) {
+		Segment_get(&globals->bands_seg2, (void *) &seg2, row, col);
+		
+			G_message(_("Row %d, Col %d, is: %f"),row,col,seg2);
+			
+		    outbuf2[col] = seg2;
+
+	    }
+	}
+	Rast_put_row(seg2_fd, outbuf2, DCELL_TYPE);
+    }
+    G_percent(1, 1, 1);
+    
+    /* close and save segment id file */
+    Rast_close(seg2_fd);
+    G_free(outbuf2);
+	}
+	
     /* set colors */
     Rast_init_colors(&colors);
     Rast_make_random_colors(&colors, 1, maxid);



More information about the grass-commit mailing list