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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 23 08:53:34 PDT 2016


Author: hao2309
Date: 2016-06-23 08:53:34 -0700 (Thu, 23 Jun 2016)
New Revision: 68749

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:
modified multi-bandsoutput, working on clustering

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h	2016-06-23 02:45:53 UTC (rev 68748)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h	2016-06-23 15:53:34 UTC (rev 68749)
@@ -61,7 +61,7 @@
 	/* ===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_suffix; /* a suffix to be appended to the input bands when writing out shifted band values */
+	char *ms_prefix; /* 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-23 02:45:53 UTC (rev 68748)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-23 15:53:34 UTC (rev 68749)
@@ -32,11 +32,12 @@
 
 
 int clustering(struct globals *globals){
-	int row, col, n;
-	int seg_id, class_val;
+	int row, col, n, nin;
+	int diag;/* if using diag pixel */
+	int class_val;
 	struct ngbr_stats Rin, Rleft, Rabove;
-	CELL left_new_val, above_new_val, Rtemp;
-	double min_val, max_val, old_val, new_val; 
+	CELL seg_id, left_new_val, above_new_val, *temp_clump;
+	CELL old_val, new_val; 
 	double cluster_threshold, cluster_threshold2;
 	/*******************************************************
     *             Part 2 cluster similar values            *
@@ -47,26 +48,34 @@
 	Rin.mean = G_malloc(globals->datasize);
 	Rabove.mean = G_malloc(globals->datasize);
 	
-	G_message(_("Pass 1 of 2 for clustering"));
+
 	G_percent_reset();
+	nin = globals->nbands;
+	diag=0;
 	class_val=0;
 	cluster_threshold = 0.05;
 	cluster_threshold2= pow(cluster_threshold ,2);
-	for (row = globals->row_min; row < globals->row_max; row++) {
+	G_message(_("Pass 1 of 2 for clustering"));
+	for (row = globals->row_min+1; 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;
-		old_val=new_val=0;
-		/* get the current pixel value */
-		Segment_get(&globals->bands_seg2, (void *)Rin.mean, row, col);
-		/* get left pixel value */
-		if (col == globals->col_min && row == globals->row_min){
+	    for (col = globals->col_min+1; col < globals->col_max; col++) {
+			if (!(FLAG_GET(globals->candidate_flag, row, col)))
+				continue;
+			old_val=new_val=0;
+			
+			/* get the current pixel value */
+			Segment_get(globals->bands_out, (void *)Rin.mean, row, col);
+			/* get left pixel value */
+			/* compare with left value and set if same cluster */
+			Segment_get(globals->bands_out, (void *)Rleft.mean, row, (col-1));
+			
+			
+			if (col == globals->col_min && row == globals->row_min){
 			class_val+=1;
 			seg_id=class_val;
-		}
-		else if (row == globals->row_min){
+			}
+			else if (row == globals->row_min){
 			/* compare with left value and set if same cluster */
 			Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
 			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
@@ -79,8 +88,8 @@
 				class_val+=1;
 				seg_id=class_val;
 			}
-		}
-		else if(col == globals->col_min){
+			}
+			else if(col == globals->col_min){
 			/* compare with above value and set if same cluster */
 			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
 			if ((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
@@ -93,66 +102,188 @@
 				class_val+=1;
 				seg_id=class_val;
 			}
-		}
-		else{
-			/* compare with left value then above value */
-			Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
-			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
+			}
+			else{
 			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
-				G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
 				Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
-				old_val =seg_id = left_new_val;
+				old_val=seg_id = left_new_val;
+			} 
+			if(diag){
+				/* check above right, center, left, in that order */
 			}
-			if((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
-				G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
-				Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
-				seg_id = above_new_val;
-				if (old_val == 0){
-					old_val = above_new_val;
-				}
-				else{
-					new_val =  above_new_val;
-					if (old_val != new_val){
-					/* conflict! preserve NEW clump ID and change OLD clump ID.
+			else{
+				/* compare with above value and set if same cluster */
+				Segment_get(&globals->bands_seg2, (void *)	Rabove.mean, row-1, col);
+				if ((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
+					
+					Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
+					temp_clump = &above_new_val;
+					seg_id = *temp_clump;
+					if (old_val==0){
+						old_val = *temp_clump;
+					}
+					else{
+					new_val = *temp_clump;
+					if(new_val != old_val){
+						/* conflict! preserve NEW clump ID and change OLD clump ID.
 					* Must go back to the left in the current row and to the right
 					* in the previous row to change all the clump values as well.
 					*/
+
+					/* left of the current row from 1 to col - 1 */
 					for(n = globals->col_min+1; n< col; n++){
-						Segment_get(&globals->rid_seg, (void *)&Rtemp, row, n);
-						if (Rtemp==old_val){
+						Segment_get(&globals->rid_seg, (void *)temp_clump, row, n);
+						if (*temp_clump==old_val){
 							Segment_put(&globals->rid_seg, &new_val, row, n);
 						}
 					}
 					// /* right of previous row from col + 1 to ncols */
 					for(n = col+1; n< globals->col_max; n++){
-						Segment_get(&globals->rid_seg, (void *)&Rtemp, row-1, n);
-						if (Rtemp==old_val){
+						Segment_get(&globals->rid_seg, (void *)temp_clump, row-1, n);
+						if (*temp_clump==old_val){
 							Segment_put(&globals->rid_seg, &new_val, row-1, n);
 						}
 					}
+					}
 				}
+			}
+			}
+			if (new_val = 0 || old_val == new_val){
+				if (old_val ==0){
+					class_val+=1;
+					seg_id = class_val;
 				}
 			}
-			if (new_val ==0 && old_val ==0){
-				class_val+=1;
-				seg_id = class_val;
-			}
-			Segment_put(&globals->rid_seg, &seg_id, row, col);
+			
 		}
+		Segment_put(&globals->rid_seg, &seg_id, row, col);
+		}
+	}
+	return TRUE;
+}
+
+// /* /* int clustering(struct globals *globals){
+	// int row, col, n;
+	// int seg_id, class_val;
+	// struct ngbr_stats Rin, Rleft, Rabove;
+	// CELL left_new_val, above_new_val, Rtemp;
+	// double min_val, max_val, old_val, new_val; 
+	// double cluster_threshold, cluster_threshold2;
+	// /*******************************************************
+    // *             Part 2 cluster similar values            *
+    // *         Refer clump.c to cluster super-pixels        *
+    // *******************************************************/
+	
+	// Rleft.mean = G_malloc(globals->datasize);
+	// Rin.mean = G_malloc(globals->datasize);
+	// Rabove.mean = G_malloc(globals->datasize);
+	
+	// G_message(_("Pass 1 of 2 for clustering"));
+	// G_percent_reset();
+	// class_val=0;
+	// cluster_threshold = 0.05;
+	// cluster_threshold2= pow(cluster_threshold ,2);
+	// 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;
+		// old_val=new_val=0;
+		// /* get the current pixel value */
+		// Segment_get(&globals->bands_seg2, (void *)Rin.mean, row, col);
+		// /* get left pixel value */
+		// if (col == globals->col_min && row == globals->row_min){
+			// class_val+=1;
+			// seg_id=class_val;
+		// }
+		// else if (row == globals->row_min){
+			// /* compare with left value and set if same cluster */
+			// Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
+			// if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
+				// G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
+				// Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
+				// seg_id = left_new_val;
+			// }
+			// /* end compare left */
+			// else{
+				// class_val+=1;
+				// seg_id=class_val;
+			// }
+		// }
+		// else if(col == globals->col_min){
+			// /* compare with above value and set if same cluster */
+			// Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
+			// if ((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
+				// G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
+				// Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
+				// seg_id = above_new_val;
+			// }
+			// /* end compare above */
+			// else{
+				// class_val+=1;
+				// seg_id=class_val;
+			// }
+		// }
+		// else{
+			// /* compare with left value then above value */
+			// Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
+			// Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
+			// if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
+				// G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
+				// Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
+				// old_val =seg_id = left_new_val;
+			// }
+			// if((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
+				// G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
+				// Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
+				// seg_id = above_new_val;
+				// if (old_val == 0){
+					// old_val = above_new_val;
+				// }
+				// else{
+					// new_val =  above_new_val;
+					// if (old_val != new_val){
+					// /* conflict! preserve NEW clump ID and change OLD clump ID.
+					// * Must go back to the left in the current row and to the right
+					// * in the previous row to change all the clump values as well.
+					// */
+					// for(n = globals->col_min+1; n< col; n++){
+						// Segment_get(&globals->rid_seg, (void *)&Rtemp, row, n);
+						// if (Rtemp==old_val){
+							// Segment_put(&globals->rid_seg, &new_val, row, n);
+						// }
+					// }
+					/* right of previous row from col + 1 to ncols */
+					// for(n = col+1; n< globals->col_max; n++){
+						// Segment_get(&globals->rid_seg, (void *)&Rtemp, row-1, n);
+						// if (Rtemp==old_val){
+							// Segment_put(&globals->rid_seg, &new_val, row-1, n);
+						// }
+					// }
+				// }
+				// }
+			// }
+			// if (new_val ==0 && old_val ==0){
+				// class_val+=1;
+				// seg_id = class_val;
+			// }
+			// Segment_put(&globals->rid_seg, &seg_id, row, col);
+		// }
 		
 		
-		G_debug(1, "row: %d, col: %d, Rin is: %f",row,col,Rin.mean[0]);
+		// G_debug(1, "row: %d, col: %d, Rin is: %f",row,col,Rin.mean[0]);
 		
-		G_debug(1, "row: %d, col: %d, seg_id is: %d",row,col,seg_id);
+		// G_debug(1, "row: %d, col: %d, seg_id is: %d",row,col,seg_id);
 		// G_debug(1, "row: %d, col: %d, cluster_threshold * globals->max_diff is: %f",row,col,pow((cluster_threshold * globals->max_diff),2));
-		/* compare with above value and set if same cluster */
-		Segment_put(&globals->rid_seg, &seg_id, row, col);
-		}
-	}
+		// /* compare with above value and set if same cluster */
+		// Segment_put(&globals->rid_seg, &seg_id, row, col);
+		// }
+	// }
 
 	
-	return TRUE;
-}
+	// return TRUE;
+// } */ */
 
 
 

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-23 02:45:53 UTC (rev 68748)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c	2016-06-23 15:53:34 UTC (rev 68749)
@@ -11,7 +11,7 @@
 {
     struct Option *group, *seeds, *bounds, *output, 
                   *method, *similarity, *threshold, *min_segment_size,
-				  *ms_range_bandwidth, *ms_spatial_bandwidth, *ms_suffix, // added ms_suffix, ms_range_bandwidth, ms_spatial_bandwidth for mean shift
+				  *ms_range_bandwidth, *ms_spatial_bandwidth, *ms_prefix, // added ms_prefix, ms_range_bandwidth, 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_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_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");
     ms_spatial_bandwidth->guisection = _("MS_Settings");
 	
 	ms_range_bandwidth = G_define_option();
@@ -274,7 +274,7 @@
     globals->ncols = Rast_window_cols();
 	
 	/* for mean shift seg2 output */
-	globals->ms_suffix = ms_suffix->answer;
+	globals->ms_prefix = ms_prefix->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-23 02:45:53 UTC (rev 68748)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-23 15:53:34 UTC (rev 68749)
@@ -18,7 +18,7 @@
 	int *seg2_fd, row, col, n;
 	DCELL **outbuf2, *seg2;
 	struct ngbr_stats;
-	char suffix[50], band_num[10];
+	char *shifted_out_name; 
 	
 	
 	seg2_fd = G_malloc(globals->nbands * sizeof(int));
@@ -32,10 +32,8 @@
     G_debug(1, "preparing shifted value output raster");
     /* open output raster map */
 	for (n=0;n<globals->nbands; n++){
-	strcpy(suffix,  globals->ms_suffix);
-    strcpy(band_num, itoa(n,band_num,10));
-	strcat(suffix,band_num);
-    seg2_fd[n] = Rast_open_new(suffix, DCELL_TYPE);
+	G_asprintf(&shifted_out_name, "%s_%d", globals->ms_prefix, n);
+    seg2_fd[n] = Rast_open_new(shifted_out_name, DCELL_TYPE);
 	}
     G_debug(1, "start data transfer from seg2 file to raster");
 
@@ -68,6 +66,7 @@
 	for (n=0;n<globals->nbands; n++){
     Rast_close(seg2_fd[n]);
 	G_free(outbuf2[n]);
+	G_free(shifted_out_name);
 	}
 	return TRUE;
  }



More information about the grass-commit mailing list